Site icon DataFlair

DSA Project – Restaurant Order Processing System

Learn DSA & Get Ready for MAANG Companies Start Now!!

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)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME 50
#define MAX_ITEM 50

typedef struct Order      // Node
{
    struct Order* prev;
    int orderId;
    char customerName[MAX_NAME];
    char foodItem[MAX_ITEM];
    struct Order* next;
} Order;

Order* head = NULL;
Order* tail = NULL;
int nextOrderId = 1;

// Create a new order
Order* createOrder(char* customerName, char* foodItem) 
{
    Order* newOrder = (Order*)malloc(sizeof(Order));   // New
    newOrder->orderId = nextOrderId++;
    strcpy(newOrder->customerName, customerName);
    strcpy(newOrder->foodItem, foodItem);
    newOrder->prev = NULL;
    newOrder->next = NULL;
    return newOrder;
}

// Add order at the end
void addOrder(char* customerName, char* foodItem) 
{
    Order* newOrder = createOrder(customerName, foodItem);
    if (head == NULL) 
    {
        head = tail = newOrder;
    } else 
    {
        tail->next = newOrder;
        newOrder->prev = tail;
        tail = newOrder;
    }
    printf("Order #%d added for %s: %s\n", newOrder->orderId, customerName, foodItem);
}

// Serve the first order
void serveOrder() 
{
    if (head == NULL) 
    {
        printf("No orders to serve.\n");
        return;
}
    Order* temp = head;
    printf("Serving Order #%d: %s (%s)\n", temp->orderId, temp->customerName, temp->foodItem);
    head = head->next;
    if (head!=NULL)
        head->prev = NULL;
    else
        tail = NULL; // List became empty
    free(temp);
}

// Cancel the last order
void cancelLastOrder() {
    if (tail == NULL) {
        printf("No orders to cancel.\n");
        return;
    }
    Order* temp = tail;
    printf("Cancelling Order #%d: %s (%s)\n", temp->orderId, temp->customerName, temp->foodItem);
    tail = tail->prev;
    if (tail)
        tail->next = NULL;
    else
        head = NULL; // List became empty
    free(temp);
}

// View all orders
void viewOrders()
 {
    if (head == NULL) 
    {
        printf("No pending orders.\n");
        return;
    }
    printf("\n--- Pending Orders ---\n");
    Order* temp = head;
    while (temp!=NULL)
     {
        printf("Order #%d | Customer: %s | Item: %s\n", temp->orderId, temp->customerName, temp->foodItem);
        temp = temp->next;
    }
}
void  viewOrderByID()
{
    int id;
    
   if(head==NULL)
   {
     printf("No pending orders.\n");
     return;
   }  
  else
  {
    printf("Enter Order id :");
    scanf("%d",&id);

      Order *temp=head;
      while(temp!=NULL)
      {
          if(id==temp->orderId)
         { 
          printf("Order #%d | Customer: %s | Item: %s\n", temp->orderId, temp->customerName, temp->foodItem);
          return;
         }  
         temp=temp->next;      
      }
      printf("No pending orders of  %d id is found.\n",id);

  }   

}
void menu() {
    int choice;
    char name[MAX_NAME];
    char item[MAX_ITEM];

    while (1) {
        printf("\n==== Restaurant Order System ====\n");
        printf("1. Add New Order\n");
        printf("2. Serve First Order\n");
        printf("3. Cancel Last Order\n");
        printf("4. View All Orders\n");
        printf("5. View  Orders By ID\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar();  // consume newline

        switch (choice) {
            case 1:
                printf("Enter customer name: ");
                fgets(name, MAX_NAME, stdin);
                name[strcspn(name, "\n")] = 0; // remove newline
                printf("Enter food item: ");
                fgets(item, MAX_ITEM, stdin);
                item[strcspn(item, "\n")] = 0;
                addOrder(name, item);  
                break;
            case 2:
                serveOrder();
                break;
            case 3:
                cancelLastOrder();
                break;
            case 4:
                viewOrders();
                break;
                case 5:
                 viewOrderByID();
                 break;    
            case 6:
                printf("Exiting system.\n");
                return;
            default:
                printf("Invalid option. Try again.\n");
        }
    }
}

int main() 
{
    menu();
    return 0;
}

 

Exit mobile version