You are on page 1of 27

Algorithms & Data Structures

CSC-112
Lab Project Proposal

Submitted Khalid Sayed Haleem


By: Maimoona Aziz

Registration FA20-BEE-086
No: FA20-BEE-094
Class: BEE-6B

Submitted Sir Imran Lodhi


To:
Lab Project
Online Food Order System

 Source Code:
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct Food {

char name[50];

float price;

struct Food *next;

} Food;

Food *menu = NULL;

Food *cart = NULL;

void displayMenu() {

Food *temp = menu;

printf("Menu:\n");

while (temp != NULL) {

printf("%s - Rs%.2f\n", temp->name, temp->price);

temp = temp->next;

printf("\n");

}
void addItemToMenu(char name[], float price) {

Food *newFood = (Food *)malloc(sizeof(Food));

strcpy(newFood->name, name);

newFood->price = price;

newFood->next = NULL;

if (menu == NULL) {

menu = newFood;

} else {

Food *temp = menu;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newFood;

void displayCart() {

Food *temp = cart;

float total = 0.0;

printf("Cart:\n");

while (temp != NULL) {

printf("%s - Rs%.2f\n", temp->name, temp->price);

total += temp->price;

temp = temp->next;

printf("Total: Rs%.2f\n", total);

printf("\n");

}
void addToCart(char itemName[]) {

Food *temp = menu;

while (temp != NULL) {

if (strcmp(temp->name, itemName) == 0) {

Food *newFood = (Food *)malloc(sizeof(Food));

strcpy(newFood->name, temp->name);

newFood->price = temp->price;

newFood->next = NULL;

if (cart == NULL) {

cart = newFood;

} else {

Food *cartTemp = cart;

while (cartTemp->next != NULL) {

cartTemp = cartTemp->next;

cartTemp->next = newFood;

printf("Item added to cart: %s\n", itemName);

return;

temp = temp->next;

printf("Item not found in the menu.\n");

// Linked list node


typedef struct Node {

char item[100];

struct Node* next;

} Node;

// Linked list structure

typedef struct LinkedList {

Node* head;

} LinkedList;

// Admin portal class

typedef struct AdminPortal {

char password[100];

LinkedList items;

} AdminPortal;

// User portal class

typedef struct UserPortal {

char password[100];

LinkedList items;

} UserPortal;

// Online Payment Options

typedef struct {

char cardNumber[20];

char cardHolder[50];

int expiryMonth;

int expiryYear;

int cvv;
} CreditCard;

////////////////////////////////////////////

// Function prototypes

void initializeAdminPortal(AdminPortal* adminPortal);

void login(AdminPortal* adminPortal);

void initializeUserPortal(UserPortal* userPortal);

void loginUserPortal(UserPortal* userPortal);

void adminMenu(AdminPortal* adminPortal);

void addItem(AdminPortal* adminPortal);

void deleteAllItems(AdminPortal* adminPortal);

void viewItems(AdminPortal* adminPortal);

void placeOrder();

// Initialize admin portal

void initializeAdminPortal(AdminPortal* adminPortal) {

strcpy(adminPortal->password, "admin123");

adminPortal->items.head = NULL;

// Login to admin portal

void login(AdminPortal* adminPortal) {

char enteredPassword[100];

printf("Enter admin password: ");

scanf("%s", enteredPassword);

if (strcmp(enteredPassword, adminPortal->password) == 0) {

adminMenu(adminPortal);
} else {

printf("Invalid password. Access denied.\n");

void initializeUserPortal(UserPortal* userPortal) {

strcpy(userPortal->password, "user123");

userPortal->items.head = NULL;

void loginUserPortal(UserPortal* userPortal) {

char enteredPassword[100];

printf("Enter user password: ");

scanf("%s", enteredPassword);

if (strcmp(enteredPassword, userPortal->password) == 0) {

printf("Login successful! Welcome to the user portal!\


n");

// Continue with the rest of the application logic

} else {

printf("Invalid password. Access denied.\n");

void loginPortal() {

char username[20];

char password[20];

printf("=== Login Portal ===\n");


printf("Username: ");

fgets(username, sizeof(username), stdin);

username[strcspn(username, "\n")] = '\0'; // Remove the


newline character

printf("Password: ");

fgets(password, sizeof(password), stdin);

password[strcspn(password, "\n")] = '\0'; // Remove the


newline character

// Validate the login details

// You can add your own logic to check against a database


or predefined values

if (strcmp(username, "admin") == 0 && strcmp(password,


"password") == 0) {

printf("Login successful! Welcome, %s!\n", username);

// Continue with the rest of the application logic

} else {

printf("Invalid username or password. Please try


again.\n");

// Handle unsuccessful login attempts

// Admin menu

void adminMenu(AdminPortal* adminPortal) {

int choice;

while (1) {

printf("\nAdmin Portal\n");

printf("1. Add item\n");


printf("2. Delete all items\n");

printf("3. View items\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

addItem(adminPortal);

break;

case 2:

deleteAllItems(adminPortal);

break;

case 3:

viewItems(adminPortal);

break;

case 4:

return;

default:

printf("Invalid choice. Try again.\n");

// Add item

void addItem(AdminPortal* adminPortal) {

char item[100];

printf("Enter item to add: ");


scanf("%s", item);

// Create a new node

Node* newNode = (Node*)malloc(sizeof(Node));

strcpy(newNode->item, item);

newNode->next = NULL;

// Insert the node at the end of the linked list

if (adminPortal->items.head == NULL) {

adminPortal->items.head = newNode;

} else {

Node* current = adminPortal->items.head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;

printf("Item added successfully.\n");

// Delete all items

void deleteAllItems(AdminPortal* adminPortal) {

Node* current = adminPortal->items.head;

while (current != NULL) {

Node* next = current->next;

free(current);

current = next;

}
adminPortal->items.head = NULL;

printf("All items deleted successfully.\n");

// View items

void viewItems(AdminPortal* adminPortal) {

Node* current = adminPortal->items.head;

if (current == NULL) {

printf("No items found.\n");

} else {

printf("Items:\n");

while (current != NULL) {

printf("%s\n", current->item);

current = current->next;

//Payment Options

void placeOrder() {

int choice;

printf("Payment Options:\n");

printf("1. Cash on Delivery\n");

printf("2. Online Payment\n");

printf("Enter your choice: ");

scanf("%d", &choice);
if (choice == 1) {

printf("You have selected Cash on Delivery.\n");

printf("Your order will be delivered and payment will


be collected upon delivery.\n");

} else if (choice == 2) {

CreditCard card;

printf("You have selected Online Payment.\n");

printf("Please enter your credit card details:\n");

printf("Card Number: ");

scanf("%s", card.cardNumber);

printf("Card Holder Name: ");

scanf("%s", card.cardHolder);

printf("Expiry Month: ");

scanf("%d", &card.expiryMonth);

// Clear the input buffer

int c;

while ((c = getchar()) != '\n' && c != EOF) {}

printf("Expiry Year: ");

scanf("%d", &card.expiryYear);

//printf("CVV: ");

//scanf("%d", &card.cvv);
// Process the payment using the provided credit card
details

// You can add your own logic here to validate the


card details and perform the payment

printf("Payment successful! Your order will be


delivered.\n");

} else {

printf("Invalid choice. Please try again.\n");

int main() {

int choice;

char itemName[50];

while (1) {

system("cls");

printf(" Welcome to the Food Ordering


System! \n");

printf("1. User Portal\n");

printf("2. Admin Portal\n");

printf("3. Exit\n");

printf("ENTER YOUR CHOICE: ");

scanf("%d", &choice);

system("cls");

switch (choice) {
case 1: {

UserPortal userPortal;

initializeUserPortal(&userPortal);

loginUserPortal(&userPortal);

while (1) {

system("cls");

printf("1. Display Menu\n");

printf("2. Add Item to Cart\n");

printf("3. Display Cart\n");

printf("4. Payment Options\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

addItemToMenu("Burger", 210);

addItemToMenu("Pizza", 300);

addItemToMenu("Fries", 100);

addItemToMenu("Coke", 60);

displayMenu();

break;

case 2:

printf("Enter item name to add to


cart: ");

scanf("%s", itemName);

addToCart(itemName);

break;
case 3:

displayCart();

break;

case 4:

printf("Welcome to the Online Food


Order System!\n");

placeOrder();

break;

case 5:

printf("Thank you for using the


food ordering system!\n");

break;

default:

printf("Invalid choice. Please try


again.\n");

break;

printf("Press any key to continue to the


main menu...\n");

getch();

if (choice == 5) {

break;

break;

case 2: {

AdminPortal adminPortal;

initializeAdminPortal(&adminPortal);

login(&adminPortal);
break;

case 3:

exit(0);

default:

printf("Invalid choice. Try again.\n");

return 0;

CODE WITH COMMENTS:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct Food {

char name[50];

float price;

struct Food *next;

} Food;

Food *menu = NULL;

Food *cart = NULL;

void displayMenu() {
Food *temp = menu;

printf("Menu:\n");

while (temp != NULL) {

printf("%s - Rs%.2f\n", temp->name, temp->price);

temp = temp->next;

printf("\n");

Comment:

This section defines a struct called Food and declares two global variables, menu
and cart, of type Food*. The displayMenu() function is defined to display the items
in the menu linked list.

void addItemToMenu(char name[], float price) {

Food *newFood = (Food *)malloc(sizeof(Food));

strcpy(newFood->name, name);

newFood->price = price;

newFood->next = NULL;

if (menu == NULL) {

menu = newFood;

} else {

Food *temp = menu;

while (temp->next != NULL) {

temp = temp->next;
}

temp->next = newFood;

Comment:

This function displays the items in the cart linked list along with the total price of
all the items.

void addToCart(char itemName[]) {

Food *temp = menu;

while (temp != NULL) {

if (strcmp(temp->name, itemName) == 0) {

Food *newFood = (Food *)malloc(sizeof(Food));

strcpy(newFood->name, temp->name);

newFood->price = temp->price;

newFood->next = NULL;

if (cart == NULL) {

cart = newFood;

} else {

Food *cartTemp = cart;

while (cartTemp->next != NULL) {

cartTemp = cartTemp->next;

cartTemp->next = newFood;

}
printf("Item added to cart: %s\n", itemName);

return;

temp = temp->next;

printf("Item not found in the menu.\n");

Comment:

This function adds an item to the cart based on the provided item name. It searches
for the item in the menu linked list and if found, creates a new Food node with the
same name and price and adds it to the end of the cart linked list.

typedef struct Node {

char item[100];

struct Node* next;

} Node;

typedef struct LinkedList {

Node* head;

} LinkedList;

typedef struct AdminPortal {

char password[100];

LinkedList items;

} AdminPortal;
typedef struct UserPortal {

char password[100];

LinkedList items;

} UserPortal;

typedef struct {

char cardNumber[20];

char cardHolder[50];

char cvv[5];

} CreditCard;

AdminPortal admin;

UserPortal user;

CreditCard card;

Comment:

This section defines several structs (Node, LinkedList, AdminPortal, UserPortal,


and CreditCard) used in the program to represent linked lists, admin and user
portals, and credit card information. It declares instances of AdminPortal,
UserPortal, and CreditCard structs named admin, user, and card, respectively.

void addItemToPortal(LinkedList *list, char item[]) {

Node *newNode = (Node *)malloc(sizeof(Node));

strcpy(newNode->item, item);

newNode->next = NULL;

if (list->head == NULL) {

list->head = newNode;
} else {

Node *temp = list->head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void displayItems(LinkedList *list) {

Node *temp = list->head;

printf("Items:\n");

while (temp != NULL) {

printf("%s\n", temp->item);

temp = temp->next;

printf("\n");

void deleteAllItems(LinkedList *list) {

Node *current = list->head;

Node *next;

while (current != NULL) {

next = current->next;

free(current);

current = next;
}

list->head = NULL;

Comment:

These functions (addItemToPortal(), displayItems(), and deleteAllItems()) are


utility functions for manipulating linked lists (LinkedList) that represent the items
in the admin and user portals. addItemToPortal() adds a new item to a given list,
displayItems() displays the items in a list, and deleteAllItems() frees the memory
occupied by all the nodes in a list.

void adminMenu() {

int choice;

char itemName[100];

while (1) {

printf("Admin Menu:\n");

printf("1. Add item to menu\n");

printf("2. Delete all items\n");

printf("3. View items\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter item name: ");


scanf("%s", itemName);

addItemToMenu(itemName, 0.0);

printf("Item added to the menu.\n");

break;

case 2:

deleteAllItems(&admin.items);

printf("All items deleted.\n");

break;

case 3:

displayItems(&admin.items);

break;

case 4:

return;

default:

printf("Invalid choice.\n");

printf("\n");

Comment:

This function represents the admin portal menu. It displays a menu of options and
based on the user's choice, performs actions such as adding an item to the menu,
deleting all items, viewing the existing items, or exiting the admin portal.

void userMenu() {
int choice;

char itemName[100];

while (1) {

printf("User Menu:\n");

printf("1. Display menu\n");

printf("2. Add item to cart\n");

printf("3. Display cart\n");

printf("4. Choose payment option\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

displayMenu();

break;

case 2:

printf("Enter item name: ");

scanf("%s", itemName);

addToCart(itemName);

break;

case 3:

displayCart();

break;

case 4:

// Payment option functionality not implemented in the provided code.


printf("Payment option not implemented.\n");

break;

case 5:

return;

default:

printf("Invalid choice.\n");

printf("\n");

Comment:

This function represents the user portal menu. It displays a menu of options and
based on the user's choice, performs actions such as displaying the menu, adding an
item to the cart, displaying the cart, choosing a payment option (not implemented),
or exiting the user portal.

int main() {

// Initialize admin portal items

admin.items.head = NULL;

// Initialize user portal items

user.items.head = NULL;

// Sample menu items (can be added through admin portal)

addItemToMenu("Item 1", 10.0);

addItemToMenu("Item 2", 20.0);


addItemToMenu("Item 3", 30.0);

int role;

printf("Choose your role:\n");

printf("1. Admin\n");

printf("2. User\n");

printf("Enter your choice: ");

scanf("%d", &role);

switch (role) {

case 1:

adminMenu();

break;

case 2:

userMenu();

break;

default:

printf("Invalid choice.\n");

return 0;

Comment:

This is the main() function that serves as the entry point of the program. It
initializes the admin and user portals, adds sample menu items to the menu, and
prompts the user to choose their role (admin or user). Based on the choice, it calls
the corresponding menu function (adminMenu() or userMenu()) to start the portal
interaction.

You might also like