Driver.java

docx

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

145

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

8

Uploaded by AmbassadorDove3627

Report
Stanley Artis IT-145 Professor Daff Kalulu 26 November 2023 Package Grazioso import java . util . ArrayList ; import java . util . Scanner ; public class Driver { private static ArrayList < Dog > dogList = new ArrayList < Dog >(); // Instance variables (if needed) private static ArrayList < Monkey > monkeyList = new ArrayList <>(); public static void main ( String [] args ) { 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. char input = 0 ; int input1 = Character . getNumericValue ( input ); boolean isValidSelection = true ; displayMenu (); //Starts Menu// do { isValidSelection = true ; try { input = scnr . nextLine (). charAt ( 0 );
if ( input == 'q' ) { //Quits Menu System . out . print ( "Good Bye!" ); System . exit ( 0 ); } else { input1 = Character . getNumericValue ( input ); } if ( input1 < 1 || input1 > 6 ) throw new Exception ( "Invalid Entry. Please try again" ); } catch ( Exception excpt ) { System . out . println ( excpt . getMessage ()); isValidSelection = false ; } } while ( ! isValidSelection ); switch ( input1 ) { case 1 -> intakeNewDog (scnr); case 2 -> intakeNewMonkey (scnr); case 3 -> reserveAnimal (scnr); case 4 -> printDogs (); case 5 -> printMonkey (); case 6 -> printAnimals (); } } // This method prints the menu options public static void displayMenu () { System . out . println ( " \n\n " ); System . out . println ( " \t\t\t\t Rescue 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 ( "Johnny" , "Macaque" , "5.9" , "8.9" , "20.6" , "male" , "1" , "16.3" , "08-10-2019" , "Canada" , "Phase I" , true , "Canada" ); Monkey monkey2 = new Monkey ( "Breck" , "Capuchin" , "5.1" , "11.2" , "21.7" , "female" , "1" , "17.1" , "06-04-2020" , "Mexico" , "in service" , false , "Mexico" ); Monkey monkey3 = new Monkey ( "Earl" , "Tamarin" , "5.7" , "7.7" , "19.3" , "male" , "3" , "17.2" , "12-05-2021" , "United States" , "intake" , false , "United States" ); 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 dog : dogList ) { if ( dog . getName (). equalsIgnoreCase ( name )) { System . out . println ( " \n\n This dog is already in our system \n\n " );
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
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 breed?" ); //Prompt user for dogs breed String breed = scanner . nextLine (); System . out . println ( "What is the dog's gender?" ); //Prompt user for dogs gender String gender = scanner . nextLine (); System . out . println ( "What is the dog's age?" ); //Prompt user for dogs age String age = scanner . nextLine (); System . out . println ( "What is the dog's weight?" ); //Prompt user for dogs weight String weight = scanner . nextLine (); System . out . println ( "What is the dog's acquisition date?" ); //Prompt user for dogs acquisition date String acqDate = scanner . nextLine (); System . out . println ( "What is the dog's acquisition country?" ); //Prompt user for dogs acquisition country String acqCountry = scanner . nextLine (); System . out . println ( "What is the dog's training status?" ); //Prompt user for dogs training status String ts = scanner . nextLine (); System . out . println ( "Is the dog reserved?" ); //Prompt user for dogs reservation status boolean res = scanner . nextBoolean (); scanner . nextLine (); System . out . println ( "What is the dog's in service country?" ); //Prompt user for dogs service country String isc = scanner . nextLine (); Dog newdog = new Dog ( name , breed , gender , age , weight , acqDate , acqCountry , ts , res , isc ); dogList . add ( newdog ); } // 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 monkey : monkeyList ) { if ( monkey . getName (). equalsIgnoreCase ( name )) { System . out . println ( " \n\n This monkey is already in our system \n\n " ); return ; //returns to menu } } System . out . println ( "What is the monkey's species?" ); String species = scanner . nextLine (); for (@ SuppressWarnings ( "unused" ) Monkey monkey : monkeyList ) { // Checks if monkey species is allowed if ( ! ( species . equalsIgnoreCase ( "Capuchin" )) && ! ( species . equalsIgnoreCase ( "Guenon" )) && ! ( species . equalsIgnoreCase ( "Macaque" )) && ! ( species . equalsIgnoreCase ( "Marmoset" )) && ! ( species . equalsIgnoreCase ( "Squirrel Monkey" )) && ! ( species . equalsIgnoreCase ( "Tamarin" ))) { System . out . println ( " \n\n This monkey's species is not allowed \n\n " ); return ; } System . out . println ( "What is the monkey's tail length?" ); //Prompt user for monkey's tail length String tailLength = scanner . nextLine (); System . out . println ( "What is the monkey's height?" ); //Prompt user for monkey's height String height = scanner . nextLine (); System . out . println ( "What is the monkey's body length?" ); //Prompt user for monkey's body length String bodyLength = scanner . nextLine (); System . out . println ( "What is the monkey's gender?" ); //Prompt user for monkey's gender String gender = scanner . nextLine (); System . out . println ( "What is the monkey's age?" ); //Prompt user for monkey's age String age = scanner . nextLine (); System . out . println ( "What is the monkey's weight?" ); //Prompt user for monkey's weight
String weight = scanner . nextLine (); System . out . println ( "What is the monkey's acquisition date?" ); //Prompt user for monkey's acquisition date String acqDate = scanner . nextLine (); System . out . println ( "What is the monkey's acquisition country?" ); //Prompt user for monkey's acquisition country String acqCountry = scanner . nextLine (); System . out . println ( "What is the monkey's training status?" ); //Prompt user for monkey's training status String ts = scanner . nextLine (); System . out . println ( "Is the monkey reserved?" ); //Prompt user for monkey's reservation status boolean res = scanner . nextBoolean (); scanner . nextLine (); System . out . println ( "What is the monkey's in service country?" ); //Prompt user for monkey's service country String isc = scanner . nextLine (); Monkey newMonkey = new Monkey ( name , species , tailLength , height , bodyLength , gender , age , weight , acqDate , acqCountry , ts , res , isc ); monkeyList . add ( newMonkey ); } } // Complete reserveAnimal // You will need to find the animal by animal type and in service country public static void reserveAnimal ( Scanner scanner ) { boolean isValidSelection = true ; do { try { System . out . println ( "Enter animal type: Monkey or Dog" ); String type = scanner . nextLine (); isValidSelection = true ; if ( type . equals ( "Monkey" ) || type . equals ( "monkey" )) { System . out . println ( "Name - " + "Training Status - " + "Acquisition Country - " + " Reservation Status" ); for ( int i = 0 ; i < monkeyList . size (); i ++ ) { if ( ! monkeyList . get ( i ). getReserved ())
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
System . out . println ( monkeyList . get ( i )); } System . out . println ( "Enter name: " ); String name = scanner . nextLine (); for ( Monkey obj : monkeyList ) { if ( obj . getName (). equals ( name )) { obj . setReserved ( true ); System . out . println ( "Name - " + "Training Status - " + "Acquisition Country - " + " Reservation Status" ); System . out . println ( obj ); return ; } else throw new Exception ( "Invalid Entry. Please try again" ); } } else if ( type . equals ( "Dog" ) || type . equals ( "dog" )) { System . out . println ( "Name - " + "Training Status - " + "Acquisition Country - " + " Reservation Status" ); for ( int i = 0 ; i < dogList . size (); ++ i ) { if ( ! dogList . get ( i ). getReserved ()) System . out . println ( dogList . get ( i )); } System . out . println ( "Enter name: " ); String name = scanner . nextLine (); for ( Dog obj : dogList ) { if ( obj . getName (). equals ( name )) { obj . setReserved ( true ); System . out . println ( "Name - " + "Training Status - " + "Acquisition Country - " + " Reservation Status" ); System . out . println ( obj ); return ; } else throw new Exception ( "Invalid Entry. Please try again" ); } } else throw new Exception ( "Invalid Entry. Please try again" ); } catch ( Exception excpt ) { System . out . println ( excpt . getMessage ()); isValidSelection = false ; } } while ( ! isValidSelection );
} // 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 ( "Name - " + "Training Status - " + "Acquisition Country - " + " Reservation Status" ); for ( int i = 0 ; i < dogList . size (); ++ i ) { if ( dogList . get ( i ). getReserved () && dogList . get ( i ). getTrainingStatus (). equals ( "in service" )) System . out . println ( dogList . get ( i )); } for ( int i = 0 ; i < monkeyList . size (); ++ i ) { if ( monkeyList . get ( i ). getReserved () && monkeyList . get ( i ). getTrainingStatus (). equals ( "in service" )) System . out . println ( monkeyList . get ( i )); } } }