(Java)实例分析 -- 宠物商店

一、实例分析

设计一个宠物商店,宠物商店中可以有多种宠物,试表示出此种关系,要求可以根据宠物的关键字查找到相应的宠物信息。
具体分析如下:
(1)要求提示宠物的信息可以自行设计,可以简单设计出名字、颜色、年龄三个属性
(2)宠物的类别很多,如猫、狗等都属于宠物,所以宠物应该是一个标准
(3)在宠物商店中,只要是符合了此宠物标准的就都应该放进宠物商店中
(4)宠物商店需要保存多种宠物,则应该是一个宠物的对象数组,如果宠物的个数由用户决定,则应该在创建宠物商店时,就已经分配好宠物的个数。

分析图
在这里插入图片描述
可以看出宠物商店不管具体的宠物是哪一个,只要是宠物就可以放进去,所以此宠物的标准应该使用接口进行定义,每个具体的宠物都实现此接口,宠物商店与接口有关。

二、代码示例

(1)Pet.java

interface Pet{//宠物接口
    public String getName();//得到宠物的名字
    public String getColor();//得到宠物的颜色
    public int getAge();//得到宠物的年龄
}

(2)Cat.java

class Cat implements Pet{//宠物猫
    private String name;
    private String color;
    private int age;
    public Cat(String name,String color,int age){//通过构造设置属性
        this.setName(name);
        this.setColor(color);
        this.setAge(age);
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getColor(){
        return this.color;
    }
    public void setColor(String color){
        this.color = color;
    }
    public int getAge(){
        return this.age;
    }
    public void setAge(int age){
        this.age = age;
    }
}

(3)Dog.java

class Dog implements Pet{//宠物狗
    private String name;
    private String color;
    private int age;
    public Dog(String name,String color,int age){//通过构造设置属性
        this.setName(name);
        this.setColor(color);
        this.setAge(age);
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getColor(){
        return this.color;
    }
    public void setColor(String color){
        this.color = color;
    }
    public int getAge(){
        return this.age;
    }
    public void setAge(int age){
        this.age = age;
    }
}

(4)PetShop.java

class PetShop{
    private Pet[] pets;//保存多个属性
    private int foot;//数据保存位置
    public PetShop(int len){
        //构造方法开辟宠物数组的大小
        if(len>0){
            this.pets = new Pet[len];
        }else this.pets = new Pet[1];//至少开辟一个空间
    }
    public boolean add(Pet pet){//增加宠物
        if(this.foot<this.pets.length){//判断宠物商店里的宠物是否已经满了
            this.pets[this.foot] = pet;//增加宠物
            this.foot++;//修改保存位置
            return true;//增加成功
        }else{
            return false;//增加失败
        }
    }
    public Pet[] search(String keyWord){// 关键字查找
        Pet p[] = null;//为查找之后的结果,此处不是固定的
        int count = 0;//记录下多少个宠物符合查询结果
        //确认开辟的空间大小,看有多少个宠物符合查询条件
        for(int i=0;i<this.pets.length;i++){
            if(this.pets[i] != null){//判断对象数组中的内容是否为空
                if(this.pets[i].getName().indexOf(keyWord) !=-1
                || this.pets[i].getColor().indexOf(keyWord)!=-1){
                    count++;//统计符合条件的宠物个数
                }
            }
        }
        p = new Pet[count];//根据已经确定的记录数开辟对象数组
        int f =0;//设置增加的位置标记
        for(int i=0;i<this.pets.length;i++){
            if(this.pets[i] != null)
                if(this.pets[i] != null){
                    if(this.pets[i].getName().indexOf(keyWord)!=-1
                    || this.pets[i].getColor().indexOf(keyWord)!=-1){
                        p[f] = this.pets[i];//将符合查询条件的宠物信息保存
                        f++;
                    }
                }
        }
        return p;
    }
}

(5)PetShopDemo.java

public class PetShopDemo {
    public static void main(String[] args) {
        PetShop ps = new PetShop(5);//5个宠物
        ps.add(new Cat("white cat","white",2));//增加宠物
        ps.add(new Cat("green cat","green",3));//增加宠物
        ps.add(new Cat("black cat","black",4));//增加宠物
        ps.add(new Dog("white dog","white",2));//增加宠物
        ps.add(new Dog("yellow dog","yellow",3));//增加宠物
        ps.add(new Dog("red dog","red",4));//增加宠物
        print(ps.search("white"));
    }
    public static void print(Pet p[]){//输出操作
        for(int i=0;i<p.length;i++){//循环输出
            System.out.println(p[i].getName()+","+p[i].getColor()+","+p[i].getAge());
        }
    }
}

在这里插入图片描述

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南淮北安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值