用javase开发宠物商店

xiaoxiao2021-02-28  72

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 + "]";     }      }
转载请注明原文地址: https://www.6miu.com/read-38913.html

最新回复(0)