Java面向對象編程(寵物商店)


寵物商店

現在要求建立寵物商店,里面就有進行要銷售寵物的上架,下架,關鍵字查詢。

要求:描述出程序的關系即可

那么假設對於寵物的信息只要求有三項:名字,年齡,顏色  

那么此時對於的關系:一個寵物商店會有多種寵物。如果按照關系表來講這是一對多的關系映射,但是現在的問題是,一方是寵物商店,而多方應該是寵物,但是寵物又分為貓,狗,豬,魚 等等。 

1.創建鏈表

 

public 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); // 判斷數據是否存在
}

/**
 * @author Gu
 * @cracked 2020-03-25 9:24
 */
class LinkImpl<E> implements ILink<E> {    // 負責鏈表的操作
    // 將Node定義為內部類,表示Node類只為Link類
    private class Node {        // 負責數據與節點關系的匹配
        private Object data; // 保存節點的數據
        private Node next; // 保存下一個節點
        public Node(Object data) {
            this.data = data;
        }
        // 第1次調用:this = Link.root
        // 第2次調用:this = Link.root.next;
        // 第3次調用:this = Link.root.next;
        public void addNode(Node newNode) { // 處理節點關系
            if(this.next == null) { //當前節點下一個為空
                this.next = newNode;
            }else{ // 現在當前節點的下一個不為空
                this.next.addNode(newNode);
            }
        }
        // 第一次調用:this = Link.root
        // 第一次調用:this = Link.root.next
        public void toArrayNode() {
            LinkImpl.this.retData[LinkImpl.this.foot++] = this.data;
            if(this.next != null){ // 現在還有下一個節點
                this.next.toArrayNode();
            }
        }
        // 第一次調用:this = Link.root
        // 第一次調用:this = Link.root.next
        public boolean containsNode(Object search) {
            if(search.equals(this.data)){ // 找到了
                return true;
            }else {
                if(this.next != null) { // 當前節點之后
                    return this.next.containsNode(search);
                }else { // 沒有節點
                    return false;
                }
            }
        }
        // 第一次調用: this = Link.root
        // 第一次調用: this = Link.root.next
        public Object getNode(int index){
            if(LinkImpl.this.foot++ == index) {
                return this.data;
            }else {
                this.next.getNode(index);
            }
            return null;
        }
        public void setNode(int index,Object newData) {
            if(LinkImpl.this.foot++ == index) { // 索引相同
                this.data = newData;
                return ; // 結束
            }else {
                if(this.next != null) {
                    this.next.setNode(index,newData);
                }
            }
        }
        // 第一次調用:this = Link.root.next,previous = Link.root;
        // 第二次調用:this = Link.root.next.next,previous = Link.root.next;
        public void removeNode(Node previous,Object data) {
            if(this.data.equals(data)) { // 當前節點為要刪除節點
                previous.next = this.next; // 刪除當前了
            }else {
                this.next.removeNode(this,data);
            }
        }
    }
    //----------------以下為link類------------//
    private Object[] retData; // 返回類型
    private int foot = 0; // 操作腳標
    private int count = 0; // 當前的保存個數
    private Node root; // 屬於根節點,沒有根節點就無法進行數據的保存
    public void add(Object data) {
        if(data == null) {// 人為的追加了規定,不允許存放空值
            return ; // 方法結束調用
        }
        // 如果要想進行數據的保存,那么就必須將數據封裝在Node節點類里面
        // 如果沒有封裝,則無法確認好節點的先后順序
        Node newNode = new Node(data);
        if(this.root == null) { // 當前並沒有根節點
            this.root = newNode; // 第一個節點設置為根節點
        }else{ // 根節點已經存在了
            // 應該把此時的節點順序的處理交給Node類自己完成
            this.root.addNode(newNode);
        }
        count++;
    }
    public int size() { // 取得元素個數
        return this.count;
    }
    public boolean isEmpty() {
        return this.root == null && this.count == 0;
    }
    public boolean contains(Object search) {
        // 沒有要查詢的內容以及鏈表為空
        if(search == null || this.root == null) {
            return false;
        }
        return this.root.containsNode(search);
    }
    public Object[] toArray() {
        if(this.count == 0) {
            return null;
        }
        // 現在鏈表中存在有數據,則開辟指定長度的數組
        // 該數組一定要交給Node類進行處理
        this.retData = new Object[this.count];
        this.foot = 0; // 進行清零的處理,需要進行腳標操作
        this.root.toArrayNode(); // 將數據的取出處理交給Node類完成
        return this.retData;
    }
    public void set(int index,Object newData) {
        if(index >= this.count){ // 超過了保存的個數
            return ; // 結束方法調用
        }
        this.foot = 0;
        this.root.setNode(index,newData);
    }
    public E get(int index) {
        if(index >= this.count){ // 超過了保存的個數
            return null;
        }
        this.foot = 0;
        return (E) this.root.getNode(index);
    }

    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(this.root,data);
            }
            this.count--;
        }
    }
}

 

2.建立寵物標准

 

// 定義寵物
interface Pet {
     public String getName();
     public String getColor();
     public int getAge();
}

 

3.對於寵物商店,只關注於寵物的標准,而不關心那種寵物

 

/**
 * @author Gu
 * @cracked 2020-03-25 9:10
 */
public class PetShop {
    private LinkImpl pets = new LinkImpl(); //開辟一個鏈表,保存多個寵物
    public void add(Pet pet) { // 上架上的寵物,
        this.pets.add(pet);
    }
    public void delete(Pet pet) {
        this.pets.remove(pet);
    }
    public LinkImpl getPets() { // 取得全部寵物
        return this.pets;
    }
    public LinkImpl search(String keyWord) {
        LinkImpl result = new LinkImpl(); // 保存查詢結果
        Object[] data = this.pets.toArray();
        for(int x = 0; x < data.length; x++) {
            Pet pet = (Pet)data[x];
            if(pet.getName().contains(keyWord) || pet.getColor().contains(keyWord)) {
                result.add(pet); // 滿足查詢結果
            }
        }
        return result;
    }
}

 

 

4.定義寵物狗:

 

public class Dog implements Pet {
    private String name;
    private int age;
    private String color;
    public Dog(String name,int age,String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }
    public String getName() {
        return this.name;
    }
    public int getAge() {
        return this.age;
    }
    public String getColor() {
        return this.color;
    }
    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.age+",顏色:"+this.color;
    }
}

 

5.定義寵物貓

 

public class Cat implements Pet {
    private String name;
    private int age;
    private String color;
    public Cat(String name,int age,String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }
    public String getName() {
        return this.name;
    }
    public int getAge() {
        return this.age;
    }
    public String getColor() {
        return this.color;
    }
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(this == obj) {
            return true;
        }
        if(!(obj instanceof Cat)) {
            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.age+",顏色:"+this.color;
    }
}

 

6.編寫測試程序

 

public class MainTest {
    public static void main(String[] args) {
        PetShop ps = new PetShop();

        // 添加寵物
        ps.add(new Dog("黑狗",1,"黑色"));
        ps.add(new Dog("金毛",2,"黃色"));
        ps.add(new Dog("臘腸",1,"金色"));
        ps.add(new Dog("拉布拉多",1,"金色"));
        ps.add(new Cat("加菲貓",1,"金色"));
        ps.add(new Cat("波斯貓",1,"白色"));

        // 刪除寵物
        ps.delete(new Dog("臘腸",1,"金色"));

        LinkImpl all = ps.search("金");

        Object[] data = all.toArray();
        for(int x = 0; x < data.length; x++) {
            System.out.println(data[x]);
        }
    }
}

7.測試結果

 

實際之中這種形式的代碼在生活中處處可見

  一個公園可以有多種植物

  動物園可以有多種動物;

  一個衣櫃可以有多件衣服;

  一個人可以吃多種食物;

總結:

  在以后的代碼開發過程之中一切都要以接口設計為主。

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM