0% found this document useful (0 votes)
9 views5 pages

Java Menu Management System Code

The Menu Management System is a Java program that allows users to manage a menu by adding, viewing, updating, and deleting items. It utilizes a HashMap to store menu items with their prices and provides a user interface through a console menu. The program continues to run until the user chooses to exit by selecting the appropriate option.

Uploaded by

saweranadeem693
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Java Menu Management System Code

The Menu Management System is a Java program that allows users to manage a menu by adding, viewing, updating, and deleting items. It utilizes a HashMap to store menu items with their prices and provides a user interface through a console menu. The program continues to run until the user chooses to exit by selecting the appropriate option.

Uploaded by

saweranadeem693
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Project Code:

import [Link]; // Import HashMap to store menu items

import [Link]; // Import Scanner for user input

public class MenuManagementSystem {

// HashMap to store menu items (item name -> item price)

static HashMap<String, Double> menu = new HashMap<>();

// Scanner object to take input from the user

static Scanner input = new Scanner([Link]);

public static void main(String[] args) {

int choice; // To store user's menu choice

// Menu loop

do {

// Displaying options to the user

[Link]("\n--- Menu Management System ---");

[Link]("1. Add Item");

[Link]("2. View Menu");


[Link]("3. Update Item");

[Link]("4. Delete Item");

[Link]("5. Exit");

[Link]("Enter your choice: ");

// Read user choice

choice = [Link]();

[Link](); // Consume newline after nextInt()

// Handle the choice using switch-case

switch (choice) {

case 1:

addItem(); // Call method to add item

break;

case 2:

viewMenu(); // Call method to view menu

break;

case 3:

updateItem(); // Call method to update item

break;

case 4:

deleteItem(); // Call method to delete item

break;

case 5:

[Link]("Exiting... Goodbye!");

break;
default:

[Link]("Invalid choice! Try again.");

} while (choice != 5); // Loop ends when user chooses 5

// Method to add a new item to the menu

public static void addItem() {

[Link]("Enter item name: ");

String name = [Link]().trim(); // Take item name

// Check if item already exists

if ([Link](name)) {

[Link]("Item already exists.");

} else {

[Link]("Enter item price: ");

double price = [Link](); // Take item price

[Link](name, price); // Add item to the menu

[Link]("Item added.");

// Method to view all menu items

public static void viewMenu() {

if ([Link]()) {
[Link]("Menu is empty.");

} else {

[Link]("\n--- Menu Items ---");

// Print each item and its price

for (String item : [Link]()) {

[Link](item + " - Rs. " + [Link](item));

// Method to update an item's price

public static void updateItem() {

[Link]("Enter item name to update: ");

String name = [Link]().trim(); // Take item name

// Check if item exists

if ([Link](name)) {

[Link]("Enter new price: ");

double newPrice = [Link](); // Take new price

[Link](name, newPrice); // Update the price

[Link]("Item updated.");

} else {

[Link]("Item not found.");

}
// Method to delete an item from the menu

public static void deleteItem() {

[Link]("Enter item name to delete: ");

String name = [Link]().trim(); // Take item name

// Check if item exists

if ([Link](name)) {

[Link](name); // Remove the item

[Link]("Item deleted.");

} else {

[Link]("Item not found.");

Summary of Methods:
Method Purpose

addItem() Adds a new item to the menu

viewMenu() Displays all menu items and prices

updateItem() Updates the price of an existing item

deleteItem() Deletes an item from the menu

Common questions

Powered by AI

The Menu Management System uses a switch-case structure to handle user choices. If the user enters a choice that isn't one of the defined options (1-5), the default case of the switch statement catches this input and the system outputs "Invalid choice! Try again." This prompts the user to enter a valid option.

Sorting menu items alphabetically would likely enhance user experience by making items easier to find, especially in larger menus. Users could quickly locate an item by scrolling to its alphabetical position, thus facilitating faster decision-making and enhancing overall usability.

Incorporating user authentication could secure the system from unauthorized access, ensuring that only authorized personnel can modify the menu. This would protect menu integrity and prevent unauthorized changes. It could employ password protection or multi-level access rights, allowing differentiation between users who can view and those who can modify the menu.

Using a HashMap provides fast retrieval, insertion, and deletion operations which are ideal for managing menu items dynamically. However, it does not maintain order, which might complicate displaying items in a particular sequence. Additionally, the use of a simple string as the key assumes unique item names, potentially leading to issues if items share the same name.

The do-while loop in the Menu Management System keeps presenting the menu options to the user until they choose to exit by entering 5. This loop ensures that after each action (add, view, update, delete), the menu options are displayed again for further operations, thus facilitating continuous user interactions without restarting the program.

The system checks for duplicate entries by using the `containsKey` method of the HashMap before adding a new item. When a new item name is entered, the system checks if the item already exists in the menu HashMap. If it does, the system informs the user that the item already exists, preventing the addition of duplicates.

To provide better user feedback, the system could be enhanced by including more descriptive error messages, offering guidance on what constitutes valid input, and confirming successful operations such as item addition or removal. Additionally, the system could validate numerical input for prices to ensure they are within a reasonable range before acceptance.

The system stores prices using the `Double` data type, which allows for decimal values to accurately represent monetary amounts. The user's price input is captured via `input.nextDouble()` and then stored in the HashMap with the item name as the key to maintain association of each item with its price.

If `input.nextLine()` is not used after `input.nextInt()`, the newline character generated by pressing 'Enter' will remain in the input buffer, causing the subsequent `input.nextLine()` calls to read an empty string instead of waiting for new input. This would lead to incorrect or unintended handling of textual inputs following the integer input.

Improvements could include providing feedback on the old price before updating and confirming the update with the user, reinforcing user awareness and reducing mistakes. Additionally, input validation could ensure that the new price input is a positive number to prevent errant updates.

You might also like