Taking online college course and I'm new to Java. We just learned about input validation. I've used input validation pretty comfortably for strings, ints, and whatnot. But I had to validate a boolean value so I tried using a Boolean
object (for a null assignment) and it worked.
Instructor feedback is slow at best and I want to learn while it's fresh in my head.
Wondering if there is a more efficient way of doing this in the future?
Code snippet is as follows:
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) {
String userInput;
Scanner scanner = new Scanner(System.in);
boolean appQuit = false;
//initializeDogList();
//initializeMonkeyList();
// Add a loop that displays the menu, accepts the users input
// and takes the appropriate action.
// For the project submission you must also include input validation
// and appropriate feedback to the user.
// Hint: create a Scanner and pass it to the necessary
// methods
// Hint: Menu options 4, 5, and 6 should all connect to the printAnimals() method.
displayMenu();
userInput = scanner.nextLine();
while (!appQuit) { //So long as user doesn't quit application
if (userInput.equals("q")) {
System.out.println("Have a great day!");
appQuit = true;
}
else if (userInput.equals("1")) { //Menu selection for intake a new dog
intakeNewDog(scanner);
displayMenu();
userInput = scanner.nextLine();
}
else if (userInput.equals("2")) {
intakeNewMonkey(scanner);
displayMenu();
userInput = scanner.nextLine();
}
else if (userInput.equals("3")) {
reserveAnimal(scanner);
displayMenu();
userInput = scanner.nextLine();
}
else if (userInput.equals("4")) {
printAnimals();
displayMenu();
userInput = scanner.nextLine();
}
else if (userInput.equals("5")) {
printAnimals();
displayMenu();
userInput = scanner.nextLine();
}
else if (userInput.equals("6")) {
printAnimals();
displayMenu();
userInput = scanner.nextLine();
}
else {
System.out.println("Not a valid input");
displayMenu();
userInput = scanner.nextLine();
}
}
}
// 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
//Optional for testing
public static void initializeMonkeyList() {
Monkey monkey1 = new Monkey("Spot", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States", "1", "1", "1", "Marmoset");
Monkey monkey2 = new Monkey("Rex", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States", "1", "1", "1", "Marmoset");
Monkey monkey3 = new Monkey("Bella", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada", "1", "1", "1", "Marmoset");
monkeyList.add(monkey1);
monkeyList.add(monkey2);
monkeyList.add(monkey3);
}
// 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 intakeDog: dogList) {
if(intakeDog.getName().equalsIgnoreCase(name)) {
System.out.println("\n\nThis dog is already in our system\n\n");
return; //returns to menu
}
}
// Add the code to instantiate a new dog and add it to the appropriate list
System.out.println("What is the dog's gender?");
String gender = scanner.nextLine();
System.out.println("What is the dog's breed?");
String breed = 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?");
System.out.println("Enter Y for yes and N for no.");
String reserveAnswer = scanner.nextLine();
Boolean inputVal = null;
boolean reserved = false;
do {
if (reserveAnswer.equalsIgnoreCase("y")) {
reserved = true;
inputVal = true;
}
else if (reserveAnswer.equalsIgnoreCase("n")) {
reserved = false;
inputVal = true;
}
else {
System.out.println("Invalid input");
System.out.println("");
System.out.println("Is the dog reserved?");
System.out.println("Enter Y for yes and N for no.");
reserveAnswer = scanner.nextLine();
}
} while(inputVal == null);
System.out.println("What is the dog's serviced Country?");
String inServiceCountry = scanner.nextLine();
//create dog object and pass parameters.
Dog intakeDog = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry,
trainingStatus, reserved, inServiceCountry);
//add dog to array
dogList.add(intakeDog);
System.out.println("Dog intake complete.");
}
// Complete intakeNewMonkey
//Instantiate and add the new monkey to the appropriate list
// 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 intakeMonkey: monkeyList) {
if(intakeMonkey.getName().equalsIgnoreCase(name)) {
System.out.println("\n\nThis monkey is already in our system\n\n");
return; //returns to menu
}
}
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?");
System.out.println("Enter Y for yes and N for no.");
String reserveAnswer = scanner.nextLine();
Boolean inputVal = null;
boolean reserved = false;
do {
if (reserveAnswer.equalsIgnoreCase("y")) {
reserved = true;
inputVal = true;
}
else if (reserveAnswer.equalsIgnoreCase("n")) {
reserved = false;
inputVal = true;
}
else {
System.out.println("Invalid input");
System.out.println("");
System.out.println("Is the monkey reserved?");
System.out.println("Enter Y for yes and N for no.");
reserveAnswer = scanner.nextLine();
}
} while(inputVal == null);
System.out.println("What is the monkey's serviced Country?");
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();
//create monkey object and pass parameters.
Monkey intakeMonkey = new Monkey(name, gender, age, weight, acquisitionDate, acquisitionCountry,
trainingStatus, reserved, inServiceCountry, tailLength, height, bodyLength, species);
//add monkey to array
monkeyList.add(intakeMonkey);
System.out.println("Monkey intake complete.");
}
// Complete reserveAnimal
// You will need to find the animal by animal type and in service country
public static void reserveAnimal(Scanner scanner) {
String desiredAnimal;
String desiredCountry;
//take user input
System.out.println("What is the desired animal type to reserve?");
System.out.println("Please enter either Dog or Monkey");
System.out.println("");
//input desired animal and validate input
desiredAnimal = scanner.nextLine();
boolean inputVal = false;
do {
if (desiredAnimal.equalsIgnoreCase("dog")) {
System.out.println("In what country do you want to reserve a dog?");
desiredCountry = scanner.nextLine();
for(Dog intakeDog: dogList) {
if(intakeDog.getInServiceLocation().equalsIgnoreCase(desiredCountry)) {
if(intakeDog.getReserved() == false) {
System.out.println("An unreserved dog has been found in that country");
intakeDog.setReserved(true);
System.out.println("Your newly reserved dog is: " + intakeDog.getName());
return; //return to menu
}
else {
System.out.println("There are no unreserved dogs in that country.");
return;
}
}
}
inputVal = true;
}
else if (desiredAnimal.equalsIgnoreCase("monkey")) {
inputVal = true;
}
else {
System.out.println("Not a valid input.");
System.out.println("Please enter either Dog or Monkey");
System.out.println("");
desiredAnimal = scanner.nextLine();
inputVal = false;
}
} while(inputVal = true);
//use desiredAnimal to decide which array to iterate through
//use desiredCountry to check assignedCountry param in array
//if desiredCountry == assignedCountry, check reserved boolean
//if boolean false, inform user animal is available and update boolean to true
//print newly reserved animal information
//if boolean true, inform user no animal available to reserve
//if desiredCountry != assignedCountry inform user no animal available to reserve at location
}
// 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() {
System.out.println("The method printAnimals needs to be implemented");
}
}
My rescue animal class (Superclass)
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;
}
}
My Dog class (extends rescue animal)
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;
}
}
And finally my monkey class (extends rescue animal class)
public class Monkey extends RescueAnimal {
//Instance 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) {
setName(name);
setGender(gender);
setAge(age);
setWeight(weight);
setAcquisitionDate(acquisitionDate);
setAcquisitionLocation(acquisitionCountry);
setTrainingStatus(trainingStatus);
setReserved(reserved);
setInServiceCountry(inServiceCountry);
setTailLength(tailLength);
setHeight(height);
setBodyLength(bodyLength);
setSpecies(species);
}
//Accessor Methods
public String getTailLength() {
return tailLength;
}
public String getHeight() {
return height;
}
public String getBodyLength() {
return bodyLength;
}
public String getSpecies() {
return species;
}
//Mutator Methods
public void setTailLength(String tailLength) {
this.tailLength = tailLength;
}
public void setHeight(String height) {
this.height = height;
}
public void setBodyLength(String bodyLength) {
this.bodyLength = bodyLength;
}
public void setSpecies(String species) {
this.species = species;
}
}