Java實現寵物商店管理
阿新 • • 發佈:2020-10-30
本文例項為大家分享了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,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,年齡 " + 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,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,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(); } } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。