Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please edit this code to have 2 Java classes where there is one class handling the UI and one handling the array list. import java.util.ArrayList;

Please edit this code to have 2 Java classes where there is one class handling the UI and one handling the array list.

import java.util.ArrayList;

import java.util.Scanner;

/** * * @author ------ */ public class AnimalArrayList {

public static void main(String[] args) {

ArrayList animals = new ArrayList<>();

String name;

int choice; //opening to promt user options //scanner Scanner scan = new Scanner(System.in);

// loop that continues till the user exits do {

System.out.println(" Menu - ");

System.out.println(" 1. Add animal ");

System.out.println(" 2. Edit animal ");

System.out.println(" 3. Delete animal ");

System.out.println(" 4. Display animals ");

System.out.println(" 5. Quit");

System.out.print(" Please enter option(1-5) : "); //ask for user input from 1-5 choice = scan.nextInt();

scan.nextLine(); // while loop to prevent other enteries. Only allows 1-5 anything else makes it invalid. while (choice < 1 || choice > 5) {

System.out.print(" Invalid option. Enter your choice(1-5) : ");

choice = scan.nextInt();

scan.nextLine();

}

switch (choice) {

case 1: addAnimal(animals);

break;

case 2: System.out.print(" Enter the name : ");

name = scan.nextLine();

editAnimal(animals, name);

break;

case 3: System.out.print(" Enter the name : ");

name = scan.nextLine();

deleteAnimal(animals, name);

break;

case 4: displayAnimals(animals);

break;

case 5: break;

}

} while (choice != 5);

scan.close();

} // data members of AnimalArrayList for name, color, vertebrate, and if they can swim.

public String name;

public String color;

public boolean isVertebrate;

public boolean canSwim;

// default constructor public AnimalArrayList() { }

// parameterized constructor public AnimalArrayList(String name, String color, boolean isVertebrate, boolean canSwim) {

this.name = name;

this.color = color;

this.isVertebrate = isVertebrate;

this.canSwim = canSwim;

}

// public method that allows user to enter animal name public static void addAnimal(ArrayList list) {

String name, color;

String vertebrate, swim;

boolean isVertebrate, canSwim; // start of scanner Scanner scan = new Scanner(System.in);

System.out.print(" Enter animal name : "); //user input name name = scan.nextLine();

System.out.print(" Enter animal color: "); //user input color color = scan.nextLine();

System.out.print(" Is the animal a vertebrate(yes/no) : "); //user input if it is verterbrate vertebrate = scan.nextLine(); //loop while (!vertebrate.equalsIgnoreCase("yes") && !vertebrate.equalsIgnoreCase("no")) {

System.out.print(" Invalid input. Is the animal a vertebrate(yes/no) ? ");

vertebrate = scan.nextLine();

}

System.out.print(" Can the animal swim(yes/no) : "); //user input if the animal can swim or not swim = scan.nextLine();

while (!swim.equalsIgnoreCase("yes") && !swim.equalsIgnoreCase("no")) {

System.out.print(" Invalid input. Can the animal swim(yes/no) ? ");

swim = scan.nextLine();

}

if (vertebrate.equalsIgnoreCase("yes")) { isVertebrate = true; } else { isVertebrate = false; }

if (swim.equalsIgnoreCase("yes")) { canSwim = true; } else { canSwim = false; }

AnimalArrayList animal = new AnimalArrayList(name, color, isVertebrate, canSwim);

list.add(animal);

System.out.print(" Animal added");

}

// public method which allows user to edit animal name and list public static void editAnimal(ArrayList list, String name) {

String color;

String vertebrate, swim;

boolean isVertebrate, canSwim;

int index = -1;

for (int i = 0; i < list.size(); i++) {

if (list.get(i).name.equalsIgnoreCase(name)) {

index = i;

break;

}

}

if (index != -1) { //start of scanner Scanner scan = new Scanner(System.in); //unser imputs eyes color System.out.print(" Enter animal color: ");

color = scan.nextLine();

System.out.print(" Is the animal a vertebrate(yes/no) : "); //user input vertribrate vertebrate = scan.nextLine();

while (!vertebrate.equalsIgnoreCase("yes") && !vertebrate.equalsIgnoreCase("no")) {

System.out.print(" Invalid input. Is the animal a vertebrate(yes/no) ? ");

vertebrate = scan.nextLine();

} //user input yes or no System.out.print(" Can the animal swim(yes/no) : ");

swim = scan.nextLine(); // user must input yes or no, if not then the loop will then repeat while (!swim.equalsIgnoreCase("yes") && !swim.equalsIgnoreCase("no")) {

System.out.print(" Invalid input. Can the animal swim(yes/no) ? ");

swim = scan.nextLine();

} //once user input yes/no it will print out true or false base on input if (vertebrate.equalsIgnoreCase("yes")) { isVertebrate = true; } else { isVertebrate = false; }

if (swim.equalsIgnoreCase("yes")) { canSwim = true; } else { canSwim = false; }

list.get(index).color = color;

list.get(index).canSwim = canSwim;

list.get(index).isVertebrate = isVertebrate;

System.out.print(" Animal edited");

} else { System.out.print(" Animal doesn't exist"); }

}

//public method to allow user to delete an animal in the animals array list public static void deleteAnimal(ArrayList list, String name) {

for (int i = 0; i < list.size(); i++) { //array list of animals lists that user entered if (list.get(i).name.equalsIgnoreCase(name)) {

list.remove(i);

System.out.print(" Animal deleted");

return;

}

}

System.out.print(" Animal doesn't exist");

}

//public method to allow user to view what the user has already entered public static void displayAnimals(ArrayList list) { //prints out list if there is at least 1 animal entered if (list.size() > 0) {

System.out.print(" List of Animals :");

for (int i = 0; i < list.size(); i++) {

System.out.print(" Animal Name : " + list.get(i).name);

System.out.print("\t Animal Color : " + list.get(i).color);

System.out.print("\t Is It A Vertebrate : " + list.get(i).isVertebrate);

System.out.print("\t Can It Swim : " + list.get(i).canSwim);

}

} else { System.out.print(" No Animals exist"); }

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago