2
\$\begingroup\$

Taking online college course and I'm new to Java. We just learned about input validation. I've used input validation pretty comfortably for strings, ints, and whatnot. But I had to validate a boolean value so I tried using a Boolean object (for a null assignment) and it worked.

Instructor feedback is slow at best and I want to learn while it's fresh in my head.

Wondering if there is a more efficient way of doing this in the future?

Code snippet is as follows:

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) {
        String userInput;
        Scanner scanner = new Scanner(System.in);
        boolean appQuit = false;


        //initializeDogList();
        //initializeMonkeyList();

        // 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.
        
        displayMenu();

        userInput = scanner.nextLine();
        while (!appQuit) { //So long as user doesn't quit application
            if (userInput.equals("q")) {
                System.out.println("Have a great day!");
                appQuit = true;
            }
            
            else if (userInput.equals("1")) { //Menu selection for intake a new dog
                intakeNewDog(scanner);
                displayMenu();
                userInput = scanner.nextLine();
            }
            
            else if (userInput.equals("2")) {
                intakeNewMonkey(scanner);
                displayMenu();
                userInput = scanner.nextLine();
            }
            
            else if (userInput.equals("3")) {
                reserveAnimal(scanner);
                displayMenu();
                userInput = scanner.nextLine();
            }
            
            else if (userInput.equals("4")) {
                printAnimals();
                displayMenu();
                userInput = scanner.nextLine();
            }
            
            else if (userInput.equals("5")) {
                printAnimals();
                displayMenu();
                userInput = scanner.nextLine();
            }
            
            else if (userInput.equals("6")) {
                printAnimals();
                displayMenu();
                userInput = scanner.nextLine();
            }
            
            else {
                System.out.println("Not a valid input");
                displayMenu();
                userInput = scanner.nextLine();
            }


        }
        
    }

            

    // 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("Spot", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States", "1", "1", "1", "Marmoset");
        Monkey monkey2 = new Monkey("Rex", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States", "1", "1", "1", "Marmoset");
        Monkey monkey3 = new Monkey("Bella", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada", "1", "1", "1", "Marmoset");

        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 intakeDog: dogList) {
            if(intakeDog.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
        System.out.println("What is the dog's gender?");
        String gender = scanner.nextLine();
        
        System.out.println("What is the dog's breed?");
        String breed = 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 the dog reserved?");
        System.out.println("Enter Y for yes and N for no.");
        String reserveAnswer = scanner.nextLine();
            Boolean inputVal = null;
            boolean reserved = false;
            do {
                if (reserveAnswer.equalsIgnoreCase("y")) {
                    reserved = true;
                    inputVal = true;                    
                }
                
                else if (reserveAnswer.equalsIgnoreCase("n")) {
                    reserved = false;
                    inputVal = true;
                }
                
                else {
                    System.out.println("Invalid input");
                    System.out.println("");
                    System.out.println("Is the dog reserved?");
                    System.out.println("Enter Y for yes and N for no.");
                    reserveAnswer = scanner.nextLine();
                }
            } while(inputVal == null);
           
        
        System.out.println("What is the dog's serviced Country?");
        String inServiceCountry = scanner.nextLine();

        
        //create dog object and pass parameters.
        Dog intakeDog = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry,
                                        trainingStatus, reserved, inServiceCountry);
        
        //add dog to array
        dogList.add(intakeDog);
        
        System.out.println("Dog intake complete.");
    }


        // 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("What is the monkey's name?");
        String name = scanner.nextLine();
        for(Monkey intakeMonkey: monkeyList) {
            if(intakeMonkey.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis monkey is already in our system\n\n");
                return; //returns to menu
            }
        }
        
        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?");
        System.out.println("Enter Y for yes and N for no.");
        String reserveAnswer = scanner.nextLine();
            Boolean inputVal = null;
            boolean reserved = false;
            do {
                if (reserveAnswer.equalsIgnoreCase("y")) {
                    reserved = true;
                    inputVal = true;                    
                }
                
                else if (reserveAnswer.equalsIgnoreCase("n")) {
                    reserved = false;
                    inputVal = true;
                }
                
                else {
                    System.out.println("Invalid input");
                    System.out.println("");
                    System.out.println("Is the monkey reserved?");
                    System.out.println("Enter Y for yes and N for no.");
                    reserveAnswer = scanner.nextLine();
                }
            } while(inputVal == null);
        
        System.out.println("What is the monkey's serviced Country?");
        String inServiceCountry = scanner.nextLine();
        
        System.out.println("What is the monkey's tail length?");
        String tailLength = scanner.nextLine();
        
        System.out.println("What is the monkey's height?");
        String height = scanner.nextLine();
        
        System.out.println("What is the monkey's body length?");
        String bodyLength = scanner.nextLine();
        
        System.out.println("What is the monkey's species?");
        String species = scanner.nextLine();
        
        //create monkey object and pass parameters.
        Monkey intakeMonkey = new Monkey(name, gender, age, weight, acquisitionDate, acquisitionCountry,
                                        trainingStatus, reserved, inServiceCountry, tailLength, height, bodyLength, species);
        
        //add monkey to array
        monkeyList.add(intakeMonkey);
        
        System.out.println("Monkey intake complete.");
        
    }
        

        // Complete reserveAnimal
        // You will need to find the animal by animal type and in service country
    public static void reserveAnimal(Scanner scanner) {
        String desiredAnimal;
        String desiredCountry;
        
        //take user input
        
        System.out.println("What is the desired animal type to reserve?");
        System.out.println("Please enter either Dog or Monkey");
        System.out.println("");
        
        //input desired animal and validate input
        desiredAnimal = scanner.nextLine();
        boolean inputVal = false;
        do {
            
            if (desiredAnimal.equalsIgnoreCase("dog")) {
                System.out.println("In what country do you want to reserve a dog?");
                desiredCountry = scanner.nextLine();
                for(Dog intakeDog: dogList) {
                    
                    if(intakeDog.getInServiceLocation().equalsIgnoreCase(desiredCountry)) {
                        
                        if(intakeDog.getReserved() == false) {
                            System.out.println("An unreserved dog has been found in that country");
                            intakeDog.setReserved(true);
                            System.out.println("Your newly reserved dog is: " + intakeDog.getName());
                            return; //return to menu
                        }
                        
                        else {
                            System.out.println("There are no unreserved dogs in that country.");
                            return;
                        }
                    }
                }
                inputVal = true;
            }
            
            else if (desiredAnimal.equalsIgnoreCase("monkey")) {
                inputVal = true;
            }
            
            else {
                System.out.println("Not a valid input.");
                System.out.println("Please enter either Dog or Monkey");
                System.out.println("");
                desiredAnimal = scanner.nextLine();
                inputVal = false;
            }           
        } while(inputVal = true);
        
    
        //use desiredAnimal to decide which array to iterate through
        
        //use desiredCountry to check assignedCountry param in array
        
        //if desiredCountry == assignedCountry, check reserved boolean
        
            //if boolean false, inform user animal is available and update boolean to true
        
                //print newly reserved animal information
        
            //if boolean true, inform user no animal available to reserve
        
        //if desiredCountry != assignedCountry inform user no animal available to reserve at location

    }

        // 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");

        }
}

My rescue animal class (Superclass)



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;
    }
}

My Dog class (extends rescue animal)


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;
    }

}

And finally my monkey class (extends rescue animal class)


public class Monkey extends RescueAnimal {
    
    //Instance variables
    private String tailLength;
    private String height;
    private String bodyLength;
    private String species;
    
    
    // Constructor
    public Monkey(String name, String gender, String age,
    String weight, String acquisitionDate, String acquisitionCountry,
    String trainingStatus, boolean reserved, String inServiceCountry,
    String tailLength, String height, String bodyLength, String species) {
        setName(name);
        setGender(gender);
        setAge(age);
        setWeight(weight);
        setAcquisitionDate(acquisitionDate);
        setAcquisitionLocation(acquisitionCountry);
        setTrainingStatus(trainingStatus);
        setReserved(reserved);
        setInServiceCountry(inServiceCountry);
        setTailLength(tailLength);
        setHeight(height);
        setBodyLength(bodyLength);
        setSpecies(species);
    }
    
    //Accessor Methods
    public String getTailLength() {
        return tailLength;
    }
    
    public String getHeight() {
        return height;
    }
    
    public String getBodyLength() {
        return bodyLength;
    }
    
    public String getSpecies() {
        return species;
    }
    
    //Mutator Methods
    public void setTailLength(String tailLength) {
        this.tailLength = tailLength;
    }
    
    public void setHeight(String height) {
        this.height = height;
    }
    
    public void setBodyLength(String bodyLength) {
        this.bodyLength = bodyLength;
    }
    
    public void setSpecies(String species) {
        this.species = species;
    }
       
}
\$\endgroup\$
1
  • \$\begingroup\$ Added them, sorry it's my first time posting to the site. Thank you for the corrections. \$\endgroup\$
    – Ryan Jones
    Apr 13 at 22:58

2 Answers 2

2
\$\begingroup\$

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much as possible against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Calling overridable methods in a constructor is very bad. The reason is that any child is still not initialized (fields are filled later, rest of child constructor executed after super constructor). When then an overriden child method is called it uses uninitialized child fields, that will be overwritten after the child method. You could make a Setter final to repair it.

\$\endgroup\$
0
2
\$\begingroup\$

ArrayList is not a good choice of data structure for your animals. Prefer instead a HashMap by String, so that lookups by name are more efficient.

Convert Driver to an instance class instead of a static class, and instantiate it in your main(). Make your dogs and monkeys (drop the "List" suffix) final instance members instead of mutable static members. Keep a single scanner member rather than passing it around to subroutines.

Don't have an appQuit loop flag; just return. Other loop flag variables like inputVal should also go away.

Convert your if on userInput into a switch.

Do not repeat displayMenu() and nextLine() after each option; do this only once on the inside of the loop at the start.

Do not hard-code \n, which is a system-specific newline. Instead, rely on println() or use %n in a printf call.

Delete your initializeList() methods. Driver is not the place for testing; move this to an actual JUnit unit test.

Delete basically all of your comments. Writing //returns to menu helps no one.

Age, weight and length variables must not be Strings, and should be instead some numeric like float.

Factor out a yes/no loop and validation method rather than copying and pasting it. Also, rather than equalsIgnoreCase, a more forgiving predicate would be just checking to see that the string starts with y or n after being lower-cased.

Your animal classes are problematic - they have mutable-by-default members, when they should have immutable-by-default members. Note: the only member variable in the entire application that needs to mutate is reserved, so make a reserve() mutation method for that, delete every set() method in the application, and mark all but boolean reserved as final.

RescueAnimal() needs to populate its constructor arguments rather than having a default constructor. Dog() and Monkey() need to call super(). No constructor should be calling set() methods, and should instead directly initialise members like this.breed = breed.

For the reservation logic, doing a country and reservation status lookup can be done with streams instead of a loop.

In a case like Your newly reserved dog is, prefer printf over string concatenation.

Suggested

Driver.java

import java.util.*;

import static java.lang.System.out;

public class Driver {
    private final Map<String, Dog> dogs = new HashMap<>();
    private final Map<String, Monkey> monkeys = new HashMap<>();
    private final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        new Driver().run();
    }

    public void run() {
        while (true) {

            displayMenu();
            String userInput = scanner.nextLine();

            switch (userInput) {
                case "q" -> {
                    out.println("Have a great day!");
                    return;
                }

                case "1" -> intakeNewDog();
                case "2" -> intakeNewMonkey();
                case "3" -> reserveAnimal();
                case "4", "5", "6" -> printAnimals();
                default -> out.println("Not a valid input");
            }
        }
    }

    public static void displayMenu() {
        out.printf(
            "\t\t\t\tRescue Animal System Menu"
            + "%n[1] Intake a new dog"
            + "%n[2] Intake a new monkey"
            + "%n[3] Reserve an animal"
            + "%n[4] Print a list of all dogs"
            + "%n[5] Print a list of all monkeys"
            + "%n[6] Print a list of all animals that are not reserved"
            + "%n[q] Quit application"
            + "%n"
            + "%nEnter a menu selection: "
        );
    }

    public void intakeNewDog() {
        out.println("What is the dog's name?");
        String name = scanner.nextLine();

        if (dogs.containsKey(name.toLowerCase())) {
            out.println("This dog is already in our system");
            return;
        }

        out.println("What is the dog's gender?");
        String gender = scanner.nextLine();

        out.println("What is the dog's breed?");
        String breed = scanner.nextLine();

        out.println("What is the dog's age?");
        float age = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the dog's weight?");
        float weight = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the dog's acquisition date?");
        String acquisitionDate = scanner.nextLine();

        out.println("What is the dog's acquisition Country?");
        String acquisitionCountry = scanner.nextLine();

        out.println("What is the dog's training status?");
        String trainingStatus = scanner.nextLine();

        boolean reserved = inputYesNo("Is the dog reserved?");

        out.println("What is the dog's serviced Country?");
        String inServiceCountry = scanner.nextLine();

        Dog intakeDog = new Dog(
            name, breed, gender, age, weight, acquisitionDate, acquisitionCountry,
            trainingStatus, reserved, inServiceCountry
        );

        dogs.put(name.toLowerCase(), intakeDog);
        out.println("Dog intake complete.");
    }

    private boolean inputYesNo(String prompt) {
        while (true) {
            out.println(prompt);
            out.println("Enter Y for yes and N for no.");
            String answer = scanner.nextLine().toLowerCase();

            if (answer.startsWith("y"))
                return true;
            if (answer.startsWith("n"))
                return false;

            out.println("Invalid input");
        }
    }

    public void intakeNewMonkey() {
        out.println("What is the monkey's name?");
        String name = scanner.nextLine();
        if (monkeys.containsKey(name.toLowerCase())) {
            out.println("This monkey is already in our system");
            return;
        }

        out.println("What is the monkey's gender?");
        String gender = scanner.nextLine();

        out.println("What is the monkey's age?");
        float age = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the monkey's weight?");
        float weight = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the monkey's acquisition date?");
        String acquisitionDate = scanner.nextLine();

        out.println("What is the monkey's acquisition Country?");
        String acquisitionCountry = scanner.nextLine();

        out.println("What is the monkey's training status?");
        String trainingStatus = scanner.nextLine();

        boolean reserved = inputYesNo("Is the monkey reserved?");

        out.println("What is the monkey's serviced Country?");
        String inServiceCountry = scanner.nextLine();

        out.println("What is the monkey's tail length?");
        float tailLength = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the monkey's height?");
        float height = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the monkey's body length?");
        float bodyLength = scanner.nextFloat();
        scanner.nextLine();

        out.println("What is the monkey's species?");
        String species = scanner.nextLine();

        Monkey intakeMonkey = new Monkey(
            name, gender, age, weight, acquisitionDate, acquisitionCountry,
            trainingStatus, reserved, inServiceCountry, tailLength, height, bodyLength, species
        );

        monkeys.put(name.toLowerCase(), intakeMonkey);
        out.println("Monkey intake complete.");
    }

    public void reserveAnimal() {
        while (true) {
            out.println("What is the desired animal type to reserve?");
            out.println("Please enter either Dog or Monkey");
            out.println();

            String desiredAnimal = scanner.nextLine().toLowerCase();
            switch (desiredAnimal) {
                case "dog" -> {
                    reserveDog();
                    return;
                }
                case "monkey" -> {
                    reserveMonkey();
                    return;
                }
                default -> out.println("Not a valid input.");
            }
        }
    }

    public void reserveDog() {
        out.println("In what country do you want to reserve a dog?");
        String desiredCountry = scanner.nextLine();

        Optional<Dog> intakeDog = dogs.values().stream()
            .filter(dog ->
                dog.getInServiceLocation().equalsIgnoreCase(desiredCountry)
                && !dog.isReserved()
            )
            .findFirst();

        if (intakeDog.isPresent()) {
            out.println("An unreserved dog has been found in that country");
            intakeDog.get().reserve();
            out.printf("Your newly reserved dog is: %s%n", intakeDog.get().getName());
        }
        else {
            out.println("There are no unreserved dogs in that country.");
        }
    }

    public void reserveMonkey() {
    }

    public static void printAnimals() {
        out.println("The method printAnimals needs to be implemented");
    }
}

RescueAnimal.java

public class RescueAnimal {
    private final String name;
    private final String animalType;
    private final String gender;
    private final float age;
    private final float weight;
    private final String acquisitionDate;
    private final String acquisitionCountry;
    private final String trainingStatus;
    private final String inServiceCountry;
    private boolean reserved;

    public RescueAnimal(
        String name,
        String animalType,
        String gender,
        float age,
        float weight,
        String acquisitionDate,
        String acquisitionCountry,
        String trainingStatus,
        boolean reserved,
        String inServiceCountry
    ) {
        this.name = name;
        this.animalType = animalType;
        this.gender = gender;
        this.age = age;
        this.weight = weight;
        this.acquisitionDate = acquisitionDate;
        this.acquisitionCountry = acquisitionCountry;
        this.trainingStatus = trainingStatus;
        this.reserved = reserved;
        this.inServiceCountry = inServiceCountry;
    }

    public String getName() { return name; }
    public String getAnimalType() { return animalType; }
    public String getGender() { return gender; }
    public float getAge() { return age; }
    public float getWeight() { return weight; }
    public String getAcquisitionDate() { return acquisitionDate; }
    public String getAcquisitionLocation() { return acquisitionCountry; }
    public boolean isReserved() { return reserved; }
    public String getInServiceLocation() { return inServiceCountry; }
    public String getTrainingStatus() { return trainingStatus; }

    public void reserve() {
        reserved = true;
    }
}

Dog.java

public class Dog extends RescueAnimal {
    private final String breed;

    public Dog(
        String name,
        String breed,
        String gender,
        float age,
        float weight,
        String acquisitionDate,
        String acquisitionCountry,
        String trainingStatus,
        boolean reserved,
        String inServiceCountry
    ) {
        super(
            name, "dog", gender, age, weight, acquisitionDate,
            acquisitionCountry, trainingStatus, reserved, inServiceCountry
        );
        this.breed = breed;
    }

    public String getBreed() { return breed; }
}

Monkey.java

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

    public Monkey(
        String name,
        String gender,
        float age,
        float weight,
        String acquisitionDate,
        String acquisitionCountry,
        String trainingStatus,
        boolean reserved,
        String inServiceCountry,
        float tailLength,
        float height,
        float bodyLength,
        String species
    ) {
        super(
            name, "monkey", gender, age, weight, acquisitionDate, acquisitionCountry,
            trainingStatus, reserved, inServiceCountry
        );

        this.tailLength = tailLength;
        this.height = height;
        this.bodyLength = bodyLength;
        this.species = species;
    }

    public float getTailLength() { return tailLength; }
    public float getHeight() { return height; }
    public float getBodyLength() { return bodyLength; }
    public String getSpecies() { return species; }
}
\$\endgroup\$
2
  • \$\begingroup\$ Don't import java.lang.String; There is always an implicit import java.lang.*;. \$\endgroup\$
    – swpalmer
    Apr 14 at 21:15
  • \$\begingroup\$ @swpalmer Fine; though that's only an error that I inherited from the OP. Fixed. \$\endgroup\$
    – Reinderien
    Apr 14 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.