In: Computer Science
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.
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.