宠物商店

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object [] toArray();
    public E get(int index);
    public void set(int index,E data);
    public boolean contains(E data);
    public void remove(E data);
    public void clean();
}
class ILinkImpl<E> implements ILink<E>{
    private class Node{
        private Node next;
        private E data;
        public Node(E data){
            this.data = data;
        }
        public void addNode(Node newNode){
            if(this.next == null){
                this.next = newNode;
            }else{
                this.next.addNode(newNode);//保存节点
            }
        }
        public void toArrayNode(){
            ILinkImpl.this.returnData[ILinkImpl.this.foot ++] = this.data;
            if(this.next != null){
                this.next.toArrayNode();
            }
        }
        public E getNode(int index){
            if(ILinkImpl.this.foot ++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E data){
            if(ILinkImpl.this.foot ++ == index){
                this.data = data;
            }else{
                this.next.setNode(index,data);
            }
        }
        public boolean containsNode(E data){
            if(data.equals(this.data)){
                return true;
            }else{
                if(this.next != null){
                    return this.next.containsNode(data);
                }else{
                    return false;
                }
            }
        }
        public void removeNode(Node previous,E data){
            if(this.data.equals(data)){
                previous = this.next;
            }else{
                this.next.removeNode(this,data);
            }
        }
    }
    private Node root;
    private int count;
    private int foot;
    private Object [] returnData;
    public void add(E e){
        if(e == null){
            return ;
        }
        Node newNode = new Node(e);
        if(this.root == null){
            this.root = newNode;
        }else{
            this.root.addNode(newNode);//保存数据
        }
        this.count ++;
    }
    public int size(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.root == null;
    }
    public Object [] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;
        this.returnData = new Object[this.count];
        this.root.toArrayNode();
        return this.returnData;
    }
    public E get(int index){
        if(index >= this.count){
            return null;
        }
        this.foot = 0;
        return this.root.getNode(index);
    }
    public void set(int index,E data){
        if(data == null){
            return ;
        }
        this.foot = 0;
        this.root.setNode(index,data);
    }
    public boolean contains(E data){
        if(data == null){
            return false;
        }
        return this.root.containsNode(data);
    }
    public void remove(E data){
        if(this.contains(data)){
            if(this.root.data.equals(data)){
                this.root = this.root.next;
            }else{
                this.root.removeNode(this.root,data);
            }
            this.count --;
        }
    }
    public void clean(){
        this.count = 0;
        this.root = null;
    }
}
class ShopPet{
    private ILink<Pet> allPets = new ILinkImpl<Pet>();
    public void add(Pet pet){
        this.allPets.add(pet);
    }
    public void remove(Pet pet){
        this.allPets.remove(pet);
    }
    public ILink<Pet> search(String keyword){
        ILink<Pet> searchResult = new ILinkImpl<Pet>();
        Object [] result = 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;
    }
}
interface Pet{
    public String getName();
    public String getColor();
}
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 boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(!(obj instanceof Cat)){
            return false;
        }
        if(obj == this){
            return true;
        }
        Cat cat = (Cat) obj;
        return this.name.equals(cat.getName()) && this.color.equals(cat.getColor());
    }
    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 boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(!(obj instanceof Dog)){
            return false;
        }
        if(obj == this){
            return true;
        }
        Dog dog = (Dog) obj;
        return this.name.equals(dog.getName()) && this.color.equals(dog.getColor());
    }
    public String toString(){
        return "【宠物信息:】名称:" + this.name + "、颜色:" + this.color;
    }
}
public class JavaDemo{
    public static void main(String args []){
        ShopPet shop = new ShopPet();
        shop.add(new Dog("黑狗","黑色"));
        shop.add(new Cat("斑点猫","黑色"));
        shop.add(new Cat("花猫","灰色"));
        shop.add(new Dog("斑点狗","黑白色"));
        Object result [] = shop.search("猫").toArray();
        for(Object obj : result){
            System.out.println(obj.toString());
        }
    }
}

宠物商店,使用链表完成,练习代码

1,有宠物标准;

2,宠物上点与链表的关系;

3,宠物标准与宠物商店的关系;

4,宠物的分类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值