208k views
4 votes
Create a grocery list app that uses an empty ArrayList to store and interact with grocery items added by the user. Use the switch case statement to allow the user to perform the following six actions:

1) Add items to the list using a string method to ensure all entries are added as lowercase.
2) Remove items from the list.
3) Sort the list using Collections.
4) Print the list using a user-defined method (described below).
5) Search for the index number of a grocery item.
6) Quit the application and print the final version of the list using the user-defined method described below.
Include the following actions and user-defined methods:
i) Include a while loop to allow users to continue using the menu items until they wish to quit the application.
ii) Make sure the menu is displayed each time the user loops back to the beginning of the while loop.
iii) Create a method that outputs a menu displaying the options above.
iv) Create a method that uses a for-each loop to output each item on the list. If the list is empty, display to the user that there are no items on the list.

1 Answer

3 votes

Final answer:

To create a grocery list app, use a while loop, switch case statement, and ArrayList to handle different actions such as adding, removing, sorting, printing, searching, and quitting. Use string methods to ensure lowercase entries and a user-defined method to display the menu. Utilize the Collections class to sort the list and the ArrayList methods to manipulate the data.

Step-by-step explanation:

To create a grocery list app that uses an empty ArrayList, you can start by creating a while loop that continues until the user chooses to quit. Inside the loop, you can display a menu using a user-defined method that allows the user to choose from different actions using a switch case statement.

In the switch case statement, you can handle each action separately:

  1. To add items to the list, you can use a string method to convert the input to lowercase and then add it to the ArrayList.
  2. To remove items from the list, you can prompt the user for the item to remove and use the remove() method of the ArrayList to delete it.
  3. To sort the list, you can use the Collections.sort() method.
  4. To print the list, you can use a user-defined method that uses a for-each loop to output each item. If the list is empty, you can display a message to the user.
  5. To search for the index number of a grocery item, you can use the indexOf() method of the ArrayList.
  6. To quit the application, you can break out of the while loop and call the user-defined method to print the final version of the list.

By following these steps, you can create a functional grocery list app that allows users to add, remove, sort, print, search, and quit the application using an ArrayList.

User DSquare
by
8.9k points