Home > Blockchain >  print list / help understanding class vs driver method placement
print list / help understanding class vs driver method placement

Time:06-16

I am struggling with basic OOP. I feel like I'm under utilizing the dog and cat classes below and don't know how to call them from the driver so I can avoid rewriting identical code to handle each subclass I may want to print a list of.

This code loops an options menu and a user can either print a doglist, print a monkeyList, or quit. It is a scaled down version of a larger code which has more options. I have left the monkey and dog intake methods to show list contents, but instantiating new dogs or monkeys is not functional in this example.

My main concern is with the printAnimals() method. Yes, it works as is, but what if in the future I want to add bears, and horses, and cats...it will be necessary to keep writing individual loops for each new class I want to print a list of. There has to be a way to apply a single loop that handles all the classes, right?

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

public static void main(String[] args) {


initializeDogList();
initializeMonkeyList();

// Loop that displays the menu, accepts the users input
// and takes the appropriate action
// loop until user quits ("q") or selects valid option
// user selects 1, 2, or q
// invalid selection results in a prompt

Scanner input = new Scanner(System.in); // allows for input
boolean takingInput = true; // loop condition

do { // do while(true)loop
    
    displayMenu(); // call display menu method
    String command = input.nextLine(); // enters menu selection
    
    switch(command) { // command = 1,2, or q
            
        case"1": printAnimals("Dog"); // prints monkey list, no conditions
            break;
            
        case"2": printAnimals("Monkey"); // prints dogs AND monkeys, conditions apply
            break;
                
        case"q": takingInput = false; // loop condition becomes false
            break;
            
        default: // invalid command
            System.out.println("invalid input");
    }
}while(takingInput); // while (true)
System.out.println("Bye"); // when false
}    

// 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] Print a list of all dogs");
System.out.println("[2] Print a list of all monkeys");
System.out.println("[q] Quit application");
System.out.println();
System.out.println("Enter a menu selection");
}


// Dog objects
public static void initializeDogList() {
Dog dog1 = new Dog("Spot", "German Shepherd");
Dog dog2 = new Dog("Rex", "Great Dane");
Dog dog3 = new Dog("Bella", "Chihuahua");

// adds dogs to list
dogList.add(dog1); 
dogList.add(dog2); 
dogList.add(dog3); 
}


// Monkey objects
public static void initializeMonkeyList() {
Monkey monkey1 = new Monkey("Bobo", "Tamarin");
Monkey monkey2 = new Monkey("Shuga", "Marmoset");
Monkey monkey3 = new Monkey("Coco", "Capuchin");

monkeyList.add(monkey1); 
monkeyList.add(monkey2); 
monkeyList.add(monkey3); 
}


// intakeNewDog method
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("This dog is already in our system"); // ...print...
        return; // ... and return to menu.
    }


System.out.println("Enter dog's breed: ");
String breed = scanner.nextLine();
        
// complete instantiation and add dog to list
Dog dog4 = new Dog(name, breed);
dogList.add(dog4); 
System.out.println("New dog added to list");
return;  // to menu
}

// intakeNewMonkey conditions are monkey's name and species
public static void intakeNewMonkey(Scanner scanner) {

System.out.println("Enter monkey's name?");
String name = scanner.nextLine();

// for each monkey object in monkey list:
for(Monkey monkey: monkeyList)
    if(monkey.getName().equalsIgnoreCase(name)) { 
        System.out.println("This monkey is already in our system"); // ...print...
        return; 
    }

System.out.println("Enter monkey's species: ");
species = scanner.nextLine();
        
// complete instantiation. add monkey to list
Monkey monkey4 = new Monkey(name, species);
monkeyList.add(monkey4); // adds new monkey to monkeyList. 
System.out.println("New monkey added to the monkey list");
return; // to menu
}

// The printAnimals() method has 2 different outputs 
public static void printAnimals(String listType) {
    // print dog list if listType is dog
    if (listType.equalsIgnoreCase("Dog")) {
        System.out.println("List of dogs"); 
        for (Dog dog: dogList) { 
            System.out.println("*"   dog.getName()   " and " dog.getBreed());
        }
    }
    // print monkey list if listType is monkey
    if (listType.equalsIgnoreCase("Monkey")) {
        System.out.println("List of monkeys"); 
        // For each monkey in monkey list:
        for (Monkey monkey: monkeyList) {
            System.out.println("*"   monkey.getName()   and monkey.getSpecies()); 
        }
    }
}
}

Should I be doing more below than in the driver??

import java.lang.String;

public class RescueAnimal {

// Instance variables
private String name;
private String animalType;


// 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 class Dog extends RescueAnimal {

// Instance variable
private String breed;

// Constructor
public Dog(String name, String breed) {
    setName(name);
    setBreed(breed);
}

// Accessor Method
public String getBreed() {
    return breed;
}

// Mutator Method
public void setBreed(String dogBreed) {
    breed = dogBreed;
}

}

public class Monkey extends RescueAnimal {

// instance variables
private String species;

// constructor
public Monkey (String name, String species) {
    setName(name);
    setSpecies(species);
}

// Accessor
public String getSpecies() {
    return species;
}

// Mutator
public void setSpecies(String species) {
    this.species = species;
}
}

CodePudding user response:

Add a toString() method to each of your animal classes, e.g:

public class Dog extends RescueAnimal {
  ...
  public String toString() {
    return "*"   dog.getName()   " and " dog.getBreed();
  }
  ...
}

and similarly for Monkey.

Change printAnimals so that you pass it specific List<RescueAnimal>:

public static void printAnimals(String listName, List<RescueAnimal> list) {
        System.out.println("List of "   listName);
        for (RescueAnimal animal: list) { 
            System.out.println(animal.toString());
        } 
}

Now printAnimals() can print any sort of animal.

And your case above becomes:

        case"": printAnimals("Dogs", dogList);
            break;
  •  Tags:  
  • java
  • Related