단순 체인 클래스(애완동물 상점 모형)

19861 단어 연습하다
interface ILink{

}
class Link implements ILink{
    private class  Node{ 
        private Object data;
        private Node next;
        public Node(Object data){
            this.data=data;
        }
        public Object getData() {
            return data;
        }
        public void setData(Object data) {
            this.data = data;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }
        //this= 
        public void addNode(Node node){
            if(this.next==null){// 
                this.next=node;
            }else{
                this.next.addNode(node);
            }

        }
        public void toArrayNode(){
            Link.this.reData[Link.this.foot++]=this.data;
            if(this.next!=null){
                this.next.toArrayNode();
            }

        }
        public boolean containsNode(Object search){
            if(this.data.equals(search)){
                return true;
            }else{
                if(this.next!=null){
                    return this.next.containsNode(search);
                }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 newdata){
            if(Link.this.foot++==index){
                this.data=newdata;
            }else{
                this.next.setNode(index, newdata);
            }
        }
        //previous=this.root
        //this=root.next
        public void removeNode(Object data,Node previous){
            if(this.data.equals(data)){
                previous.next=this.next ;
            }else{
                this.next.removeNode(data, this);
            }
        }

    }
    //------------------------------
    private Object []reData;
    private int foot;
    private int count=0;
    private Node root;// 
    public void add(Object data){// 
        if(data==null){
            return;// 
        }
        Node node=new Node(data); 
        if(this.root==null){
        this.root=node;
        }else{
            this.root.addNode(node);
        }
        this.count++;

    }
    public int size(){// 
        return this.count;
    }
    public boolean isEmpty(){// 
        return this.root==null&&this.count==0;
    }
    public Object [] toArray(){
        if(this.count==0){
        return null;
        }
        this.reData=new Object[this.count];// 
        this.foot=0;
        this.root.toArrayNode();
        return this.reData; 
    }
    public boolean contains(Object search){
        if(search==null&&this.root==null){
            return false;
        }else{
            return this.root.containsNode(search);
        }
    }
    public Object get(int index){
            if(index>=this.count){
                return "wrong";
            }else{
                this.foot=0;
                return this.root.getNode(index);
            }
        }
    public void set(int index,Object newdata){
        if(index>=this.count&&newdata==null){
            return;
        }else{
            this.foot=0;
            this.root.setNode(index, newdata);
        }
    }
    public void remove(Object data){
        if(this.contains(data)){
            if(this.root.data.equals(data)){
                this.root=this.root.next;
            }else{
                this.root.next.removeNode(data, this.root);
            }this.count--;
        }
    }
}
interface Pets{
    public String getName();
    public String getColor();
    public int getAge();
}
class PetShop{
    private Link pets=new Link();// , 
    public void add(Pets pet){
        this.pets.add(pet);
    }
    public void delet(Pets pet){
        this.pets.remove(pet);
    }
    public Link getPets(){
        return this.pets;
    }
    public Link search(String keyWord){
        Link result=new Link();
        Object []data=this.pets.toArray();
        for(int x=0;xif(pet.getName().contains(keyWord)||pet.getColor().contains(keyWord)){
                result.add(pet);
            }
        }
        return result;
    }
}
class Dog implements Pets{
    private String name;
    private int age;
    private String color;
    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return this.name;
    }
    @Override
    public String getColor() {
        // TODO Auto-generated method stub
        return this.color;
    }
    @Override
    public int getAge() {
        // TODO Auto-generated method stub
        return this.age;
    }
    public Dog(String name,String color,int age){
        this.name=name;
        this.color=color;
        this.age=age;
    }
    public boolean equals(Object obj){
        if(obj==null){
            return false;
        }if(this==obj){
            return true;
        }if(!(obj instanceof Dog)){
            return false;
        }
        Dog pet=(Dog) obj;
        return this.name.equals(pet.name)&&this.age==pet.age&&this.color.equals(pet.color);
    }
    public String toString(){
        return this.name+this.color+this.age;
    }
}
class Cat implements Pets{
    private String name;
    private int age;
    private String color;
    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return this.name;
    }
    @Override
    public String getColor() {
        // TODO Auto-generated method stub
        return this.color;
    }
    @Override
    public int getAge() {
        // TODO Auto-generated method stub
        return this.age;
    }
    public Cat(String name,String color,int age){
        this.name=name;
        this.color=color;
        this.age=age;
    }
    public boolean equals(Object obj){
        if(obj==null){
            return false;
        }if(this==obj){
            return true;
        }if(!(obj instanceof Dog)){
            return false;
        }
        Cat pet=(Cat) obj;
        return this.name.equals(pet.name)&&this.age==pet.age&&this.color.equals(pet.color);
    }
    public String toString(){
        return this.name+this.color+this.age;
    }
}
//==============================================
public class test2 {
    public static void main(String args[]){
        PetShop ps=new PetShop();
        Dog dog1=new Dog(" ","witer",1);
        ps.add(dog1);
        ps.add(new Dog("hei","black",10));
        ps.add(new Dog(" ","black",10));
        ps.add(new Dog(" ","black",10));
        ps.add(new Dog(" ","black",10));
        ps.delet(new Dog(" ","black",10));
        Link all=ps.search("witer");

        Object[]data=all.toArray();
        for(int x=0;xfor(int x=0;x

좋은 웹페이지 즐겨찾기