编写一个宠物商店程序,要求如下:
假设你拥有一家宠物商店,该店能够寄样一批宠物(用数组存储,用常量设置数组最大值,如10),通过程序实现该宠物商店的宠物管理。程序实现具体宠物寄养功能(寄样功能可理解为宠物对象创建,如:用户输入1,表示寄样一只狗;输入2,表示寄样一只猫;输入3,表示创建一只鸟),并在寄样时对该宠物信息登记并命名,最后在内存中保存这些创建的宠物。程序提供针对名称的检索功能,即根据用户提供的名称在寄样的宠物中查找宠物并输出该宠物的类型及创建序号。程序提供对宠物的信息输出功能(信息输出,如:喂养序号为1,名称为大黄的宠物狗,可以输出“1 狗 大黄”),可根据类型输出所有类型相同的宠物;并可根据寄样序号,输出该序号之前所有当前在店中寄样的宠物。构建所有宠物的父类:Pet,该类中定义宠物的基本属性及方法。构建基本宠物类:狗(Dog)、猫(Cat)、鸟(Bird)等。可通过多态实现对任意宠物的喂养。定义静态方法,该方法可以对传入的对象实例进行判断,并输出该对象实例的类型。创建“领走宠物”类,即寄样时间到达后,用户可以领走自己所寄养的宠物。构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用户的操作请求。domain包主要实现各种对各种数据对象的封装,例如Pet,Dog,Bird,Cat等等
Petpackage animalstore.domain; abstract public class Pet {protected String name;public Pet(String name) {super();this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public abstract void feed();public String getType() {String[] info = this.getClass().getTypeName().split("[.]");return info[info.length-1];} }
1234567891011121314151617181920212223242526 Dogpackage animalstore.domain; public class Dog extends Pet {public Dog(String name) {super(name);}@Overridepublic void feed() {System.out.println("feedForDogInstruction:狗吃骨头");} } 12345678910111213 Cat
package animalstore.domain; public class Cat extends Pet{public Cat(String name) {super(name);}@Overridepublic void feed() {System.out.println("feedForCatInstruction:猫吃鱼");} } 1234567891011121314 Bird
package animalstore.domain; public class Bird extends Pet {public Bird(String name) {super(name);}@Overridepublic void feed() {System.out.println("feedForBirdInstruction:鸟吃虫");} } 123456789101112
service包主要实现对数据对象的操作,例如添加,删除,查询等等操作
PetStoreServicepackage animalstore.service; import java.util.Scanner; import animalstore.domain.Bird; import animalstore.domain.Cat; import animalstore.domain.Dog; import animalstore.domain.Pet; public class PetStoreService {private final int MAX_FOSTER_PET = 10;// 寄养宠物最大值(商店的最大承载量)private Pet[] fosterPets = new Pet[MAX_FOSTER_PET];// 寄养宠物以数组形式存储private int total = 0;// 寄养宠物实际数量private int[] timeForFosterPets = new int[MAX_FOSTER_PET];private Scanner scan = new Scanner(System.in);public PetStoreService() {super();String[][] PETS = PetsData.PETS;int[] TIME = PetsData.TAKE_AWAY_TIME;for (int i = 0; i < PETS.length; i++) {timeForFosterPets[i] = TIME[i];if ("Dog".equalsIgnoreCase(PETS[i][1])) {fosterPets[i] = new Dog(PETS[i][0]);} else if ("Cat".equalsIgnoreCase(PETS[i][1])) {fosterPets[i] = new Cat(PETS[i][0]);} else if ("Bird".equalsIgnoreCase(PETS[i][1])) {fosterPets[i] = new Bird(PETS[i][0]);}total++;}}/** * @Description 实现具体宠物的寄养功能,寄养功能理解为宠物对象的创建(1-Dog, 2-Cat, 3-Bird) * @apiNote 寄养宠物的同时对宠物信息登记并且命名 * @apiNote 寄养id即为宠物在对应商店的index + 1 * @author Nasir * @throws PetFosterException * @date 2022年4月27日下午3:45:14 */public void foster() throws PetFosterException {System.out.println("n----------宠物寄养----------n");if (total >= MAX_FOSTER_PET) {throw new PetFosterException("宠物商店寄养负载,宠物寄养失败");}System.out.print("请选择寄养动物(1-Dog, 2-Cat, 3-Bird):");int petType = scan.nextInt();if (!(petType == 1 || petType == 2 || petType == 3)) {throw new PetFosterException("输入错误<只能输入1,2,3>, 宠物添加失败");}System.out.print("请输入宠物姓名:");String petName = scan.next();System.out.print("请输入寄养时间(DAY):");int days = scan.nextInt();timeForFosterPets[total] = days;switch (petType) {case 1:fosterPets[total++] = new Dog(petName);break;case 2:fosterPets[total++] = new Cat(petName);break;case 3:fosterPets[total++] = new Bird(petName);break;}System.out.println("n----------添加成功----------n");}/** * @Description 宠物被人领走(根据宠物id检索) * @author Nasir * @throws PetFosterException * @date 2022年4月27日下午4:03:23 */public void takeAway() throws PetFosterException {System.out.println("n----------领走宠物----------n");System.out.print("请输入要领走宠物的ID:");int id = scan.nextInt();if (id <= 0 || id > total)throw new PetFosterException("序号输入错误");System.out.print("你的宠物最晚可以" + timeForFosterPets[id - 1] + "天后取回,确认(Y/N):");String isTake = scan.next();if ('N' == isTake.toUpperCase().charAt(0)) {return;}System.out.println("领走宠物:" + fosterPets[id - 1].getName());for (int i = id - 1; i < total - 1; i++) {fosterPets[i] = fosterPets[i + 1];}fosterPets[--total] = null;System.out.println("n----------领走成功----------n");}/** * @Description 基于名称的检索 输出相应的类型和序号 * @author Nasir * @throws PetFosterException * @date 2022年4月27日下午7:15:46 */public void search() throws PetFosterException {System.out.println("n----------宠物查询----------n");System.out.println("输入查询宠物的名称");String name = scan.next();int count = 0;for (int i = 0; i < total; i++) {if (name.equalsIgnoreCase(fosterPets[i].getName()))count++;}if (count == 0)throw new PetFosterException("无查询结果,请检查输入");System.out.println("查询结果为");for (int i = 0; i < total; i++) {if (name.equalsIgnoreCase(fosterPets[i].getName()))System.out.println("类型:" + fosterPets[i].getType() + ", 序号:" + (i + 1));}System.out.println("n----------查询成功----------n");}/** * @Description 输出寄养宠物的列表 * @author Nasir * @throws PetFosterException * @date 2022年4月27日下午4:04:27 */public void list() throws PetFosterException {System.out.println("n----------宠物列表----------n");if (total == 0) {throw new PetFosterException("没有宠物在寄养中···");}System.out.println("序号t宠物类型t宠物姓名");for (int i = 0; i < total; i++) {System.out.println((i + 1) + "t" + fosterPets[i].getType() + "t" + fosterPets[i].getName());}System.out.println("n--------------------------n");}/** * @Description 输出特定类型的寄养宠物的列表 * @author Nasir * @date 2022年4月27日下午4:05:59 * @param petType * @throws PetFosterException */public void list(String petType) throws PetFosterException {String type = "";if ("Dog".equalsIgnoreCase(petType))type = "Dog";else if ("Cat".equalsIgnoreCase(petType))type = "Cat";else if ("Bird".equalsIgnoreCase(petType))type = "Bird";elsethrow new PetFosterException("打印错误,宠物类型输入错误");System.out.println("n--------宠物列表(" + type + ")--------n");int count = 0;for (int i = 0; i < total; i++) {if (type.equalsIgnoreCase(fosterPets[i].getType())) {count++;}}if (count == 0)throw new PetFosterException("没有" + type + "在寄养中");System.out.println("序号t宠物类型t宠物姓名");for (int i = 0; i < total; i++) {if (type.equalsIgnoreCase(fosterPets[i].getType())) {System.out.println((i + 1) + "t" + fosterPets[i].getType() + "t" + fosterPets[i].getName());}}System.out.println("n--------------------------n");}/** * @Description 输出特定序号及之前的寄养宠物列表 * @author Nasir * @date 2022年4月27日下午4:07:28 * @param endId * @throws PetFosterException */public void list(int endId) throws PetFosterException {System.out.println("n----------宠物列表----------n");if (endId <= 0 || endId > 10)throw new PetFosterException("输入序号错误,范围应在[1,10]");if (endId > total)throw new PetFosterException("输入序号不存在,寄养宠物最大序号为:" + total);System.out.println("序号t宠物类型t宠物姓名");for (int i = 0; i < endId; i++) {System.out.println((i + 1) + "t" + fosterPets[i].getType() + "t" + fosterPets[i].getName());}System.out.println("n--------------------------n");}/** * @Description 返回传入宠物的类型 * @author Nasir * @date 2022年4月27日下午7:09:26 * @param pet * @return */public String typeReturn(Pet pet) {if (pet instanceof Dog)return "Dog";else if (pet instanceof Cat)return "Cat";else if (pet instanceof Bird)return "Bird";elsereturn "Pet";} }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 PetsDatapackage animalstore.service; public class PetsData {public static final String[][] PETS = { { "Karen", "Dog" }, { "Lucky", "Cat" }, { "emo", "Bird" } };public static final int[] TAKE_AWAY_TIME = {2,8,3}; } 1234567 PetFosterException
package animalstore.service; public class PetFosterException extends Exception{/** * serialVersionUID */private static final long serialVersionUID = -425116014159456908L;public PetFosterException() {super();}public PetFosterException(String msg) {super(msg);} }
1234567891011121314151617181920view包主要实现在控制台层面的数据交互
PetStoreViewpackage animalstore.view; import java.util.Scanner; import animalstore.service.PetFosterException; import animalstore.service.PetStoreService; public class PetStoreView {private PetStoreService pss = new PetStoreService();private Scanner scan = new Scanner(System.in);public void enterMenu() {boolean isLoop = true;while (isLoop) {try {pss.list();} catch (PetFosterException e) {System.out.println(e.getMessage());}System.out.print("1-寄养宠物2-取走宠物3-查询宠物4-宠物列表(一类)5-宠物列表(指定序号)6-退出");int choice = scan.nextInt();if (choice < 1 || choice > 5) {System.out.println("输入错误,请重新输入");continue;}switch (choice) {case 1:try {pss.foster();} catch (PetFosterException e) {System.out.println(e.getMessage());}break;case 2:try {pss.takeAway();} catch (PetFosterException e) {System.out.println(e.getMessage());}break;case 3:try {pss.search();} catch (PetFosterException e) {System.out.println(e.getMessage());}break;case 4:System.out.print("请输入待查询宠物的类型:");String type = scan.next();try {pss.list(type);} catch (PetFosterException e) {System.out.println(e.getMessage());}break;case 5:System.out.print("请输入最后一个要查询的宠物的序号:");int id = scan.nextInt();try {pss.list(id);} catch (PetFosterException e) {System.out.println(e.getMessage());}break;case 6:System.out.println("确认是否退出?(Y/N)");String isExit = scan.next();if('Y' == isExit.toUpperCase().charAt(0)) {isLoop = false;}}}}public static void main(String[] args) {PetStoreView psv = new PetStoreView();psv.enterMenu();} }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485相关知识
编写Java程序,以继承和多态思想模拟饲养员喂养不同动物的不同行为
Java面向对象
java宠物商店项目
java宠物商店代码
Java宠物商店项目案例分析与实践
Java面向对象编程(宠物商店)
Java+MySQL宠物商店系统设计与实现
java面向对象基础案例
宠物商店详细设计说明书
宠物商店电子商务网站可行性研究报告
网址: Java的面向对象特性练习题:编写一个宠物商店程序 https://m.mcbbbk.com/newsview581316.html
上一篇: 宠物鸟龟寄养指南,哪里可以找到可 |
下一篇: 北京竟然有鸟咖啦…想rua鸟的快 |