Question

In: Computer Science

Read in the names of several grocery items from the keyboard and create a shopping list...

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

Flow of Program:

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 user via the keyboard).

3) Prompt the user for an item to search for in 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 don’t 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.

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Scanner;

public class ShoppingList {
        
        public static void displayList(ArrayList<String> list) {
                System.out.println("Your shopping List:");
                for(int i=0; i<list.size(); i++) {
                        System.out.println((i+1) + ": " + list.get(i));
                }
                System.out.println();
        }

        public static void main(String[] args) {
                
                Scanner in = new Scanner(System.in);
                
                ArrayList<String> shoppingList = new ArrayList<>();
                
                
                for(int i=0; i<5; i++) {
                        System.out.println("Enter an item to insert in shopping list: ");
                        shoppingList.add(in.nextLine());
                }
                
                displayList(shoppingList);

                System.out.println("Enter an item to search in shopping list: ");
                String toSearch = in.nextLine();
                
                if(shoppingList.contains(toSearch)) {
                        System.out.println("The item exists in shopping list");
                } else {
                        System.out.println("The item do not exist in shopping list");
                }
                System.out.println(); 

                System.out.println("Enter an item to delete in shopping list(hit enter to avoid): ");
                String toDelete = in.nextLine();
                
                if(!toDelete.isEmpty()) {
                        if(shoppingList.remove(toDelete)) {
                                System.out.println("Successfully removed the item from the list.");
                        } else {
                                System.out.println("Item was not found.");
                        }
                }
                System.out.println(); 
                
                displayList(shoppingList);
                
                System.out.println("Enter an item to insert: ");
                String toInsert = in.nextLine();
                
                System.out.println("At what position, do you want to insert: ");
                int pos = in.nextInt();
                
                if(pos < 0) {
                        pos = 0;
                }
                if(pos > shoppingList.size()) {
                        pos = shoppingList.size();
                }
                shoppingList.add(pos, toInsert);

                
                displayList(shoppingList);

                in.close();
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Create an array list (names) that will hold the names of several hall of fame soccer...
Create an array list (names) that will hold the names of several hall of fame soccer players. 2. Place your name on the screen as the master recorder:              “Master Hall of Fame Recorder: John Paul Jones” 3. Ask the user how many names they would like to record. (input 5 when the program runs) 4. Create a loop that will ask for the “Hall of fame member #1: “, etc Add in the following names: Pele Rooney Maradona Messi...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt the user if they'd like to (each action is a function): See the list Display all items (if any) in the list Add item to their list Confirm with the user before adding item (y/n or yes/no) If they enter a duplicate item, notify them the item already exists Remove items from their list Confirm with the user before removing item (y/n or yes/no)...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
Write code to read a list of song names and durations from input. Input first receives...
Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit. #include <iostream> #include <string> #include <vector> using namespace std; class Song { public: void SetNameAndDuration(string songName, int songDuration) { name = songName; duration = songDuration; } void PrintSong() const { cout << name << " - " << duration << endl; } string GetName() const { return name;...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese...
Here are some prices for randomly selected grocery items from the grocery store: Items Prices: Cheese $3.29 Butter $4.99 Eggs $3.49 Yogurt $3.49 Juice $3.89 Tea $3.69 Chips $3.99 Soda $1.99 Pastry $2.99 Cerrial $4.99 Oats $3.29 Almond Milk $2.79 Almonds $4.39 Popcorn $3.29 Crackers $3.59 Ice Cream $6.99 Cookies $2.99 Jam $3.69 Peanut Butter $3.29 Coffee $3.19 Green Tea $4.99 BBQ Sauce $2.99 Oil $6.69 Mayonnaise $4.59 Mustard $2.99 1. Compute the sample mean x and the sample standard...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT