简体   繁体   中英

Add a menu loop that: Displays the (included) menu Prompts the user for input and validates the input Takes the appropriate action based on the input

Below are the directions and the code I have so far. I am not sure where I am going wrong but I know that my loop isn't right. Will you please help me? In a new Java file, create the Monkey class, using the specification document as a guide. The Monkey class must do the following: Inherit from the RescueAnimal class. Implement all attributes to meet the specifications. Include a constructor. You may use a default constructor. To score “exemplary” on this criterion, you must include the more detailed constructor that takes all values for the attributes and sets them. Refer to the constructor in the Dog class for an example. Include accessors and mutators for all implemented attributes.

In the Driver.java class, modify the main method. In main(), you must create a menu loop that does the following: Displays the menu by calling the displayMenu method. This method is in the Driver.java class. Prompts the user for input Takes the appropriate action based on the value that the user entered

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


    

The issue with your code is that while your loop is working fairly well, it has no while section at the end. Your code within the while loop also seems to only print the menu option instead of doing anything. I'd say the best thing to do would be to call the displayMenu() method with displayMenu(); at the beginning of the do loop then finish the loop with }while (.input.equals("q"))

In regards to your Monkey class, remember it is extending from the RescueAnimal class. So you do not need to redo the same setters/getters from the RescueAnimal class. All you need are the Monkey specific attributes.

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

public Monkey(String species, String tailLength, String height, String boydLength) { 
            setSpecies(species);
            setTailLength(tailLength);
            setHeight(height);
            setBodyLength(bodyLength);
    }

public String getSpecies() {
    return species;
}

public void setSpecies(String species) {
    this.species = species;
}

public String getTailLength() {
    return tailLength;
}

public void setTailLength(String tailLength) {
    this.tailLength = tailLength;
}

public String getHeight() {
    return height;
}

public void setHeight(String height) {
    this.height = height;
}

public String getBodyLength() {
    return bodyLength;
}

public void setBodyLength(String bodyLength) {
    this.bodyLength = bodyLength;
}

}

And for your driver class, remember a do/while loop only works when both are utilized. So DO this, WHILE this condition is met.

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>();

    public static void main(String[] args) {


    initializeDogList();
    initializeMonkeyList();
    
    Scanner input = new Scanner(System.in); // scanner class object
    char option;
    
    do // loop until user exits application
    {
        displayMenu();
        option = input.next().charAt(0);
        if (option == '1') { // intakeNewDog method is called
            intakeNewDog(input);
        }
        
        else if (option == '2') { // intakeNewMonkey method is called
            intakeNewMonkey(input);
        }
        
        else if (option == '3') { // reserveAnimal method is called
            reserveAnimal(input);
        }
        
        else if (option == '4') { // printAnimals method is called to print the dog list
            printAnimals(option);
        }
        
        else if (option == '5') { // printAnimals method is called to print the monkey list
            printAnimals(option);
        }
        
        else if (option == '6') { // printAnimals method is called to print all available animals
            printAnimals(option);
        }
        
        else if (option == 'q') { // exit message prints and application stops running
            System.out.print("You have exited the application.");
            break;
        }
        
        else { // in the event of an invalid input, tells user and prompts for a new input
            System.out.print("You have entered an invalid input. Please enter a valid input.");
        }
    }
    while (option != 'q');
}

// 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
public static void initializeMonkeyList() {
    Monkey monkey1 = new Monkey("Alan", "male", "2", "8.5", "03-26-2020", "United States", "in service", false, "United States", "Squirrel Monkey", "14.8", "15.6", "17.2");
    Monkey monkey2 = new Monkey("Lucas", "male", "1", "7.2", "09-12-2021", "Argentina", "Phase 4", true, "United States", "Capuchin", "19", "21.2", "18.5");
    Monkey monkey3 = new Monkey("Sasha", "female", "3", "18.2", "07-07-2019", "Colombia", "in service", true, "Colombia", "Tamarin", "15.2", "11", "12.2");
    
    monkeyList.add(monkey1);
    monkeyList.add(monkey2);
    monkeyList.add(monkey3);
}

// Already completed intakeNewDog method
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
        }
    }

    System.out.println("What is the dog's breed?"); // the following is to add a new dog to the system
    String breed = scanner.nextLine();
    System.out.println("What is the dog's gender?");
    String gender = scanner.nextLine();
    System.out.println("What is the dog's age?");
    String age = scanner.nextLine();
    System.out.println("What is the dog's weight?");
    String weight = scanner.nextLine();
    System.out.println("What is the dog's acquisition date?");
    String acquisitionDate = scanner.nextLine();
    System.out.println("What is the dog's acquisition country?");
    String acquisitionCountry = scanner.nextLine();
    System.out.println("What is the dog's training status?");
    String trainingStatus = scanner.nextLine();
    System.out.println("Is this dog reserved?");
    boolean reserved = scanner.nextBoolean();
    scanner.nextLine();
    System.out.println("Which country is the dog in service?");
    String inServiceCountry = scanner.nextLine();
    
    Dog dog4 = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reserved, inServiceCountry);
    dogList.add(dog4);
    System.out.println("Your entry has been added to the Dog List.");
}

    public static void intakeNewMonkey(Scanner scanner) {
        System.out.println("What is the 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 gender?"); // the following is to add a new monkey to the system
    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 this monkey reserved?");
    boolean reserved = scanner.nextBoolean();
    System.out.println("Which country is the monkey in service?");
    String inServiceCountry = scanner.nextLine();
    System.out.println("What is the monkey's species?");
    String species = scanner.nextLine();
    System.out.println("What is the tail length?");
    String tailLength = scanner.nextLine();
    System.out.println("What is the height?");
    String height = scanner.nextLine();
    System.out.println("What is the body length?");
    String bodyLength = scanner.nextLine();
    
    Monkey monkey4 = new Monkey(name, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reserved, inServiceCountry, species, tailLength, height, bodyLength);
    monkeyList.add(monkey4);
    System.out.println("Your entry has been added to the Monkey List.");
    }

    public static void reserveAnimal(Scanner scanner) { // how to reserve a monkey
        scanner.nextLine();
        System.out.println("Enter animal type: ");
        String animalType = scanner.nextLine();
        if (animalType.equalsIgnoreCase("Monkey")) {
            System.out.println("Enter the monkey's acquisition country: ");
            String country = scanner.nextLine();
            for (Monkey obj: monkeyList) {
                if (obj.getAcquisitionLocation().equalsIgnoreCase(country)) {
                    obj.setReserved(true);
                    System.out.println("This monkey is now reserved.");
                    return;
                }
            }
            System.out.println("The monkey entered is not in the list.");
        }
        else if (animalType.equalsIgnoreCase("Dog")) { // how to reserve a dog
            System.out.println("Enter the dog's acquisition country: ");
            String country = scanner.nextLine();
            for (Dog obj: dogList) {
                if (obj.getAcquisitionLocation().equalsIgnoreCase(country)) {
                    obj.setReserved(true);
                    System.out.println("This dog is now reserved.");
                    return;
                }
            }
            System.out.println("The dog entered is not in the list.");
        }
        else {
            System.out.println("Animal type not found.");
        }
    }

    public static void printAnimals(char option) { // print options for dogList, monkeyList, all available animals
        if (option == '4') {
            System.out.println(dogList);
        }
        else if (option == '5') {
            System.out.println(monkeyList);
        }
        else if (option == '6') {
            for (int i = 0; i < dogList.size(); i++) {
                if (dogList.get(i).getTrainingStatus().equals("in service") && dogList.get(i).getReserved()==false) {
                    System.out.println(dogList.get(i));
                }
            }
            for (int i = 0; i < monkeyList.size(); i++) {
                if (monkeyList.get(i).getTrainingStatus().equals("in service") && monkeyList.get(i).getReserved()==false) {
                    System.out.println(monkeyList.get(i));
                }
            }
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM