Java-面向对象实例--宠物商店

面向对象实例分析—宠物商店
设计一个宠物商店,在商店中可以有多种宠物,试表示出此种关系,并要求可以根据关键字查找相应的宠物信息。所需要的宠物信息自行设定。
(1).简单设计出名字、颜色、年龄属性;
(2).宠物种类多,所以宠物应该是一个标准;
(3).在在宠物店中,只要是符合宠物标准的都应该可以放进宠物店中
(4).宠物店中存在多种宠物,则肯定是宠物的对象数组,臭无个数有用户来决定。
宠物接口:

interface Pet{  // 定义宠物接口
    public String getName() ;
    public String getColor() ;
    public int getAge() ;
}

宠物猫:

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 void setName(String name){
        this.name = name ;
    }
    public void setColor(String color){
        this.color = color;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public String getColor(){
        return this.color ;
    }
    public int getAge(){
        return this.age ;
    }
};

宠物狗:

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 void setName(String name){
        this.name = name ;
    }
    public void setColor(String color){
        this.color = color;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public String getColor(){
        return this.color ;
    }
    public int getAge(){
        return this.age ;
    }
};

上面给出了两种动物,其他动物可以实现pet接口即可。
宠物商店:

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].getName().indexOf(keyWord)!=-1
                    ||this.pets[i].getColor().indexOf(keyWord)!=-1){
                    p[f] = this.pets[i] ;
                    f++ ;
                }
            }
        }
        return p ;

    }
};

测试宠物商店:

public class PetShopDemo{
    public static void main(String args[]){
        PetShop ps = new PetShop(5) ;   // 五个宠物
        ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功
        ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功
        ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功
        ps.add(new Dog("拉步拉多","黄色的",3)) ;   // 增加宠物,成功
        ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功
        ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败
        print(ps.search("黑")) ;
    }
    public static void print(Pet p[]){
        for(int i=0;i<p.length;i++){
            if(p[i]!=null){
                System.out.println(p[i].getName() + "," + p[i].getColor()
                    +"," + p[i].getAge()) ;
            }
        }
    }
};

运行结果为:

黑猫,黑色的,3

以上程序只能存放5中宠物,所以添加第六种时无法添加。

  • 10
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

aotulive

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

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

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

打赏作者

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

抵扣说明:

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

余额充值