I want to create two methods: 1. Menu item 1. Ask the user for the id of the pla
ID: 3713536 • Letter: I
Question
I want to create two methods: 1. Menu item 1. Ask the user for the id of the plant and how many shares to remove
// Do not let them remove more than exist in inventory
// If the id does not exist, make certain to indicate this.
2. Menu item 2. ask for the plant id and totally delete that plant from the collection
// If the id does not exist, make certain to indicate this.
Here is the collection information:
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;
Explanation / Answer
Ask the user for the id of the plant and how many shares to remove:
void removeShares(ArrayList<Plants> plants, int idP, int qty){
boolean found = false;
for(int i=0; i<plants.size() && !(found) ;i++){
Plants temp = plants.get(i);
if(temp.id = idP){
found = true; // to exit the loop when it's found
}
}
if (found){
if(temp.shares < qty){
System.out.println("Number of shares present is: " + temp.shares + " User provided shares: " + qty + "Cannot remove shares more than what is available");
}else{
temp.shares = temp.shares - qty;
plants.add(i, temp);
}
} else{
System.out.println("Plant with id " + idP + "is not found");
}
}
Ask for the plant id and totally delete that plant from the collection:
void removeAll(ArrayList<Plants> plants, int idP){
boolean found = false;
for(int i=0; i<plants.size() && !(found) ;i++){
Plants temp = plants.get(i);
if(temp.id = idP){
found = true; // to exit the loop when it's found
}
}
if (found){
plants.remove(i); // Remove entry at index i where idP is found
}
} else{
System.out.println("Plant with id " + idP + "is not found");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.