首页 > 分享 > Java宠物店程序

Java宠物店程序

最新推荐文章于 2023-09-07 16:46:51 发布

weixin_42041289 于 2019-05-07 14:07:13 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

Java 宠物店程序

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 ; } }; 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()) ; } } } };

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131

相关知识

java计算机毕业设计宠物店管理系统(开题+程序+论文)
java计算机毕业设计网上宠物店(开题+程序+论文)
Java宠物店程序
java计算机毕业设计宠物店销售管理系统(开题+程序+论文)
java欢迎来到宠物店,用JAVA语言实现简易的宠物店程序暨寒假集训心得
java程序SQl
java毕业设计宠物民宿小程序
java乐多多宠物店网站(ssm)
Java基于宠物店管理(开题+源码)
java计算机毕业设计宠物寄养平台(开题+程序+论文)

网址: Java宠物店程序 https://m.mcbbbk.com/newsview1124480.html

所属分类:萌宠日常
上一篇: 2023年宠物概念受益的股票有哪
下一篇: 宠物经济概念上市公司:关注三只龙