Question

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

ShoppingList.java

package classes1;

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

public class ShoppingList {
   public static void main(String[] args) {

       //create empty list
       ArrayList<String> items = new ArrayList<>();
       String input;
       Scanner sc = new Scanner(System.in);
       Scanner scInt = new Scanner(System.in);
       //adding five items
       for(int i=0;i<5;i++){
           System.out.print("Enter name of item "+(i+1)+" : ");
           input = sc.nextLine();
           items.add(input);
       }

       //ask and search for an input
       System.out.print("Enter name of item to search : ");
       input = sc.nextLine();
       if(items.contains(input)){
           System.out.println("Item "+input+" present in the list");
       }else{
           System.out.println("Item "+input+" is not present in the list");
       }


       //ask and delete item
       System.out.print("Enter name of item to remove : ");
       input = sc.nextLine();
       if(items.contains(input)){
           items.remove(input);
           System.out.println("Item "+input+" removed from the list");
       }else{
           System.out.println("Item "+input+" is not present in the list");
       }


       //ask and delete item
       System.out.print("Enter name of item to insert : ");
       input = sc.nextLine();
       System.out.print("Enter location where need to add : ");
       int index = scInt.nextInt();
       try{
       items.add(index, input);
       }catch(Exception e){
           System.out.println("Error, Index is out of bound");
       }

      
       //printing items
       System.out.println("\nItems in the cart is as below");
       for(String s : items){
           System.out.println(s);
       }
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
Objective: Read in the names of several grocery items from the keyboard and create a shopping...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • Python Lab 5. 3 Create a shopping list containing some items (strings). Prompt user for an...

    Python Lab 5. 3 Create a shopping list containing some items (strings). Prompt user for an item to be replaced by another item and the new item. Print the updated list.

  • Q1 (2 pts) Shopping list Write a program that prompts a user for items on their...

    Q1 (2 pts) Shopping list Write a program that prompts a user for items on their shopping list and keeps prompting the user until they enter "Done" (make sure your program can stop even if the user enters "Done" with different cases, like "done"), then prints out the items in the list, line by line, and prints the total number of items on the shopping list. Output (after collecting items in the while loop) should look something like this: Apple...

  • working on a Python program, making a shopping list giving the user several options:to view the...

    working on a Python program, making a shopping list giving the user several options:to view the list, to add an item or to delete and an option to exit the program. It's not working at all. please help. File Edit Format Run Options Window Help class shopping: det init (self, final list): self.final_list=1 det add_item(self, item): self.final list.append(item] print("The added temi " + str(item) + ".") det delete item(self, item): gelf.final list.remove(item) print (the deleted item is "+ste (item) +".")...

  • Grocery shopping list (LinkedList)

    Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      // TODO: Declare a LinkedList called shoppingList of type ListItem      String...

  • Grocery shopping list (LinkedList)

    Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      // TODO: Declare a LinkedList called shoppingList of type ListItem      String...

  • need help in Java Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user...

    need help in Java Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user objects. The Facebook class should have methods to list all of the users (i.e. print out their usernames), add a user,delete a user, and get the password hint for a user You will also need to create a driver program. The driver should create an instance of the Facebook class and display a menu containing five options: list users...

  • Warm up: Online shopping cart (Part 1)

    8.6 LAB*: Warm up: Online shopping cart (Part 1)(1) Create two files to submit:ItemToPurchase.java - Class definitionShoppingCartPrinter.java - Contains main() methodBuild the ItemToPurchase class with the following specifications:Private fieldsString itemName - Initialized in default constructor to "none"int itemPrice - Initialized in default constructor to 0int itemQuantity - Initialized in default constructor to 0Default constructorPublic member methods (mutators & accessors)setName() & getName() (2 pts)setPrice() & getPrice() (2 pts)setQuantity() & getQuantity() (2 pts)(2) In main(), prompt the user for two items and create...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT