Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public ArrayList loadPlants() { ArrayList plants = new ArrayList(); plants.add(n

ID: 3715800 • Letter: P

Question

public ArrayList loadPlants() { ArrayList plants = new ArrayList(); 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; } Here is the menu I want to use: public void menuDelete() { // 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. //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. //Menu item 3. Go back to the main menu. System.out.println(" 1. delete plants from existing inventory"); System.out.println("2. remove the plant (delete id) from inventory"); System.out.println("3. exit to main menu "); }

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");

            }

}

public ArrayList loadPlants() {

            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 menuDelete() {

            // 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.

            //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.

            //Menu item 3. Go back to the main menu.

            System.out.println("1. delete plants from existing inventory");

            System.out.println("2. remove the plant (delete id) from inventory");

            System.out.println("3. exit to main menu "); }

              

public static void main(String[] args){

            Scanner sc = new Scanner(System.in);

            int choice;

            menuDelete();

            choice = sc.nextInt();

            switch(choice){

                        case 1:

                        int id, quantity;

                        System.out.println("Enter plant ID and number of shares to remove");

                        id = sc.nextInt();

                        quantity = sc.nextInt();

                        removeShares(plants, id,quantity);

                        break;

                        case 2:

                        int idAll;

                        System.out.println("Enter plant ID for which shares are completely removed");

                        idAll = sc.nextInt();

                        removeAll(plants,idAll);

                        break;

                        case 3: return;

            }

}