|
发表于 2025-3-19 19:59:14
|
显示全部楼层
本楼为最佳答案
1. Animal.java
- public class Animal {
- private String name;
- private String species;
- private int age;
-
- public Animal(String name, String species, int age) {
- this.name = name;
- this.species = species;
- this.age = age;
- }
-
- // Getters and setters
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getSpecies() {
- return species;
- }
-
- public void setSpecies(String species) {
- this.species = species;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- // Display animal information
- public void display() {
- System.out.println("- " + name + " (" + species + ", " + age + " years old)");
- }
- }
复制代码
2. Item.java
- public class Item {
- private String name;
- private String code;
- private double price;
-
- public Item(String name, String code) {
- this.name = name;
- this.code = code;
- this.price = 0.0;
- }
-
- // Getters and setters
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getCode() {
- return code;
- }
-
- public void setCode(String code) {
- this.code = code;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
- }
复制代码
3. Logistics.java
- public class Logistics {
- private Item vehicle;
- private Item fuel;
- private String[] caretakers;
-
- public Logistics(Item vehicle, Item fuel, String[] caretakers) {
- this.vehicle = vehicle;
- this.fuel = fuel;
- this.caretakers = caretakers;
- }
-
- // Display logistics information
- public void display(String sourceZoo, String destinationZoo, Animal animal) {
- System.out.println("\n----- LOGISTICS INFORMATION -----");
- System.out.println("Moving: " + animal.getName() + " (" + animal.getSpecies() + ")");
- System.out.println("From: " + sourceZoo);
- System.out.println("To: " + destinationZoo);
- System.out.println("Vehicle: " + vehicle.getName() + " (Code: " + vehicle.getCode() + ", Cost: RMB" + vehicle.getPrice() + ")");
- System.out.println("Fuel: " + fuel.getName() + " (Code: " + fuel.getCode() + ", Cost: RMB" + fuel.getPrice() + ")");
-
- System.out.println("Caretakers:");
- for (int i = 0; i < caretakers.length; i++) {
- System.out.println(" " + (i+1) + ". " + caretakers[i]);
- }
-
- double totalCost = vehicle.getPrice() + fuel.getPrice();
- System.out.println("Total Transportation Cost: RMB" + totalCost);
- System.out.println("----------------------------------");
- }
- }
复制代码
4. Zoo.java
5. ZooManagementSystem.java
- import java.util.Scanner;
- public class ZooManagementSystem {
- public static void main(String[] args) {
- // 创建用户输入的scanner
- Scanner input = new Scanner(System.in);
-
- // 创建两个动物园实例
- Zoo southernZone = new Zoo("Southern-Zone Zoo");
- Zoo northernZone = new Zoo("Northern-Zone Zoo");
-
- // 向Southern-Zone Zoo添加5只动物
- southernZone.addAnimal(new Animal("Simba", "African Lion", 6));
- southernZone.addAnimal(new Animal("Dumbo", "African Elephant", 12));
- southernZone.addAnimal(new Animal("Luna", "Gray Wolf", 4));
- southernZone.addAnimal(new Animal("Poe", "Raven", 3));
- southernZone.addAnimal(new Animal("Benny", "Grizzly Bear", 8));
-
- // 向Northern-Zone Zoo添加5只动物
- northernZone.addAnimal(new Animal("Arctic", "Polar Bear", 7));
- northernZone.addAnimal(new Animal("Blizzard", "Snow Leopard", 5));
- northernZone.addAnimal(new Animal("Frost", "Arctic Fox", 3));
- northernZone.addAnimal(new Animal("Penguin", "Emperor Penguin", 4));
- northernZone.addAnimal(new Animal("Aurora", "Caribou", 6));
-
- // 显示两个动物园中的所有动物
- southernZone.displayAnimals();
- northernZone.displayAnimals();
-
- // 动物园管理交互菜单
- boolean running = true;
- while (running) {
- // 显示菜单选项
- System.out.println("\n===== ZOO MANAGEMENT SYSTEM =====");
- System.out.println("1. Display all animals in Southern-Zone Zoo");
- System.out.println("2. Display all animals in Northern-Zone Zoo");
- System.out.println("3. Move an animal between zoos");
- System.out.println("4. Add a new animal to a zoo");
- System.out.println("5. Remove an animal from a zoo");
- System.out.println("6. Find an animal");
- System.out.println("0. Exit");
- System.out.print("Enter your choice: ");
-
- // 获取用户选择
- int choice = input.nextInt();
- input.nextLine(); // 清除缓冲区
-
- // 处理用户选择
- switch (choice) {
- case 1:
- southernZone.displayAnimals();
- break;
-
- case 2:
- northernZone.displayAnimals();
- break;
-
- case 3:
- // 在动物园之间移动动物
- System.out.println("\n----- MOVE ANIMAL BETWEEN ZOOS -----");
- System.out.println("1. Move from Southern-Zone to Northern-Zone");
- System.out.println("2. Move from Northern-Zone to Southern-Zone");
- System.out.print("Enter your choice: ");
-
- // 根据用户选择确定源和目标动物园
- int moveChoice = input.nextInt();
- input.nextLine(); // 清除缓冲区
-
- Zoo from = (moveChoice == 1) ? southernZone : northernZone;
- Zoo to = (moveChoice == 1) ? northernZone : southernZone;
-
- // 显示源动物园中可用的动物
- System.out.println("Available animals in " + from.getName() + ":");
- from.displayAnimals();
-
- // 检查源动物园是否有动物可移动
- if (from.getCounter() == 0) {
- System.out.println("No animals available to move.");
- break;
- }
-
- // 获取要移动的动物名称
- System.out.print("Enter the name of the animal to move: ");
- String animalName = input.nextLine();
-
- // 检查动物是否存在于源动物园
- int index = from.findAnimal(animalName);
- if (index != -1) {
- // 设置移动的物流
- System.out.println("\nSetting up logistics for the move...");
-
- // 创建车辆项目
- Item vehicle = new Item("Transport Truck", "VEH_001");
- System.out.print("Enter vehicle cost: RMB");
- vehicle.setPrice(input.nextDouble());
- input.nextLine(); // 清除缓冲区
-
- // 创建燃料项目
- Item fuel = new Item("Diesel Fuel", "FUEL_001");
- System.out.print("Enter fuel cost: RMB");
- fuel.setPrice(input.nextDouble());
- input.nextLine(); // 清除缓冲区
-
- // 设置照料者
- int numCaretakers;
- // 确保数字在1-3之间
- do {
- System.out.print("Enter the number of caretakers (between 1 and 3): ");
- numCaretakers = input.nextInt();
- input.nextLine(); // 清除缓冲区
- if (numCaretakers < 1 || numCaretakers > 3) {
- System.out.println("Invalid input! Please enter a number between 1 and 3.");
- }
- } while (numCaretakers < 1 || numCaretakers > 3);
- System.out.println("Number of caretakers set to: " + numCaretakers);
-
- // 获取照料者姓名
- String[] caretakers = new String[numCaretakers];
- for (int i = 0; i < numCaretakers; i++) {
- System.out.print("Enter name of caretaker " + (i+1) + ": ");
- caretakers[i] = input.nextLine();
- }
-
- // 创建带有所有组件的物流对象
- Logistics logistics = new Logistics(vehicle, fuel, caretakers);
-
- // 将动物从源动物园移动到目标动物园
- from.moveAnimal(animalName, to, logistics);
-
- // 显示更新后的动物园信息
- from.displayAnimals();
- to.displayAnimals();
- } else {
- System.out.println("--- Animal '" + animalName + "' not found in " + from.getName());
- }
- break;
-
- case 4:
- // 添加新动物
- System.out.println("\n----- ADD NEW ANIMAL -----");
- System.out.println("1. Add to Southern-Zone Zoo");
- System.out.println("2. Add to Northern-Zone Zoo");
- System.out.print("Enter your choice: ");
-
- // 确定要添加到哪个动物园
- int addChoice = input.nextInt();
- input.nextLine(); // 清除缓冲区
-
- Zoo selectedZooForAdd = (addChoice == 1) ? southernZone : northernZone;
-
- // 从用户获取动物详细信息
- System.out.print("Enter animal name: ");
- String name = input.nextLine();
-
- System.out.print("Enter animal species: ");
- String species = input.nextLine();
-
- System.out.print("Enter animal age: ");
- int age = input.nextInt();
- input.nextLine(); // 清除缓冲区
-
- // 创建并添加新动物
- Animal newAnimal = new Animal(name, species, age);
- boolean added = selectedZooForAdd.addAnimal(newAnimal);
-
- if (added) {
- System.out.println("Animal added successfully!");
- }
-
- // 显示更新后的动物园
- selectedZooForAdd.displayAnimals();
- break;
-
- case 5:
- // 移除动物
- System.out.println("\n----- REMOVE ANIMAL -----");
- System.out.println("1. Remove from Southern-Zone Zoo");
- System.out.println("2. Remove from Northern-Zone Zoo");
- System.out.print("Enter your choice: ");
-
- // 确定从哪个动物园移除
- int removeChoice = input.nextInt();
- input.nextLine(); // 清除缓冲区
-
- Zoo selectedZooForRemove = (removeChoice == 1) ? southernZone : northernZone;
-
- // 显示可用的动物
- selectedZooForRemove.displayAnimals();
-
- // 检查动物园是否有动物可移除
- if (selectedZooForRemove.getCounter() == 0) {
- System.out.println("No animals to remove.");
- break;
- }
-
- // 获取要移除的动物名称
- System.out.print("Enter the name of the animal to remove: ");
- String animalToRemove = input.nextLine();
-
- // 移除动物并显示更新后的动物园
- selectedZooForRemove.deleteAnimal(animalToRemove);
- selectedZooForRemove.displayAnimals();
- break;
-
- case 6:
- // 查找动物
- System.out.println("\n----- FIND ANIMAL -----");
- System.out.print("Enter the name of the animal to find: ");
- String animalToFind = input.nextLine();
-
- // 在两个动物园中搜索动物
- int indexSouthern = southernZone.findAnimal(animalToFind);
- int indexNorthern = northernZone.findAnimal(animalToFind);
-
- // 根据动物被发现的位置显示结果
- if (indexSouthern != -1) {
- System.out.println("Animal found in Southern-Zone Zoo:");
- southernZone.getAnimal(indexSouthern).display();
- } else if (indexNorthern != -1) {
- System.out.println("Animal found in Northern-Zone Zoo:");
- northernZone.getAnimal(indexNorthern).display();
- } else {
- System.out.println("Animal '" + animalToFind + "' not found in either zoo.");
- }
- break;
-
- case 0:
- running = false;
- System.out.println("-----*****Exiting Zoo Management System. Goodbye!******-----");
- break;
-
- default:
- System.out.println("Invalid choice. Please try again.");
- }
- }
-
- // 关闭scanner
- input.close();
- }
- }
复制代码
主要修复内容:
- 创建了缺失的Animal、Item和Logistics类
- 修复了ZooManagementSystem中的输入处理问题,确保在使用nextInt()后调用nextLine()清除缓冲区
- 增加了更详细的注释(中文)
- 确保在程序退出时关闭Scanner
这个实现现在应该可以正常工作,处理了之前代码中的所有问题。
|
|