This code implements a **Food Order Management System** with the...

January 2, 2025 at 04:07 AM

#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_USERNAME 50 #define MAX_PASSWORD 50 #define FILENAME "credentials.txt" #define MAX_NAME 100 #define MAX_ITEMS 10 #define MAX_RESTAURANTS 3 #define HASH_SIZE 64 // Structure for menu items at a restaurant typedef struct { char name[MAX_NAME]; float price; } MenuItem; // Structure for a restaurant typedef struct { char name[MAX_NAME]; MenuItem menu[MAX_ITEMS]; int num_items; } Restaurant; // Structure for login data typedef struct { char username[MAX_USERNAME]; char password[MAX_PASSWORD]; } User; // Function to check if a username already exists int checkUsernameExists(char *username) { FILE *file; char file_username[MAX_USERNAME], file_password[MAX_PASSWORD]; file = fopen(FILENAME, "r"); if (file == NULL) { return 0; // No users registered yet } while (fscanf(file, "%s %s", file_username, file_password) != EOF) { if (strcmp(username, file_username) == 0) { fclose(file); return 1; // Username exists } } fclose(file); return 0; // Username doesn't exist } // Function to sign up a new user void signup() { FILE *file; char username[MAX_USERNAME], password[MAX_PASSWORD]; printf("Enter a username: "); scanf("%s", username); if (checkUsernameExists(username)) { printf("Username already exists! Please try a different one.\n"); return; } printf("Enter a password: "); scanf("%s", password); file = fopen(FILENAME, "a"); // Open file in append mode if (file == NULL) { printf("Error opening file!\n"); return; } fprintf(file, "%s %s\n", username, password); // Write username and password to the file printf("Signup successful! You can now login.\n"); fclose(file); } // Function to log in an existing user int login() { FILE *file; char username[MAX_USERNAME], password[MAX_PASSWORD]; char file_username[MAX_USERNAME], file_password[MAX_PASSWORD]; int found = 0; printf("Enter your username: "); scanf("%s", username); printf("Enter your password: "); scanf("%s", password); file = fopen(FILENAME, "r"); // Open file in read mode if (file == NULL) { printf("No users registered yet.\n"); return 0; } while (fscanf(file, "%s %s", file_username, file_password) != EOF) { if (strcmp(username, file_username) == 0 && strcmp(password, file_password) == 0) { printf("Login successful!\n"); found = 1; break; } } fclose(file); if (!found) { printf("Invalid username or password.\n"); } return found; // Return 1 if login successful, otherwise 0 } // Function to display the menu of a restaurant void displayMenu(Restaurant *restaurant) { printf("\nMenu for %s:\n", restaurant->name); for (int i = 0; i < restaurant->num_items; i++) { printf("%d. %s - $%.2f\n", i + 1, restaurant->menu[i].name, restaurant->menu[i].price); } } // Function to order food from a restaurant void orderFood(Restaurant *restaurant, float *totalBill) { int choice, quantity; float bill = 0.0; while (1) { printf("\nEnter the item number to order (0 to finish): "); scanf("%d", &choice); if (choice == 0) { break; // End the order from this restaurant } if (choice > 0 && choice <= restaurant->num_items) { printf("Enter quantity: "); scanf("%d", &quantity); bill += restaurant->menu[choice - 1].price * quantity; } else { printf("Invalid choice! Please try again.\n"); } } *totalBill += bill; // Add the restaurant's bill to the total bill printf("Total bill for %s: $%.2f\n", restaurant->name, bill); } // Function to initialize the restaurants and their menus void initializeRestaurants(Restaurant restaurants[]) { // Restaurant 1 strcpy(restaurants[0].name, "Pizza Place"); restaurants[0].num_items = 3; strcpy(restaurants[0].menu[0].name, "Margherita Pizza"); restaurants[0].menu[0].price = 12.99; strcpy(restaurants[0].menu[1].name, "Pepperoni Pizza"); restaurants[0].menu[1].price = 14.99; strcpy(restaurants[0].menu[2].name, "Veggie Pizza"); restaurants[0].menu[2].price = 13.99; // Restaurant 2 strcpy(restaurants[1].name, "Burger Joint"); restaurants[1].num_items = 3; strcpy(restaurants[1].menu[0].name, "Cheeseburger"); restaurants[1].menu[0].price = 9.99; strcpy(restaurants[1].menu[1].name, "Bacon Burger"); restaurants[1].menu[1].price = 11.99; strcpy(restaurants[1].menu[2].name, "Veggie Burger"); restaurants[1].menu[2].price = 10.99; // Restaurant 3 strcpy(restaurants[2].name, "Sushi Spot"); restaurants[2].num_items = 3; strcpy(restaurants[2].menu[0].name, "California Roll"); restaurants[2].menu[0].price = 8.99; strcpy(restaurants[2].menu[1].name, "Spicy Tuna Roll"); restaurants[2].menu[1].price = 10.99; strcpy(restaurants[2].menu[2].name, "Salmon Roll"); restaurants[2].menu[2].price = 9.99; } // Main function to run the combined login, signup, and food order management system int main() { Restaurant restaurants[MAX_RESTAURANTS]; int choice, restaurantChoice; float totalBill = 0.0; int loggedIn = 0; // Initialize restaurants and menus initializeRestaurants(restaurants); while (1) { printf("\n--- Food Order Management ---\n"); if (!loggedIn) { printf("1. Login\n"); printf("2. Signup\n"); } printf("3. Order Food\n"); printf("4. View Bill\n"); printf("5. Exit\n"); if (!loggedIn) { printf("Enter your choice: "); scanf("%d", &choice); } else { printf("Enter your choice: "); scanf("%d", &choice); } switch (choice) { case 1: if (!loggedIn) { loggedIn = login(); } else { printf("You are already logged in!\n"); } break; case 2: if (!loggedIn) { signup(); } else { printf("You are already logged in!\n"); } break; case 3: if (loggedIn) { printf("\nSelect a Restaurant:\n"); for (int i = 0; i < MAX_RESTAURANTS; i++) { printf("%d. %s\n", i + 1, restaurants[i].name); } printf("Enter your choice: "); scanf("%d", &restaurantChoice); if (restaurantChoice > 0 && restaurantChoice <= MAX_RESTAURANTS) { displayMenu(&restaurants[restaurantChoice - 1]); orderFood(&restaurants[restaurantChoice - 1], &totalBill); } else { printf("Invalid restaurant choice! Try again.\n"); } } else { printf("You must log in first!\n"); } break; case 4: printf("\nTotal Bill: $%.2f\n", totalBill); break; case 5: printf("Exiting program...\n"); return 0; default: printf("Invalid choice. Please try again.\n"); } } return 0; }

This code implements a Food Order Management System with the following features:

  1. User Authentication System:

    • Users can sign up by entering a unique username and password.
    • Usernames and passwords are stored in a plain text file named credentials.txt.
    • During signup, the program ensures that usernames are unique.
    • Users can log in using their registered credentials.
  2. Restaurant and Menu Management:

    • The program stores data for three restaurants, each having its menu of items (up to 10 items per restaurant). Each item has a name and a price.
    • The program allows users to view the menu of a selected restaurant.
  3. Food Ordering:

    • Logged-in users can select a restaurant, view its menu, and place orders for items by specifying quantities.
    • The cost for ordered items is calculated and accumulated into a total bill for the user.
  4. Billing:

    • The program displays the current total bill, which consists of all orders made by the user across restaurants.
  5. Structure of the Code:

    • Uses struct to manage data related to restaurants, menu items, and user credentials.
    • Modular functions are implemented for various tasks like signup, login, checking usernames, displaying menus, and ordering food.
  6. File I/O:

    • The file credentials.txt is used to store and load user credentials for persistence between program runs.
  7. Flow of Execution:

    • On startup, the user is presented with a menu of options. Based on their input, they can:
      • Sign up for a new account.
      • Log in with an existing account.
      • Order food from restaurants.
      • View their total bill.
      • Exit the program.

Example Workflow:

  1. The user starts the program.
  2. If the user is new, they choose to sign up by providing a username and password.
  3. They log in using the registered credentials.
  4. Once logged in, they can:
    • Choose a restaurant.
    • View the menu.
    • Order items by specifying item number and quantity.
  5. They can view their total bill at any point.
  6. They can exit the application when done.

Limitations/Observations:

  • Passwords are stored in plaintext, which is insecure.
  • The program does not hash or encrypt passwords, making it vulnerable to unauthorized access.
  • The system supports a maximum of 3 restaurants and 10 items per restaurant, which is hardcoded.
  • The user must be logged in to order food or view the bill.
  • The data (except user credentials) is not saved between sessions, meaning restaurant data is reinitialized every time the program runs.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node