用javase开发宠物商店

public class Test6 {    //测试类
    public static void main(String args[]){
        PetShop shop = new PetShop();
        shop.add(new Cat("wang",20));
        shop.add(new Cat("ming",10));
        shop.add(new Dog("wang",20));
        shop.add(new Dog("ming",10));
        Link all = shop.search("wang");
        Object[] obj = all.toArray();
        for(int x = 0 ; x < obj.length ; x ++){
            System.out.println(obj[x]);
        }
    }
}
class Link{
    
    private class Node{
        private Object data;    //保存数据
        private Node next;        //保存关系
        public Node(Object data){        
            this.data = data;
        }
        public void addNode(Node newNode){  //添加新数据
            if(this.next == null){
                this.next = newNode;
            }else{
                this.next.addNode(newNode);        //递归调用
            }
        }
        public boolean containsNdoe(Object data){
            if(data.equals(this.data)){
                return true;
            }else{
                if(this.next != null){
                    return this.next.containsNdoe(data);
                }else{
                    return false;
                }
            }
        }
        public Object getNode(int index){
            if(Link.this.foot ++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,Object data){
            if(Link.this.foot ++ == index){
                this.data = data;
            }else{
                this.next.setNode(index, data);
            }
        }
        public void removeNode(Node previous,Object data){
            if(data.equals(this.data)){
                previous.next = this.next;
            }else{
                this.next.removeNode(this, data);
            }
        }
        public void toArraNode(){
            Link.this.retArray[Link.this.foot ++] = this.data;
            if(this.next != null){
                this.next.toArraNode();
            }
        }
    }
    
    
    
    private Node root;        //定义根节点
    private int count = 0;        //保存数据的个数
    private int foot = 0;        //定义小标
    private Object[] retArray;    //保存数据的数组
    public void add(Object data){
        if(data == null){
            return ;
        }
        Node newNode = new Node(data);
        if(this.root == null){
            this.root = newNode;
        }else{
            this.root.addNode(newNode);
        }
        this.count ++;
    }
    public int size(){
        return this.count;
    }
    public boolean isEmpty(){
        return count == 0;
    }
    public Object get(int index){
        if(index > count){
            return null;
        }else{
            this.foot = 0;
            return this.root.getNode(index);
        }
    }
    public boolean contians(Object data){
        if(data == null || this.root == null){
            return false;
        }
        return this.root.containsNdoe(data);
    }
    public void set(int index,Object data){
        if(index > this.count){
            return ;
        }
        this.foot = 0;
        this.root.setNode(index, data);
    }
    public Object[] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;
        this.retArray = new Object[this.count];
        this.root.toArraNode();
        return this.retArray;
    }
    public void remove(Object data){
        if(this.contians(data)){
            if(data.equals(this.root.data)){
                this.root = this.root.next;
            }else{
                this.root.removeNode(this.root, data);
            }
            count --;
        }
    }
    
}

interface IPet{        //宠物接口,所有的宠物要满足此接口才可以
    public String getName();
    public int getAge();
}
class PetShop{        //定义宠物商店
    private Link perts = new Link();
    public void add(IPet pet){        //宠物添加
        this.perts.add(pet);
    }
    public void delete(IPet pet){    //宠物删除
        this.perts.remove(pet);
    }
    public Link search(String keyword){        //关键字查找,根据名字
        Link result = new Link();
        Object[] obj = this.perts.toArray();
        for(int x = 0 ; x < obj.length ; x ++){
            IPet p = (IPet) obj[x];
            if(p.getName().contains(keyword)){
                result.add(p);
            }
        }
        return result;
    }
}
class Cat implements IPet{
    private String name;
    private int age;
    public Cat(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public boolean equals(Object obj){
        if(obj == this){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof Cat)){
            return false;
        }
        Cat c = (Cat) obj;
        if(this.name.equals(c.name) && this.age == c.age){
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return "Cat [name=" + name + ", age=" + age + "]";
    }
    
}

class Dog implements IPet{
    private String name;
    private int age;
    public Dog(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public boolean equals(Object obj){
        if(obj == this){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof Dog)){
            return false;
        }
        Dog c = (Dog) obj;
        if(this.name.equals(c.name) && this.age == c.age){
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return "Dog [name=" + name + ", age=" + age + "]";
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值