Java Animal Reserve项目功能实现及代码bug排查求助
Java Animal Reserve项目问题修复方案
核心问题修复思路
- 对象输出仅显示内存地址:为
Dog、Monkey实体类重写toString()方法,自定义需要打印的属性即可解决 intakeNewMonkey方法缺失逻辑:补充物种合法性校验、Monkey实例化、实例加入monkeyList的逻辑,同时修复nextBoolean()执行后换行符残留导致后续输入读取为空的问题reserveAnimal方法逻辑错误:补充服务国家筛选逻辑,删除if判断后的多余分号修复筛选失效问题,调整狗列表筛选逻辑为输出未预留的可预约动物printAnimals方法未实现:补充方法入参listType,按需求实现三个输出分支,其中available分支合并输出两类动物中已完成训练且未被预留的个体
修复后代码示例
实体类toString方法示例(Monkey类逻辑一致)
// 放在Dog类内部 @Override public String toString() { return "Dog{" + "名称='" + name + '\'' + ", 训练状态='" + trainingStatus + '\'' + ", 采集国家='" + acquisitionCountry + '\'' + ", 是否预留=" + reserved + '}'; }
intakeNewMonkey方法
public static void intakeNewMonkey(Scanner scanner) { // 定义允许录入的物种列表,可根据需求调整 List<String> allowedSpecies = Arrays.asList("猕猴", "金丝猴", "黑猩猩", "长臂猿"); System.out.println("What is the new monkey's name?"); String name = scanner.nextLine(); for (Monkey monkey: monkeyList) { if(monkey.getName().equalsIgnoreCase(name)) { System.out.println("\n\nThis monkey is already in our system.\n\n"); return; } } System.out.println("What is the monkey's tail length?"); String tailLength = scanner.nextLine(); System.out.println("What is the monkey's gender?"); String gender = scanner.nextLine(); System.out.println("What is the monkey's age?"); String age = scanner.nextLine(); System.out.println("What is the monkey's weight?"); String weight = scanner.nextLine(); System.out.println("What is the monkey's acquisition date?"); String acquisitionDate = scanner.nextLine(); System.out.println("What is the monkey's acquisition country?"); String acquisitionCountry = scanner.nextLine(); System.out.println("What is the monkey's training status?"); String trainingStatus = scanner.nextLine(); System.out.println("Is the monkey reserved? <enter true or false>"); boolean reservedBoolean = scanner.nextBoolean(); // 吃掉nextBoolean残留的换行符,避免后续nextLine读取为空 scanner.nextLine(); System.out.println("What is the monkey's service country?"); String serviceCountry = scanner.nextLine(); System.out.println("What is the monkey's body length?"); String bodyLength = scanner.nextLine(); System.out.println("What is the monkey's height?"); String height = scanner.nextLine(); System.out.println("What is the monkey's species?"); String species = scanner.nextLine(); // 物种合法性校验 if (!allowedSpecies.contains(species)) { System.out.println("\n\n不允许录入该物种的猴子\n\n"); return; } // 实例化Monkey,构造方法参数根据实际定义调整 Monkey newMonkey = new Monkey(name, tailLength, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reservedBoolean, serviceCountry, bodyLength, height, species); // 加入列表 monkeyList.add(newMonkey); System.out.println("猴子录入成功"); }
reserveAnimal方法
public static void reserveAnimal(Scanner scanner) { System.out.println("Enter animal type"); String type = scanner.nextLine(); System.out.println("Enter service country"); String serviceCountry = scanner.nextLine(); if (type.equalsIgnoreCase("Monkey")) { System.out.println("当前可预约的猴子列表:"); for (int i = 0; i < monkeyList.size(); i++) { Monkey monkey = monkeyList.get(i); // 筛选未预留且服务国家匹配的个体 if (!monkey.getReserved() && monkey.getServiceCountry().equalsIgnoreCase(serviceCountry)) { System.out.println(monkey); } } System.out.println("Enter name: "); String name = scanner.nextLine(); for (Monkey obj: monkeyList) { if (obj.getName().equalsIgnoreCase(name)) { obj.setReserved(true); System.out.println("猴子预约成功"); return; } } System.out.println("Monkey not found in list"); } if (type.equalsIgnoreCase("Dog")) { System.out.println("当前可预约的狗列表:"); for (int i = 0; i < dogList.size(); i++) { Dog dog = dogList.get(i); // 筛选未预留且服务国家匹配的个体 if (!dog.getReserved() && dog.getServiceCountry().equalsIgnoreCase(serviceCountry)) { System.out.println(dog); } } System.out.println("Enter name: "); String name = scanner.nextLine(); for (Dog obj: dogList) { if (obj.getName().equalsIgnoreCase(name)) { obj.setReserved(true); System.out.println("狗预约成功"); return; } } System.out.println("Dog not found in list"); } }
printAnimals方法
// 补充listType入参,由菜单调用时传入 public static void printAnimals(String listType) { if ("dog".equalsIgnoreCase(listType)) { System.out.println("===== 狗列表 ====="); for (Dog dog : dogList) { System.out.println(dog); } } else if ("monkey".equalsIgnoreCase(listType)) { System.out.println("===== 猴子列表 ====="); for (Monkey monkey : monkeyList) { System.out.println(monkey); } } else if ("available".equalsIgnoreCase(listType)) { System.out.println("===== 可预约动物列表(已完成训练且未预留) ====="); // 输出符合条件的狗 for (Dog dog : dogList) { if ("in service".equalsIgnoreCase(dog.getTrainingStatus()) && !dog.getReserved()) { System.out.println(dog); } } // 输出符合条件的猴子 for (Monkey monkey : monkeyList) { if ("in service".equalsIgnoreCase(monkey.getTrainingStatus()) && !monkey.getReserved()) { System.out.println(monkey); } } } else { System.out.println("无效的列表类型"); } }
内容的提问来源于stack exchange,提问作者Kolgrim17
相关产品推荐
相关产品推荐

