Program 1
// Project: Restaurant Order Processing System(Based on Doubly linked list and DQUEUE)
/*
Features:
1. Add order at the end of the queue (new order)
2. Serve the first order (remove from front)
3. Cancel the last order (remove from end)
4. View all pending orders (from first to last)
5. Search order according to order ID
6. Total Count of pending orders
*/
import java.util.Scanner;
class Order // Node
{
Order prev;
int orderId;
String customerName;
String foodItem;
Order next;
public Order(int orderId, String customerName, String foodItem)
{
this.orderId = orderId;
this.customerName = customerName;
this.foodItem = foodItem;
this.prev=null;
this.next=null;
}
}
public class RestaurantOrderSystem
{
private static Order head = null;
private static Order tail = null;
private static int nextOrderId = 1;
private static final Scanner sc = new Scanner(System.in);
// Create new order node and add at end
public static void addOrder(String customerName, String foodItem)
{
Order newOrder = new Order(nextOrderId++, customerName, foodItem);
if (head == null)
{
head = tail = newOrder;
} else
{
tail.next = newOrder;
newOrder.prev = tail;
tail = newOrder;
}
System.out.println("Order #" + newOrder.orderId + " added for " + customerName + ": " + foodItem);
}
// Serve the first order
public static void serveOrder()
{
if (head == null) {
System.out.println(" No orders to serve.");
return;
}
System.out.println("Serving Order #" + head.orderId + ": " + head.customerName + " - " + head.foodItem);
head = head.next;
if (head != null)
head.prev = null;
else
tail = null;
}
// Cancel the last order
public static void cancelLastOrder()
{
if (tail == null) {
System.out.println(" No orders to cancel.");
return;
}
System.out.println(" Cancelling Order #" + tail.orderId + ": " + tail.customerName + " - " + tail.foodItem);
tail = tail.prev;
if (tail != null) tail.next = null;
else head = null;
}
// View all pending orders
public static void viewOrders()
{
if (head == null)
{
System.out.println(" No pending orders.");
return;
}
System.out.println("\n--- ---- Pending Orders ---------");
Order temp = head;
while (temp != null)
{
System.out.printf("Order #%3d | Customer: %-15s | Item: %s\n", temp.orderId, temp.customerName, temp.foodItem);
temp = temp.next;
}
}
// Search order by ID
public static void searchById(int id)
{
Order temp = head;
while (temp != null)
{
if (temp.orderId == id)
{
System.out.println(" Found Order #" + temp.orderId + ": " + temp.customerName + " - " + temp.foodItem);
return;
}
temp = temp.next;
}
System.out.println(" Order ID not found.");
}
// Search order by Customer Name
public static void searchByName(String name)
{
Order temp = head;
boolean found = false;
while (temp != null) {
if (temp.customerName.equalsIgnoreCase(name))
{
System.out.println(" Order #" + temp.orderId + ": " + temp.foodItem);
found = true;
}
temp = temp.next;
}
if (!found) {
System.out.println(" No orders found for " + name);
}
}
// Count total pending orders
public static void countOrders()
{
int count = 0;
Order temp = head;
while (temp != null) {
count++;
temp = temp.next;
}
System.out.println(" Total pending orders: " + count);
}
// Main menu
public static void menu()
{
while (true) {
System.out.println("\n========== Restaurant Order System ==========");
System.out.println("1. Add New Order");
System.out.println("2. Serve First Order");
System.out.println("3. Cancel Last Order");
System.out.println("4. View All Orders");
System.out.println("5. Search Order by ID");
System.out.println("6. Search Order by Customer Name");
System.out.println("7. Count Pending Orders");
System.out.println("8. Exit");
System.out.println("\n======================================\n");
System.out.print("Enter Your Choice: ");
int choice;
try {
choice = Integer.parseInt(sc.nextLine());
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number.");
continue;
}
switch (choice) {
case 1:
System.out.print("Enter customer name: ");
String name = sc.nextLine();
System.out.print("Enter food item: ");
String item = sc.nextLine();
addOrder(name, item);
break;
case 2:
serveOrder();
break;
case 3:
cancelLastOrder();
break;
case 4:
viewOrders();
break;
case 5:
System.out.print("Enter Order ID: ");
int id = Integer.parseInt(sc.nextLine());
searchById(id);
break;
case 6:
System.out.print("Enter customer name: ");
String searchName = sc.nextLine();
searchByName(searchName);
break;
case 7:
countOrders();
break;
case 8:
System.out.println(" Exiting system. Goodbye!");
return;
default:
System.out.println(" Invalid option. Try again.");
}
}
}
public static void main(String[] args) {
menu();
}
}