0

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

    }

}

Seth
  • 3
  • 4
  • 1
    Have you overloaded `toString` in `Dog` / `Monkey` classes? You should do this to get rid of "memory address" which is actually a hash code of the object. – Nowhere Man Feb 18 '21 at 17:11
  • How exactly do I overload toString? I haven't learned that yet – Seth Feb 18 '21 at 17:20
  • 1
    Sorry, I meant overriding, not overloading. [How to override to string properly in Java](https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java) – Nowhere Man Feb 18 '21 at 17:25
  • So would I put the monkeyList and dogList into a toString then print it? I don't understand how the toString would print the values? – Seth Feb 18 '21 at 17:29
  • `toString` provides string representation of the object. When you call `System.out.println(dogList)`, `toString` of `ArrayList` is invoked, and in its turn it invokes `toString()` of each `Dog` instance - that's why you see output like `[Dog@d3d9d231, Dog@abcdef0a]` until `toString` is overridden to return a better string. – Nowhere Man Feb 18 '21 at 17:58
  • I think I'm starting to understand, so that's why I need to override the toString. How would the code look to override the dog instance? I had been trying to use a for loop to print the array list but it wasn't working – Seth Feb 18 '21 at 18:00

1 Answers1

1

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.

bef
  • 36
  • 4
  • So I would return every attribute that I want to be printed? – Seth Feb 18 '21 at 22:53
  • Yes, but you have to build it as a string. For example if you wants to return the attributes name and gender, so you could code this: `return name + " has the gender " + gender;` – bef Feb 19 '21 at 12:32
  • I have been trying to do this and it keeps saying I need to change visibility to protected, not sure what that means. – Seth Feb 19 '21 at 16:05
  • How look your Dog and Monkey classes like? – bef Feb 19 '21 at 16:09
  • it is far too long to post in a comment, is there any other way to print the array list without overriding? – Seth Feb 19 '21 at 20:31
  • A other way would be, that you use a for loop about the list (`for (int i = 0; i < dogList.length(); i++) { }`). In the loop, you can call a function from Dog, which print the information (not so nice), that you want to have, **or** you request the information you want to print from the object with a getter and print it (better). In the loop you get with `dogList.get(i)` the object from the list. – bef Feb 19 '21 at 21:33
  • So I figured out how to do the for loop for the monkey and dog array list, however now the monkey list only prints as null. What is causing this? – Seth Feb 20 '21 at 00:07
  • How look your loops like? – bef Feb 21 '21 at 13:01