简体   繁体   English

如何搜索我的数组列表以查找保留的动物?

[英]How do I search my array list to find a reserved animal?

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

You should not be using == to test for String equality.您不应该使用==来测试字符串是否相等。 Use equals使用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());
    } 
                

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();在monkey和dog下有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.当您的用户使用 setter 填充数据时,只需使用 if/else 也将可用动物填充到您的新列表中。 (code below is just the reserved setter, all others above it with the add line at the top.) (下面的代码只是保留的 setter,它上面的所有其他代码都带有顶部的添加行。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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