- 题目描述
- 代码构成(分三个包)
- package animalstore.domain;
- Pet
- Dog
- Cat
- Bird
- package animalstore.service;
- PetStoreService
- PetsData
- PetFosterException
- package animalstore.view;
- PetStoreView
编写一个宠物商店程序,要求如下:
- 假设你拥有一家宠物商店,该店能够寄样一批宠物(用数组存储,用常量设置数组最大值,如10),通过程序实现该宠物商店的宠物管理。
- 程序实现具体宠物寄养功能(寄样功能可理解为宠物对象创建,如:用户输入1,表示寄样一只狗;输入2,表示寄样一只猫;输入3,表示创建一只鸟),并在寄样时对该宠物信息登记并命名,最后在内存中保存这些创建的宠物。
- 程序提供针对名称的检索功能,即根据用户提供的名称在寄样的宠物中查找宠物并输出该宠物的类型及创建序号。
- 程序提供对宠物的信息输出功能(信息输出,如:喂养序号为1,名称为大黄的宠物狗,可以输出“1 狗 大黄”),可根据类型输出所有类型相同的宠物;并可根据寄样序号,输出该序号之前所有当前在店中寄样的宠物。
- 构建所有宠物的父类:Pet,该类中定义宠物的基本属性及方法。
- 构建基本宠物类:狗(Dog)、猫(Cat)、鸟(Bird)等。
- 可通过多态实现对任意宠物的喂养。
- 定义静态方法,该方法可以对传入的对象实例进行判断,并输出该对象实例的类型。
- 创建“领走宠物”类,即寄样时间到达后,用户可以领走自己所寄养的宠物。
- 构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用户的操作请求。
程序应具有良好的类层次结构,良好的人机交互性能,即:程序应向用户提示功能说明,并可根据用户的功能选择,执行对应的功能,并给出带详细描述信息的最终执行结果。
Petdomain包主要实现各种对各种数据对象的封装,例如Pet,Dog,Bird,Cat等等
package 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];
}
}
Dog
package animalstore.domain;
public class Dog extends Pet {
public Dog(String name) {
super(name);
}
@Override
public void feed() {
System.out.println("feedForDogInstruction:狗吃骨头");
}
}
Cat
package animalstore.domain;
public class Cat extends Pet{
public Cat(String name) {
super(name);
}
@Override
public void feed() {
System.out.println("feedForCatInstruction:猫吃鱼");
}
}
Bird
package animalstore.domain;
public class Bird extends Pet {
public Bird(String name) {
super(name);
}
@Override
public void feed() {
System.out.println("feedForBirdInstruction:鸟吃虫");
}
}
package animalstore.service;
PetStoreServiceservice包主要实现对数据对象的操作,例如添加,删除,查询等等操作
package 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++;
}
}
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");
}
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");
}
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");
}
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");
}
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";
else
throw 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");
}
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");
}
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";
else
return "Pet";
}
}
PetsData
package 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};
}
PetFosterException
package animalstore.service;
public class PetFosterException extends Exception{
private static final long serialVersionUID = -425116014159456908L;
public PetFosterException() {
super();
}
public PetFosterException(String msg) {
super(msg);
}
}
package animalstore.view;
PetStoreViewview包主要实现在控制台层面的数据交互
package 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();
}
}



