简体   繁体   中英

Printing an array list and storing data

Sorry for the long post, but I need to be able to print the array lists and all it prints is the memory
address, I understand how to intake the input but I can't figure out how to display the input.

I am also struggling with finishing the printAnimals function and reserve animals function.

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

public class Driver {

private static Scanner scanner = new Scanner(System.in);

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

private static ArrayList<Monkey> monkeyList = new ArrayList<Monkey>();

public static void main(String[] args) {

    String menuChoice = "";
    
    initializeDogList();
    initializeMonkeyList();

// Loop to display and run menu choices based on user input

while( !menuChoice.equalsIgnoreCase("q") ){
    displayMenu();
    menuChoice = scanner.nextLine();
    switch(menuChoice) {
    case "1":
        intakeNewDog(scanner);
        System.out.println("");
        break;
    case "2":
        intakeNewMonkey(scanner);
        System.out.println("");
        break;
    case "3":
        reserveAnimal(scanner);
        break;
    case "4":
        System.out.println(dogList);
        break;
    case "5":
        System.out.println(monkeyList);
        break;
    case "6":
        printAnimals();
        break;
    
        
        
        
        
    }
}
}

// 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");
    
    dogList.add(dog1);
}




// Adds monkeys to a list for testing
//Optional for testing
public static void initializeMonkeyList() {
    Monkey monkey1 = new Monkey("Jim", "12", "Male", "3", "12", "12-1-2005", "Venezuela", "Phase 7", false, "United States", "15", "5", "Capuchin");
    
    monkeyList.add(monkey1);
}


// 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
        }
    }
    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("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 <true or false>?");
    boolean reservedBoolean = scanner.nextBoolean();
    
    System.out.println("What is the dog's service country?");
    String serviceCountry = scanner.nextLine();
    
    Dog dog = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reservedBoolean, serviceCountry);
    
    dogList.add(dog);
    
}


    //Runs the intake monkey scanner and adds values based on user input
    public static void intakeNewMonkey(Scanner scanner) {
        
        System.out.println("What is the new monkey's name?");
        String name = scanner.nextLine();
        for (Monkey monkey: monkeyList) {
            if(monkey.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis dog is already in our system.\n\n");
                return;
            }
        }
        
        System.out.println("What is the monkey's tail length?");
        String tailLength = scanner.nextLine();
        
        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? <enter true or false>");
        boolean reservedBoolean = scanner.nextBoolean();
        
        System.out.println("What is the monkey's service country?");
        String serviceCountry = scanner.nextLine();
        
        System.out.println("What is the monkey's body length?");
        String bodyLength = scanner.nextLine();
        
        System.out.println("What is the monkey's height?");
        String height = scanner.nextLine();
        
        System.out.println("What is the monkey's species?");
        String species = scanner.nextLine();
        
        Monkey monkey = new Monkey(name, tailLength, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reservedBoolean, serviceCountry, bodyLength, height, species);
        
        
        monkeyList.add(monkey);
    }

    // Complete reserveAnimal
    // You will need to find the animal by animal type and in service country
    public static void reserveAnimal(Scanner scanner) {
        
    }

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

    }

}

Like others wrote before, you have to override toString() in Dog and Monkey.

For example, it could look like this:

public class Dog {
    // Code...

    @Override
    public String toString() {
        return name; /* if you have a attribut "name"; or every attribut or String you want to return*/
    }

}

Now you can use System.out.println(dogList) or your for loop (for a better formatted output). I hope that I could help you.

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