1

以下是我到目前为止的说明和代码。我不确定我哪里出错了,但我知道我的循环不对。你能帮帮我吗?在一个新的 Java 文件中,使用规范文档作为指南创建 Monkey 类。Monkey 类必须执行以下操作: 从 RescueAnimal 类继承。实现所有属性以满足规范。包括一个构造函数。您可以使用默认构造函数。要在此标准上获得“模范”评分,您必须包含更详细的构造函数,该构造函数获取属性的所有值并设置它们。示例请参阅 Dog 类中的构造函数。包括所有实现属性的访问器和修改器。

在 Driver.java 类中,修改 main 方法。在 main() 中,您必须创建一个执行以下操作的菜单循环: 通过调用 displayMenu 方法显示菜单。此方法位于 Driver.java 类中。提示用户输入 根据用户输入的值采取适当的行动

import java.util.ArrayList;
import java.util.Scanner;

public class Driver {
    private static ArrayList<Dog> dogList = new ArrayList<Dog>();
    private static ArrayList<Monkey> monkeyList = new ArrayList<Monkey>();
    // Instance variables (if needed)

public static void main(String[] args) {displayMenu();
    Scanner scnr = new Scanner(System.in);
    String displayMenu = scnr.nextLine();
    String menu_option = scnr.nextLine();
    initializeDogList();
    initializeMonkeyList();
    do {
        displayMenu();
        if(menu_option != "q") {
            System.out.println("\t\t\t\tRescue Animal System Menu");
        }
        else if(menu_option == "1") {
            System.out.println("[1] Intake a new dog");
        }
        else if(menu_option == "2") {
            System.out.println("[2] Intake a new monkey");
        }
        else if(menu_option == "3") {
            System.out.println("[3] Reserve an animal");
        }
        else if(menu_option == "4") {
            System.out.println("[4] Print a list of all dogs");
        }
        else if(menu_option == "5") {
            System.out.println("[5] Print a list of all monkeys");
        }
        else if(menu_option =="6") {
            System.out.println("[6] Print a list of all animals that are not reserved");
        }
        else if(menu_option == "q") {
            System.out.println("[q] Quit application");
        }
        else {
            System.out.println("Enter a menu selection");
        }
    }
        
    
    
    
    // Add a loop that displays the menu, accepts the users input
    // and takes the appropriate action.
// For the project submission you must also include input validation
    // and appropriate feedback to the user.
    // Hint: create a Scanner and pass it to the necessary
    // methods 
// Hint: Menu options 4, 5, and 6 should all connect to the printAnimals() method.

}

// This method prints the menu options
public static void displayMenu() {
    System.out.println("\n\n");
    System.out.println("\t\t\t\tRescue Animal System Menu");
    System.out.println("[1] Intake a new dog");
    System.out.println("[2] Intake a new monkey");
    System.out.println("[3] Reserve an animal");
    System.out.println("[4] Print a list of all dogs");
    System.out.println("[5] Print a list of all monkeys");
    System.out.println("[6] Print a list of all animals that are not reserved");
    System.out.println("[q] Quit application");
    System.out.println();
    System.out.println("Enter a menu selection");
}


// Adds dogs to a list for testing
public static void initializeDogList() {
    Dog dog1 = new Dog("Spot", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States");
    Dog dog2 = new Dog("Rex", "Great Dane", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States");
    Dog dog3 = new Dog("Bella", "Chihuahua", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada");

    dogList.add(dog1);
    dogList.add(dog2);
    dogList.add(dog3);
}


// Adds monkeys to a list for testing
//Optional for testing
public static void initializeMonkeyList() {
    Monkey monkey1 = new Monkey();
    Monkey monkey2 = new Monkey();
    Monkey monkey3 = new Monkey();
    
    monkeyList.add(monkey1);
    monkeyList.add(monkey2);
    monkeyList.add(monkey3);

}


// Complete the intakeNewDog method
// The input validation to check that the dog is not already in the list
// is done for you
public static void intakeNewDog(Scanner scanner) {
    System.out.println("What is the dog's name?");
    String name = scanner.nextLine();
    for(Dog dog: dogList) {
        if(dog.getName().equalsIgnoreCase(name)) {
            System.out.println("\n\nThis dog is already in our system\n\n");
            return; //returns to menu
        }
    }

    // Add the code to instantiate a new dog and add it to the appropriate list
}


    // Complete intakeNewMonkey
  //Instantiate and add the new monkey to the appropriate list
    // For the project submission you must also  validate the input
// to make sure the monkey doesn't already exist and the species type is allowed
    public static void intakeNewMonkey(Scanner scanner) {
        System.out.println("The method intakeNewMonkey needs to be implemented");
    }

    // Complete reserveAnimal
    // You will need to find the animal by animal type and in service country
    public static void reserveAnimal(Scanner scanner) {
        System.out.println("The method reserveAnimal needs to be implemented");

    }

    // Complete printAnimals
    // Include the animal name, status, acquisition country and if the animal is reserved.
   // Remember that this method connects to three different menu items.
    // The printAnimals() method has three different outputs
    // based on the listType parameter
    // dog - prints the list of dogs
    // monkey - prints the list of monkeys
    // available - prints a combined list of all animals that are
    // fully trained ("in service") but not reserved 
    // Remember that you only have to fully implement ONE of these lists. 
   // The other lists can have a print statement saying "This option needs to be implemented".
   // To score "exemplary" you must correctly implement the "available" list.
        public static void printAnimals() {
            System.out.println("The method printAnimals needs to be implemented");

        }
}

import java.lang.String;

public class RescueAnimal {

// Instance variables
private String name;
private String animalType;
private String gender;
private String age;
private String weight;
private String acquisitionDate;
private String acquisitionCountry;
private String trainingStatus;
private boolean reserved;
private String inServiceCountry;


// Constructor
public RescueAnimal() {
}


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public String getAnimalType() {
    return animalType;
}


public void setAnimalType(String animalType) {
    this.animalType = animalType;
}


public String getGender() {
    return gender;
}


public void setGender(String gender) {
    this.gender = gender;
}


public String getAge() {
    return age;
}


public void setAge(String age) {
    this.age = age;
}


public String getWeight() {
    return weight;
}


public void setWeight(String weight) {
    this.weight = weight;
}


public String getAcquisitionDate() {
    return acquisitionDate;
}


public void setAcquisitionDate(String acquisitionDate) {
    this.acquisitionDate = acquisitionDate;
}


public String getAcquisitionLocation() {
    return acquisitionCountry;
}


public void setAcquisitionLocation(String acquisitionCountry) {
    this.acquisitionCountry = acquisitionCountry;
}


public boolean getReserved() {
    return reserved;
}


public void setReserved(boolean reserved) {
    this.reserved = reserved;
}


public String getInServiceLocation() {
    return inServiceCountry;
}


public void setInServiceCountry(String inServiceCountry) {
    this.inServiceCountry = inServiceCountry;
}




public String getTrainingStatus() {
    return trainingStatus;
}


    public void setTrainingStatus(String trainingStatus) {
        this.trainingStatus = trainingStatus;
    }
}
public class Dog extends RescueAnimal {

// Instance variable
private String breed;

// Constructor
public Dog(String name, String breed, String gender, String age,
String weight, String acquisitionDate, String acquisitionCountry,
String trainingStatus, boolean reserved, String inServiceCountry) {
    setName(name);
    setBreed(breed);
    setGender(gender);
    setAge(age);
    setWeight(weight);
    setAcquisitionDate(acquisitionDate);
    setAcquisitionLocation(acquisitionCountry);
    setTrainingStatus(trainingStatus);
    setReserved(reserved);
    setInServiceCountry(inServiceCountry);

}

// Accessor Method
public String getBreed() {
    return breed;
}

    // Mutator Method
      public void setBreed(String dogBreed) {
        breed = dogBreed;
    }

}
public class Monkey extends RescueAnimal{
    private String species;
    private String breed;
    private double tailLength;
    private double height;
    private double bodyLength;

enum Species{Capuchin, Guenon, Macaque, marmoset, SquirrelMonkey, Tamarin;}


public static void main(String[] args) {
    // TODO Auto-generated method stub  
}

public Monkey() {
    species = "";
    breed = "";
    tailLength = 0.0;
    height = 0.0;
    bodyLength = 0.0;
}
    
public Monkey(String mSpecies, String mBreed, double mTailLength, double mHeight, double
mBodyLength, String name, String gender, String age, String weight, String acquisitionDate, String
acquisitionCountry,
        String trainingStatus, boolean reserved, String inServiceCountry) {
    this.species = mSpecies;
    this.breed = mBreed;
    this.tailLength = mTailLength;
    this.height = mHeight;
    this.bodyLength = mBodyLength;
    this.name = name;
    this.gender = gender;
    this.age = age;
    this.weight = weight;
    this.acquisitionDate = acquisitionDate;
    this.acquisitionCountry = acquisitionCountry;
    this.trainingStatus = trainingStatus;
    this.reserved = reserved;
    this.inServiceCountry = inServiceCountry;
}

String getSpecies() {
    return species;
}
String getBreed() {
    return breed;
}
double getTailLength() {
    return tailLength;
}
double getHeight() {
    return height;
}
double getBodyLength() {
    return bodyLength;
}

void setSpecies(String mSpecies) {
    this.species = mSpecies;
}
void setBreed(String mBreed) {
    this.breed = mBreed;
}
void setTailLength(double mTailLength) {
    this.tailLength = mTailLength;
}
void setHeight(double mHeight) {
    this.height = mHeight;
}
void setBodyLength(double mBodyLength) {
    this.bodyLength = mBodyLength;
}
}



enter code here


    
4

1 回答 1

0

您的代码的问题是,虽然您的循环运行良好,但它最后没有 while 部分。您在 while 循环中的代码似乎也只打印菜单选项而不做任何事情。我想说最好的办法是用 displayMenu() 调用 displayMenu() 方法;在 do 循环的开头,然后用 }while (!input.equals("q")) 结束循环

于 2021-02-06T17:19:34.283 回答