-1

Hello i am having a problem figuring out how to search my array list for an animal with a false reserve and if one is found how to print that animals name. I need to do this for all animals in both array list. I have attached my code below. I can not find the right method for searching through the array list and then printing that objects name if the element is found.

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) {
        Scanner scnr = new Scanner(System.in);
        

        initializeDogList();
        initializeMonkeyList();
        
        //Menu loop
        
        while(true) {
            displayMenu();
            String menuOption = scnr.nextLine();
            if (menuOption.equals("1")) {
                intakeNewDog(scnr);
                
            }
            else if (menuOption.equals("2") ) {
                intakeNewMonkey(scnr);
            }
            else if (menuOption.equals("3")) {
                reserveAnimal(scnr);
            }
            else if (menuOption.equals("4")) {
                printAnimals("dog");
            }
            else if (menuOption.equals("5")) {
                printAnimals("monkey");
                
            }
            else if (menuOption.equals("6")){
                printAnimals("available");
                
            }
            else if (menuOption.equals("q")) {
                break;
            }
            else {
                System.out.print("Invalid Input");
            }
        }
    }

    // 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() {

    }


    //Intakes a new dog
    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
            }
        }
        
        //Gathers all information for new dog
        
        System.out.println("What is the dog's breed?");
        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("In what country was the dog acquired?");
        String acquisitionDate = scanner.nextLine();
        System.out.println("Where was the dog acquired?");
        String acquisitionCountry = scanner.nextLine();
        System.out.println("What is the dog's training status?");
        String trainingStatus = scanner.nextLine();
        System.out.println("What country is the dog in service?");
        String inServiceCountry = scanner.nextLine();
        
        //Creates a new object for dogList
        
        Dog objt = new Dog(name, breed, gender, age, weight, acquisitionDate,
                 acquisitionCountry, trainingStatus, false, inServiceCountry);
        
        //adds new dog to dogList
        
        dogList.add(objt);
        
        //End message to let user know that the animal has been added.
        System.out.println("Thank you! " + name + " has been added to the system.");
        
        
    }


        
    
        // 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 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?"); 
            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("When was the monkey acquired?");
            String acquisitionDate = scanner.nextLine();
            System.out.println("What country was the monkey acquired in?");
            String acquisitionCountry = scanner.nextLine();
            System.out.println("What is the monkey's training status?");
            String trainingStatus = scanner.nextLine();
            System.out.println("What country is the monkey in service?");
            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();
          
            
            Monkey objt = new Monkey(name, gender, age, weight, acquisitionDate,
                                     acquisitionCountry, trainingStatus, false,
                                     inServiceCountry, tailLength, height,
                                     bodyLength, species);
            monkeyList.add(objt);
            
            System.out.println("Thank you! " + name + " has been added to the system.");
        }

        // 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("What type of animal would you like to reserve?");
            String animalType = scanner.nextLine();
            if (animalType == "dog") {
                
            }
            else if (animalType == "monkey") {

            }
            else {
                System.out.println("Invalid animal.");
            }   
            System.out.println("What country is the animal in service?");
            String inServiceCountry = scanner.nextLine();
            
            
        }

        // 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(String type) {
        
            String listType = type;
            if (listType == "dog") {
                System.out.println(dogList.toString());
                
            } 
            else if(listType == "monkey") {
                System.out.println(monkeyList.toString());
                
            } 
            else {
            }
                
            
            

        }
}


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

    @Override
    public String toString() {
        return getName();
    }
}


public class Monkey extends RescueAnimal {
    
    //Monkey 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) {
        setTailLength(tailLength);
        setHeight(height);
        setBodyLength(bodyLength);
        setSpecies(species);
        setName(name);
        setGender(gender);
        setAge(age);
        setWeight(weight);
        setAcquisitionDate(acquisitionDate);
        setAcquisitionLocation(acquisitionCountry);
        setTrainingStatus(trainingStatus);
        setReserved(reserved);
        setInServiceCountry(inServiceCountry);
                    
    }
    
    //Mutator
    
    public void setTailLength(String TailLength){
        tailLength = TailLength;
    }
    
    public void setHeight(String Height) {
        height = Height;
    }
    
    public void setBodyLength(String BodyLength) {
        bodyLength = BodyLength;
    }
    
    public void setSpecies(String Species) {
        species = Species;
    }
    
    //Accessor
    
    public String getTailLength() {
        return tailLength;
    }
    
    public String getHeight() {
        return height;
    }
    
    public String getBodyLength() {
        return bodyLength;
    }
    
    public String getSpecies() {
        return species;
    }
    @Override
    public String toString() {
        return getName();
    }
}


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;
    }
}
CodeCreater
  • 1
  • 1
  • 3
  • You can use the ArrayList as a Stream by called ".stream()" on your ArrayList. This opens up a lot of functional programming methods to use. You'll want to use ".filter(Predicate predicate)" here, it should more or less perfectly suite your needs. Once done with your ".stream()", I suppose you'll want a new ArrayList of the non-reserved animals, so you'll call ".collect(Collector collector)" for pre-16 JDK implementations. An example pre-16: `dogList.stream().filter(d -> !d.reversed).collect(Collectors.toList)`. An example >16: `dogList.stream().filter(d -> !d.reserved).toList()` – Aliics Aug 12 '21 at 23:52

2 Answers2

0

You should not be using == to test for String equality. Use equals

    if (listType == "dog") { 
           System.out.println(dogList.toString());
    } 
    else if(listType == "monkey") { 
        System.out.println(monkeyList.toString());
     }

Change to

   if ("dog".equals(listType)) { 
           System.out.println(dogList.toString());
    } 
    else if("monkey".equals(listType)) { 
        System.out.println(monkeyList.toString());
    } 
                
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Like my father always told me,... "The easiest way to do something is to not do it." kidding,... mostly, instead of searching your list, make a new one. under monkey and dog have availableList(); or whatever you want. when your user fills data with the setter just use if/else to also fill the available animal to your new list. (code below is just the reserved setter, all others above it with the add line at the top.)

        System.out.println("Is " + name + "reserved?");
        if (scan.next().equalsIgnoreCase("yes")) {
            dog.setReserved(true);          
        } else {
            dog.setReserved(false);
            RescueAnimal animal = new RescueAnimal();
            availableList.add(-i, animal);