久染

导航

Java day18【第三十章】单链表案例实战

【第三十章】单链表案例实战

一.宠物商店:

现在假设一个宠物商店,里面可以出售各种宠物,要求可以实现宠物的上架,下架处理,以及查询宠物的信息。

 

interface ILink<E>{
    public void add(E date);
    public int getsize();
    public boolean isEmpty();
    public Object[] toArray();
    public E getdate(int index);
    public void set(int index,E date);
    public boolean contain(E date);
    public void remove(E date);
    public void clean();
}
class SetLink<E> implements ILink<E>{
    private int size;
    private Node<E> root;
    public Object [] returnDate;
    private int foot ;
    //定义节点类
    public class Node<E>{
        E date;
        Node<E> next;
        Node<E> previous;
        public Node(E date, Node<E> next) {
            this.date = date;
            this.next = next;
        }
        
        //获取链表数据辅助
        public void toArraynode() {
            SetLink.this.returnDate[SetLink.this.foot ++] = this.date;
            if(this.next != null) {
                this.next.toArraynode();
            }
        }
        //辅助根据索引获取数据
        public E getnode(int index) {
            if(SetLink.this.foot ++ == index) {
                return this.date;
            }else {
                return this.next.getnode(index);
            }
        }
        
        //找到索引位置.
        public void setnode(int index,E date) {
            if(SetLink.this.foot == index) {
                this.date= date;
            }else {
                this.next.setnode(index,date);;
            }
        }
        
        //判断数据存在
        public boolean containnode(E date) {
            if(this.date.equals(date)) {
                return true;
            }else {
                if(this.next == null) {
                    return false;
                }else {
                    return this.next.containnode(date);
                }
            }
        }
        
        //删除节点
        public void removeNode(Node<E> previous,E date) {
            if(this.date.equals(date)) {
                previous.next = this.next;
            }else {
                if(this.next!=null) {
                    this.next.removeNode(this,date);
                }
            }
        }
    }
    //增加数据
    public void add(E date) {
        if(root == null) {
            root = new Node<E>(date , null);
        }else {
            Node<E> current = root;
            while(current.next != null) {
                current = current.next;
            }
            current.next = new Node<E>(date,null);
        }
        size ++;
    }
    //获取链表长度
    public int getsize() {
        return size;
    }
    //判断链表是否为空
    public boolean isEmpty() {
        return this.size ==0;
    }
    //将数据以数组形式返回
    public Object[] toArray() {
        if(root == null) {
            return null;
        }
        this.foot = 0;
        this.returnDate = new Object[this.size] ;
        this.root.toArraynode();
        return returnDate;
    }    
    //获取索引位置的数据:
    public E getdate(int index) {
        if(index > this.size) {
            return null;
        }
        this.foot = 0;
        return this.root.getnode(index);
    }
    //修改指定位置的数据
    public void set(int index , E date) {
        this.foot = 0;
        this.root.setnode(index,date) ;
    }
    //判断指定数据是否存在
    public boolean contain(E date) {
        return this.root.containnode(date);
    }
    //数据删除
    public void remove(E date) {
        if(this.contain(date)) {
            if(this.root.date.equals(date)) {
                this.root = this.root.next;
            }else {
                this.root.next.removeNode(this.root, date);
            } 
            this.size--;
        }
    }
    //清空链表:
    public void clean() {
        this.root = null ;
        this.size = 0;
    }
}

interface Pet{
    public String getname();
    public String getcolor();
}
class Petshop{
    private ILink<Pet> allpets = new SetLink<Pet>();
    public void add(Pet pet) {
        this.allpets.add(pet);
    }
    public void delete(Pet pet) {
        this.allpets.remove(pet);
    }
    public ILink<Pet> search(String keyword){
        ILink<Pet> searchresult = new SetLink<Pet>();
        Object result[] = this.allpets.toArray();
        if(result !=null ) {
            for(Object obj:result) {
                Pet pet = (Pet)obj;
                if(pet.getname().contains(keyword)||
                    pet.getcolor().contains(keyword)) {
                    searchresult.add(pet);
                }
            }
        }
        return searchresult;
    }
}
class Cat implements Pet{
    private String name;
    private String color;
    public Cat(String name,String color) {
        this.name = name;
        this.color = color;
    }
    public String getname() {
        return this.name;
    }
    public String getcolor() {
        return this.color;
    }
    public String toString() {
        return "【宠物猫】名字:"+this.name+"、颜色:"+this.color;
    }
}
class Dog implements Pet{
    private String name;
    private String color;
    public Dog(String name,String color) {
        this.name = name;
        this.color = color;
    }
    public String getname() {
        return this.name;
    }
    public String getcolor() {
        return this.color;
    }
    public String toString() {
        return "【宠物狗】名字:"+this.name+"、颜色:"+this.color;
    }
}
public class ListCon{
    public static void main(String args[]) {
        Petshop shop = new Petshop();
        shop.add(new Cat("侯","Blue"));
        shop.add(new Dog("侯h","Green"));
        shop.add(new Cat("侯hh","Green"));
        shop.add(new Cat("侯hhh","Yellow"));
        Object result[] = shop.search("h").toArray();
        for(Object obj : result) {
            System.out.println(obj);
        }
        }
}

 

二. 超市购物车:

  使用面向对象的概念表示出下面的生活场景:小明去超市买东西,所有买到的东西都放在了购物车,最后到收银员结账。

interface IGoods{
    public String getname();
    public double getprice();
}
interface IShopcar{
    public void add(IGoods goods);
    public void delete(IGoods goods);
    public Object [] getAll();
}
interface ICashier{
    public int getNumber();
    public double getPrice(); 
}
class Shopcar implements IShopcar{
    ILink<IGoods> allgoods = new SetLink<IGoods>();
    public void add(IGoods goods) {
        this.allgoods.add(goods);
    }
    public void delete(IGoods goods) {
        this.allgoods.remove(goods);
    }
    public Object [] getAll() {
        return this.allgoods.toArray();
    }
}
class Cashier {
    private int number;
    private double price;
    private IShopcar shopcar;
    public Cashier(IShopcar shopcar) {
        this.shopcar = shopcar;
    }
    public int getNumber() {
        return this.shopcar.getAll().length;
    }
    public double getPrice() {
        double all =0.0;
        Object result [] = this.shopcar.getAll();
        for(Object obj : result) {
            IGoods objs = (IGoods)obj;
            all = all + objs.getprice();
        }
        return all;
    }
}
class Bag implements IGoods{
    private String name;
    private double price;
    public Bag(String name,double price) {
        this.name = name;
        this.price = price;
    }
    public String getname() {
        return this.name;
    }
    public double getprice() {
        return this.price;
    }
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Bag)) {
            return false;
        }
        Bag bag = (Bag)obj;
        return this.name.equals(bag.name) && this.price == bag.price;
    }
    public String toString() {
        return "【背包信息】背包:"+this.name + "、价格:"+this.price;
    }
}
class Book implements IGoods{
    private String name;
    private double price;
    public Book(String name,double price) {
        this.name = name;
        this.price = price;
    }
    public String getname() {
        return this.name;
    }
    public double getprice() {
        return this.price;
    }
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Book)) {
            return false;
        }
        Book book = (Book)obj;
        return this.name.equals(book.name) && this.price == book.price;
    }
    public String toString() {
        return "【书籍信息】书名:"+this.name + "、价格:"+this.price;
    }
}
public class ListCon{
    public static void main(String args[]) {
            IShopcar car = new Shopcar();
            car.add(new Bag("taidi",88));
            car.add(new Bag("tscde",84));
            Cashier cas = new Cashier(car);
            Object[] goods = car.getAll();
            for(Object obj : goods) {
                System.out.println(obj);
            }
            System.out.println("【购物小票】"+"购买总数:"+cas.getNumber()+"、总价格:"+cas.getPrice());
        }
}

 

 

posted on 2019-11-09 18:06  久染  阅读(217)  评论(0编辑  收藏  举报