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.测试结果

实际之中这种形式的代码在生活中处处可见

  一个公园可以有多种植物

  动物园可以有多种动物;

  一个衣柜可以有多件衣服;

  一个人可以吃多种食物;

总结:

  在以后的代码开发过程之中一切都要以接口设计为主。

  

原文地址:https://www.cnblogs.com/Tony98/p/10451479.html