Question: Assignment 4: Shopping list (JAVA) Objective: Read in the names of several grocery items from the keyboard and create a shopping list using the Java

Assignment 4: Shopping list (JAVA)

Objective: Read in the names of several grocery items from the keyboard and create a shopping list using the Java ArrayList abstract data type.

1) Create a new empty ArrayList

2) Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from the user via the keyboard).

3) Prompt the user for an item to search for on the list. Output a message to the user letting them know whether the item exists in the shopping list. Use a method that is part of the ArrayList class to do the search.

4) Prompt the user for an item to delete from the shopping list and remove it. (be sure to handle the case where they dont want to delete anything). Use a method that is part of the ArrayList class to do the delete.

5) Prompt the user for an item to insert into the list. Ask them what they want to insert and where they want to insert it (after which item). Use a method that is part of the ArrayList class to do the insertion.

6) Output the final list to the user.

MY CODE

package assignment4;

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class arrayexample {

public static void main(String[] args) { ArrayList shoppingList = new ArrayList(); int numItems = 5; for (int i = 1; i < numItems; i++) { String input = JOptionPane.showInputDialog("enter five Items. "); shoppingList.add(input); } String searchItems = JOptionPane.showInputDialog("Enter the item. "); boolean Items = shoppingList.contains(searchItems); if(Items) { System.out.print("List contains item. "); } else { System.out.println("List does not contain item. "); } String removeItems = JOptionPane.showInputDialog("Which item do you want to remove? "); if(shoppingList.contains(removeItems)) { shoppingList.remove(removeItems); } else { System.out.println("The item does not contain in the list. "); } String addItem = JOptionPane.showInputDialog("What item do you want to add on the list "); String afterItem = JOptionPane.showInputDialog("What item do you want to add it after "); shoppingList.add(15);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!