Introduction
A shopping list application helps users organize the items they need to buy. It allows them to add, view, remove, and update items, ensuring they do not forget anything during shopping. This Java program will implement a basic shopping list application that enables users to manage their shopping list interactively through the command line.
The program will utilize simple data structures, such as an ArrayList, to store the shopping items. The user will interact with the program via a console interface to perform operations like adding an item, displaying the list, and removing items from the list.
Objective
The primary objective of this program is to create a simple shopping list application where users can:
- Add items to the shopping list.
- View the current shopping list.
- Remove items from the shopping list.
- Exit the application when finished.
Code
import java.util.ArrayList; import java.util.Scanner; public class ShoppingListApp { private static ArrayList shoppingList = new ArrayList<>(); private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int choice; do { displayMenu(); choice = scanner.nextInt(); scanner.nextLine(); // Consume the newline character switch (choice) { case 1: addItem(); break; case 2: viewList(); break; case 3: removeItem(); break; case 4: System.out.println("Exiting the application..."); break; default: System.out.println("Invalid choice. Please try again."); } } while (choice != 4); } private static void displayMenu() { System.out.println("\nShopping List Application"); System.out.println("1. Add item to the shopping list"); System.out.println("2. View shopping list"); System.out.println("3. Remove item from the shopping list"); System.out.println("4. Exit"); System.out.print("Enter your choice: "); } private static void addItem() { System.out.print("Enter the item you want to add: "); String item = scanner.nextLine(); shoppingList.add(item); System.out.println(item + " has been added to your shopping list."); } private static void viewList() { if (shoppingList.isEmpty()) { System.out.println("Your shopping list is empty."); } else { System.out.println("\nShopping List:"); for (int i = 0; i < shoppingList.size(); i++) { System.out.println((i + 1) + ". " + shoppingList.get(i)); } } } private static void removeItem() { if (shoppingList.isEmpty()) { System.out.println("Your shopping list is empty. No item to remove."); return; } System.out.print("Enter the number of the item to remove: "); int index = scanner.nextInt(); scanner.nextLine(); // Consume the newline character if (index > 0 && index <= shoppingList.size()) { String removedItem = shoppingList.remove(index - 1); System.out.println(removedItem + " has been removed from your shopping list."); } else { System.out.println("Invalid item number. Please try again."); } } }
Program Explanation
This Java program implements a basic shopping list application where users can add, view, and remove items interactively. Here’s a breakdown of the program’s components:
- ArrayList: The program uses an ArrayList called
shoppingList
to store the items. ArrayLists are dynamic, so they can grow as the user adds more items, making them a perfect fit for this use case. - Menu: The
displayMenu
method displays a menu to the user, giving them options to add, view, or remove items from the list. The user can also exit the application by choosing option 4. - Adding Items: The
addItem
method allows the user to input an item and adds it to the shopping list. It uses thescanner.nextLine()
method to read the user’s input. - Viewing the List: The
viewList
method checks if the shopping list is empty and displays the list of items if there are any. If the list is empty, it informs the user. - Removing Items: The
removeItem
method prompts the user for the index of the item to remove. It verifies if the index is valid and then removes the item from the list. - Input Handling: The program uses a
Scanner
object to capture user input. After each choice, the program responds accordingly by calling the appropriate methods to perform the desired action.
How to Run the Program
Follow these steps to run the program:
- Save the code in a file named
ShoppingListApp.java
. - Open a terminal or command prompt and navigate to the directory where the file is saved.
- Compile the program with the Java compiler:
javac ShoppingListApp.java
- Run the program:
java ShoppingListApp
- The program will display the menu and allow you to interact with the shopping list.