您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Java实现一个宠物商店管理?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
第一种实现方式:抽象类和对象数组
public abstract class AbstractPet //定义宠物模板 { private String name; //名称 private String color; //颜色 private int age; //年龄 public AbstractPet(){} public AbstractPet(String name, String color, int age){ this.setName(name); this.setColor(color); this.setAge(age); } public String getName(){ return this.name; } public String getColor(){ return this.color; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setColor(String color){ this.color = color; } public void setAge(int age){ if (age > 0) { this.age = age; }else{ this.age = 1; } } //定义抽象方法 public abstract void printInfo(); //自我介绍 }
public class Dog extends AbstractPet { public Dog(String name, String color, int age){ super(name, color, age); } //实现抽象方法 public void printInfo(){ //自我介绍 System.out.println("狗: " + super.getName() + ",年龄 " + super.getAge() + "岁,颜色:" + super.getColor()); } }
public class Cat extends AbstractPet { public Cat(String name, String color, int age){ super(name, color, age); } //实现抽象方法 public void printInfo(){ //自我介绍 System.out.println("狗: " + super.getName() + ",年龄 " + super.getAge() + "岁,颜色:" + super.getColor()); } }
public class PetShop { private AbstractPet[] pets; private int foot; //定义下标 public PetShop(int len){ //宠物数量由用户确定 if (len > 0) { this.pets = new AbstractPet[len]; }else{ this.pets = new AbstractPet[1]; } } //添加宠物的方法 public boolean add(AbstractPet pet){ if (this.foot < this.pets.length) { this.pets[foot] = pet; this.foot ++; return true; }else{ return false; } } //定义查询宠物的方法[提供按照种类查询和按照姓名查询两种方法] public AbstractPet[] search(String keyword){ int n = 0; //定义查询到的宠物数量 AbstractPet[] searchPets = null; 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) { n++; } } } searchPets = new AbstractPet[n]; n = 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) { searchPets[n] = this.pets[i]; n ++; } } } return searchPets; } }
public class testPetShop { public static void main(String[] args){ PetShop p = new PetShop(5); p.add(new Dog("狗1", "黑色的", 3)); p.add(new Dog("狗2", "红色的", 2)); p.add(new Cat("猫1", "褐色的", 3)); p.add(new Cat("猫2", "黄色的", 3)); p.add(new Cat("猫3", "黑色的", 5)); p.add(new Dog("狗3", "棕色的", 4)); print(p.search("黑")); } public static void print(AbstractPet pets[]){ for (int i = 0; i < pets.length; i++) { if (pets[i] != null) { pets[i].printInfo(); } } } }
第二种实现方式:接口和对象数组
interface IPet { String getName(); //取得宠物姓名 String getColor(); //取得宠物颜色 int getAge(); //取得宠物年龄 void show(); //显示宠物信息 }
public class Dog implements IPet { 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 String getColor(){ return this.color; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setColor(String color){ this.color = color; } public void setAge(int age){ if (age < 0 || age > 50) { this.age = 1; //默认值 }else{ this.age = age; } } public void show(){ System.out.println(this.toString()); } public String toString(){ return "狗:" + this.getName() + " " + this.getColor() + " " + this.getAge(); } }
public class Cat implements IPet { 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 String getColor(){ return this.color; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setColor(String color){ this.color = color; } public void setAge(int age){ if (age < 0 || age > 50) { this.age = 1; //默认值 }else{ this.age = age; } } public void show(){ System.out.println(this.toString()); } public String toString(){ return "猫:" + this.getName() + " " + this.getColor() + " " + this.getAge(); } }
public class PetShop { private IPet[] pets; private int foot; public PetShop(int len){ //宠物店的宠物数量由用户决定 if (len > 0) { pets = new IPet[len]; }else{ pets = new IPet[1]; //默认最小数量为1 } } public boolean add(IPet pet){ if (this.foot < this.pets.length) { this.pets[this.foot] = pet; this.foot ++; return true; }else{ return false; } } public IPet[] search(String keyword){ //定义一个新的宠物对象数组,用来存储符合查询条件的宠物 IPet[] resultPet = 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 ++; } } } resultPet = new IPet[count]; int n = 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) { resultPet[n] = this.pets[i]; n ++; } } } return resultPet; } }
public class TestPetShop { public static void main(String[] args){ //创建一个宠物商店 PetShop ps = new PetShop(7); //假设可以放置5只宠物 ps.add(new Dog("旺旺", "黑色的",4)); ps.add(new Dog("旺财", "白色的",6)); ps.add(new Dog("小黑", "黄色的",3)); ps.add(new Cat("波波", "褐色的",7)); ps.add(new Cat("咪咪", "黑色的",8)); ps.add(new Cat("小云", "灰色的",2)); ps.add(new Dog("仔仔", "黄色的",5)); print(ps.search("色")); } public static void print(IPet[] pet){ for (int i = 0; i < pet.length; i++) { if (pet[i] != null) { pet[i].show(); } } } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。