首页 分享 宠物商店

宠物商店

来源:萌宠菠菠乐园 时间:2024-09-17 14:47

宠物商店

最新推荐文章于 2023-09-07 16:46:51 发布

多笑笑smile 于 2018-11-30 13:44:24 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

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,宠物的分类。

相关知识

宠物商店加盟
宠物商店 案例分析
宠物商店营销策略
科学网—宠物商店
宠物商店详细设计说明书
@宠物商店 的个人主页
bootstrap5实现宠物商店网站 Cat
宠物商店网站毕业论文.docx
宠物商店业务
动物救援:宠物商店下载2.1.2最新版

网址: 宠物商店 https://www.mcbbbk.com/newsview177270.html

所属分类:萌宠日常
上一篇: 深度学习+大规模计算+大数据=人
下一篇: 图示起重架的最大起吊重量(包括行

推荐分享