Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

4. Will technology eliminate the need for HR managers?

Answered: 1 week ago