宠物商店

目录

 

一 、“宠物商店” 实例要求 

       设计一个“宠物商店”,在商店中可以由多种宠物(商店的宠物数量由用户决定),试表示出此种关系,并要求可以根据宠物的关键字查询xiang到相应的宠物信息,所需要的宠物信息自行设计

二 、分步骤实现过程

1 、定义宠物标准接口

2 、实现接口,定义具体的宠物种类

3 、定义宠物商店的操作类

4 、编写主方法进行测试

完整代码及运行结果


 

一 、“宠物商店” 实例要求 

设计一个“宠物商店”,在商店中可以由多种宠物(商店的宠物数量由用户决定),试表示出此种关系,并要求可以根据宠物的关键字查询xiang到相应的宠物信息,所需要的宠物信息自行设计

 具体分析:

(1)、简单设计出名字 、颜色 、年龄宠物信息属性

(2)、符合宠物商店标准的均可被放进商店。可定义一个宠物接口,制定宠物标准

(3)、宠物商店要保存多种宠物,由对象数组存储宠物信息,商店宠物数量由用户决定,那么在创建商店时,就分配好宠物数量,开辟相应的内存空间

 

二 、分步骤实现过程

1 、定义宠物标准接口

interface Pet{   //定义宠物接口
    public String getName(); //取得宠物姓名抽象方法
    public String getColor(); //取得宠物毛色抽象方法
    public int getAge(); //取得宠物年龄抽象方法
}

2 、实现接口,定义具体的宠物种类

定义宠物猫实现宠物接口:

class Cat implements Pet{  //宠物猫
    private String name;   //宠物名字
    private String color;  //宠物毛色
    private int age;       //宠物年龄

    public Cat(String name, String color, int age) { //通过构造设置属性
        this.name = name;
        this.color = color;
        this.age = age;
    }

    @Override
    public String getName() { //Getter方法,同时覆写接口Pet的抽象方法
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getColor() { //Getter方法,同时覆写接口Pet的抽象方法
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public int getAge() { //Getter方法,同时覆写接口Pet的抽象方法
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

定义宠物狗实现宠物接口:

class Dog implements Pet{  //宠物狗
    private String name;
    private String color;
    private int age;

    public Dog(String name, String color, int age) {
        this.name = name;
        this.color = color;
        this.age = age;
    }

    @Override
    public String getName() { //Getter方法,同时覆写接口Pet的抽象方法
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getColor() { //Getter方法,同时覆写接口Pet的抽象方法
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public int getAge() { //Getter方法,同时覆写接口Pet的抽象方法
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3 、定义宠物商店的操作类

在宠物商店中包含增加宠物方法,根据关键字查询宠物信息方法

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;  //返回对象数组
    }
}

4 、编写主方法进行测试

public class PetShopDemo {
    public static void main(String[] args) {
        PetShop ps = new PetShop(5);  //确定宠物商店的宠物数量,为5只
        ps.add(new Cat("橘猫","黄色",2)); //增加宠物,成功
        ps.add(new Cat("花猫","花色",3)); //增加宠物,成功
        ps.add(new Cat("黑猫","黑色",1)); //增加宠物,成功
        ps.add(new Dog("拉布拉多","米黄色",2)); //增加宠物,成功
        ps.add(new Dog("金毛","深黄色",3)); //增加宠物,成功
        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());
            }
        }
    }
}

完整代码及运行结果

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.name = name;
        this.color = color;
        this.age = age;
    }

    @Override
    public String getName() { //Getter方法,同时覆写接口Pet的抽象方法
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getColor() { //Getter方法,同时覆写接口Pet的抽象方法
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public int getAge() { //Getter方法,同时覆写接口Pet的抽象方法
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Dog implements Pet{  //宠物狗
    private String name;
    private String color;
    private int age;

    public Dog(String name, String color, int age) {
        this.name = name;
        this.color = color;
        this.age = age;
    }

    @Override
    public String getName() { //Getter方法,同时覆写接口Pet的抽象方法
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getColor() { //Getter方法,同时覆写接口Pet的抽象方法
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public int getAge() { //Getter方法,同时覆写接口Pet的抽象方法
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

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);  //确定宠物商店的宠物数量,为5只
        ps.add(new Cat("橘猫","黄色",2)); //增加宠物,成功
        ps.add(new Cat("花猫","花色",3)); //增加宠物,成功
        ps.add(new Cat("黑猫","黑色",1)); //增加宠物,成功
        ps.add(new Dog("拉布拉多","米黄色",2)); //增加宠物,成功
        ps.add(new Dog("金毛","深黄色",3)); //增加宠物,成功
        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());
            }
        }
    }
}

程序运行结果:

  • 9
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值