I want to create two methods that deletes 1. deletes an entire plant listing and
ID: 3713523 • Letter: I
Question
I want to create two methods that deletes 1. deletes an entire plant listing and 2. deletes a specific id number from an array list.
here is the array list info:
ArrayList<Plants> plants = new ArrayList<Plants>();
plants.add(new Plants("JAPMA", "Japanese Maple", 30, 60.25, 85.50));
plants.add(new Plants("DOG", "Dogwood", 20, 15.50, 32.50));
plants.add(new Plants("RED", "Redbud", 45, 21.00, 28.00));
plants.add(new Plants("RBUCK", "Red Buckeye", 24, 21.00, 33.00));
plants.add(new Plants("CRMY", "Crape Myrtle", 48, 19.00, 29.50));
plants.add(new Plants("TULIP", "Tulip Tree", 30, 23.00, 38.50));
plants.add(new Plants("FALSE", "Hinoki False Cypress", 40, 18.35, 29.50));
plants.add(new Plants("SERVICE", "Serviceberry", 30, 43.00, 58.50));
plants.add(new Plants("SMOKE", "Smoke Tree", 18, 34.00, 45.50));
return plants;
public void deletePlants(ArrayList<Plants> st) // menu item #7
{
//calls the menuDelete method below
menuDelete();
// write the code to do the item depending on their choice
Scanner scan = new Scanner(System.in);
int ans=0;
boolean exit = false;
do
{
menuDelete();
System.out.println("CHOICE:");
ans = scan.nextInt();
if (ans==1)
{
st.removeAll();
break;
}
if (ans==2)
{
plants.remove(id);
break;
}
if (ans == 3)
{
exit = true;
break;
}
}
while(!true);
}
Explanation / Answer
public void menuDelete(){
System.out.println("Menu");
System.out.println("1. Delete All Plants");
System.out.println("2. Delete plant by ID");
}
public void deletePlants(ArrayList<Plants> st){
// calls the menuDelete method below
menuDelete();
// write the code to do the item depending on their choice
Scanner scan = new Scanner(System.in);
int ans = 0;
boolean exit = false;
do{
menuDelete();
System.out.println("CHOICE:");
ans = scan.nextInt();
if (ans == 1){
deleteAllPlants(st);
//st.removeAll();
break;
}
if (ans == 2){
System.out.println("Enter ID to delete: ");
String ID = scan.nextLine();
deletePlantById(st, ID);
//plants.remove(id);
break;
}
if (ans == 3){
exit = true;
break;
}
} while (!exit);
}
/*
* Removes all the plants from ArrayList and return true . Returns false if
* list is empty
*/
public boolean deleteAllPlants(ArrayList<Plants> st) {
if (st.isEmpty()) {
// Return false if list is empty
System.out.println("List is Empty");
return false;
} else {
// Clear removes all the elements/plants from the list
st.clear();
return true;
}
}
/*
* Removed the plant based on ID and returns true Returns false if list is
* empty or ID not found
*/
public boolean deletePlantById(ArrayList<Plants> st, String id) {
if (st.isEmpty()) {
// Return false if list is empty
System.out.println("List is Empty");
return false;
} else {
int index = 0;
boolean matchFound = false;
// Find out index of the plant with the given ID.
for (Plants plant : st) {
// Check if ID of plant object is equal to ID to be deleted. If
// Yes break else increment the index.
if (plant.id.equals(id)) {
// index = index + 1;
matchFound = true;
break;
} else {
index = index + 1;
}
}
// Remove Element using ID.
if (matchFound) {
st.remove(index);
System.out.println("Plant with " + id + "removed from the list");
return true;
} else
// ID did not match
System.out.println("ID did not match");
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.