Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
25 kB
2
Indexable
Never
import mysql.connector
from prettytable import PrettyTable
import time

# Database Connection
con = mysql.connector.connect(host='localhost', user='root', passwd='root1234')
cur = con.cursor()

try:
    cur.execute("CREATE DATABASE IF NOT EXISTS Restaurant_Management")
    cur.execute("USE Restaurant_Management")

    # Create Tables
    cur.execute("CREATE TABLE IF NOT EXISTS Menu (ItemID INT AUTO_INCREMENT PRIMARY KEY, ItemName VARCHAR(255), "
                "Category VARCHAR(255), Price INT)")

    cur.execute("CREATE TABLE IF NOT EXISTS Orders (OrderID INT AUTO_INCREMENT PRIMARY KEY, ItemID INT, "
                "CustomerName VARCHAR(255), OrderDate DATE, FOREIGN KEY (ItemID) REFERENCES Menu(ItemID))")

    # Insert Sample Data
    cur.execute("INSERT IGNORE INTO Menu VALUES (1, 'Burger', 'Fast Food', 10), "
                "(2, 'Pizza', 'Fast Food', 12), (3, 'Salad', 'Healthy', 8)")

    con.commit()

except mysql.connector.Error as err:
    print(f"Error: {err}")
    con.rollback()

# Display Menu
def display_menu():
    try:
        cur.execute("SELECT * FROM Menu")
        menu_items = cur.fetchall()
        table = PrettyTable(["Item ID", "Item Name", "Category", "Price"])
        for item in menu_items:
            table.add_row(item)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Place Order
def place_order(customer_name):
    try:
        display_menu()
        item_id = int(input("\nEnter the Item ID to order: "))
        order_date = time.strftime('%Y-%m-%d')
        cur.execute('SELECT ItemID FROM Menu')
        data = cur.fetchall()
        if (item_id,) in data:
            cur.execute("INSERT INTO Orders (ItemID, CustomerName, OrderDate) VALUES (%s, %s, %s)",
                        (item_id, customer_name, order_date))
            print(f"\nOrder placed successfully for {customer_name}!")
            con.commit()
        else:
            print('Item not found. ')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Display Customer Orders
def display_customer_orders(customer_name):
    try:
        cur.execute("SELECT Menu.ItemName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "WHERE CustomerName = %s", (customer_name,))
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Main Function
def main():
    while True:
        print("\n\t\t\t\tRestaurant Management System")
        print("1. Display Menu")
        print("2. Place an Order")
        print("3. View My Orders")
        print("4. Exit\n")

        user_choice = input("Enter your choice: ")

        if user_choice == '1':
            display_menu()
        elif user_choice == '2':
            customer_name = input("Enter your name: ")
            place_order(customer_name)
        elif user_choice == '3':
            customer_name = input("Enter your name: ")
            display_customer_orders(customer_name)
        elif user_choice == '4':
            print("Thank you for using the Restaurant Management System!")
            break
        else:
            print("Invalid input! Please choose a valid option.")

# Admin - Admin Functionalities
def admin_menu():
    while True:
        print("\n\t\t\t\tAdmin Menu")
        x = int(input('''
1. View Menu
2. Add Item to Menu
3. View Orders
4. Exit
'''))
        if x == 1:
            display_menu()
        elif x == 2:
            add_item_to_menu()
        elif x == 3:
            view_orders()
        elif x == 4:
            break
        else:
            print('Please enter a valid input. ')

# Add Item to Menu
def add_item_to_menu():
    try:
        item_name = input("Enter Item Name: ")
        category = input("Enter Category: ")
        price = int(input("Enter Price: "))

        cur.execute("INSERT INTO Menu (ItemName, Category, Price) VALUES (%s, %s, %s)",
                    (item_name, category, price))
        con.commit()
        print("Item added to the menu successfully!")

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# View Orders
def view_orders():
    try:
        cur.execute("SELECT Menu.ItemName, Orders.CustomerName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID")
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Customer Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Main Function
def main():
    while True:
        print("\n\t\t\t\tRestaurant Management System")
        print("1. Login as Admin")
        print("2. Customer Menu")
        print("3. Exit\n")

        user_type = input("Enter your choice (Integer only): ")

        if user_type == '1':
            admin_menu()
        elif user_type == '2':
            customer_name = input("Enter your name: ")
            while True:
                print("\n\t\t\t\tCustomer Menu")
                print("1. Display Menu")
                print("2. Place an Order")
                print("3. View My Orders")
                print("4. Exit\n")

                user_choice = input("Enter your choice: ")

                if user_choice == '1':
                    display_menu()
                elif user_choice == '2':
                    place_order(customer_name)
                elif user_choice == '3':
                    display_customer_orders(customer_name)
                elif user_choice == '4':
                    print("Thank you for using the Restaurant Management System!")
                    break
                else:
                    print("Invalid input! Please choose a valid option.")
        elif user_type == '3':
            print("Thank you for using the Restaurant Management System!")
            break
        else:
            print("Invalid input! Please choose a valid option.")


# Admin - Update Item in Menu
def admin_update_menu_item():
    try:
        display_menu()
        item_id = int(input("Enter Item ID to update: "))
        cur.execute('SELECT ItemID FROM Menu')
        data = cur.fetchall()
        if (item_id,) in data:
            new_price = int(input("Enter the new price: "))
            cur.execute("UPDATE Menu SET Price = %s WHERE ItemID = %s", (new_price, item_id))
            con.commit()
            print(f"Item ID {item_id} updated successfully!")
        else:
            print('Sorry, this item does not exist.')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Delete Item from Menu
def admin_delete_menu_item():
    try:
        display_menu()
        item_id = int(input("Enter Item ID to delete: "))
        cur.execute("DELETE FROM Menu WHERE ItemID = %s", (item_id,))
        con.commit()
        print(f"Item ID {item_id} deleted successfully!")

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Display Customers
def admin_display_customers():
    try:
        cur.execute("SELECT DISTINCT CustomerName FROM Orders")
        customers = cur.fetchall()
        table = PrettyTable(["Customer Name"])
        for customer in customers:
            table.add_row(customer)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - View Customer Orders
def admin_view_customer_orders():
    try:
        admin_display_customers()
        customer_name = input("Enter Customer Name to view orders: ")
        cur.execute("SELECT Menu.ItemName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "WHERE CustomerName = %s", (customer_name,))
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Total Revenue
def admin_total_revenue():
    try:
        cur.execute("SELECT SUM(Price) FROM Menu "
                    "JOIN Orders ON Menu.ItemID = Orders.ItemID")
        total_revenue = cur.fetchone()[0]
        print(f"Total Revenue: ${total_revenue}")

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Statistics
def admin_statistics():
    try:
        cur.execute("SELECT Category, COUNT(*) FROM Menu GROUP BY Category")
        statistics = cur.fetchall()
        table = PrettyTable(["Category", "ItemCount"])
        for stat in statistics:
            table.add_row(stat)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Admin Functionalities
def admin_menu():
    while True:
        print("\n\t\t\t\tAdmin Menu")
        x = int(input('''
1. View Menu
2. Add Item to Menu
3. Update Item in Menu
4. Delete Item from Menu
5. View Orders
6. View Customer Orders
7. Display Customers
8. Total Revenue
9. Statistics
10. Exit
'''))
        if x == 1:
            display_menu()
        elif x == 2:
            add_item_to_menu()
        elif x == 3:
            admin_update_menu_item()
        elif x == 4:
            admin_delete_menu_item()
        elif x == 5:
            view_orders()
        elif x == 6:
            admin_view_customer_orders()
        elif x == 7:
            admin_display_customers()
        elif x == 8:
            admin_total_revenue()
        elif x == 9:
            admin_statistics()
        elif x == 10:
            break
        else:
            print('Please enter a valid input. ')

# Main Function
def main():
    while True:
        print("\n\t\t\t\tRestaurant Management System")
        print("1. Login as Admin")
        print("2. Customer Menu")
        print("3. Exit\n")

        user_type = input("Enter your choice (Integer only): ")

        if user_type == '1':
            admin_menu()
        elif user_type == '2':
            customer_name = input("Enter your name: ")
            while True:
                print("\n\t\t\t\tCustomer Menu")
                print("1. Display Menu")
                print("2. Place an Order")
                print("3. View My Orders")
                print("4. Exit\n")

                user_choice = input("Enter your choice: ")

                if user_choice == '1':
                    display_menu()
                elif user_choice == '2':
                    place_order(customer_name)
                elif user_choice == '3':
                    display_customer_orders(customer_name)
                elif user_choice == '4':
                    print("Thank you for using the Restaurant Management System!")
                    break
                else:
                    print("Invalid input! Please choose a valid option.")
        elif user_type == '3':
            print("Thank you for using the Restaurant Management System!")
            break
        else:
            print("Invalid input! Please choose a valid option.")

# Admin - Update Item in Menu
def admin_update_menu_item():
    try:
        display_menu()
        item_id = int(input("Enter Item ID to update: "))
        cur.execute('SELECT ItemID FROM Menu')
        data = cur.fetchall()
        if (item_id,) in data:
            new_price = int(input("Enter the new price: "))
            cur.execute("UPDATE Menu SET Price = %s WHERE ItemID = %s", (new_price, item_id))
            con.commit()
            print(f"Item ID {item_id} updated successfully!")
        else:
            print('Sorry, this item does not exist.')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Delete Item from Menu
def admin_delete_menu_item():
    try:
        display_menu()
        item_id = int(input("Enter Item ID to delete: "))
        cur.execute("DELETE FROM Menu WHERE ItemID = %s", (item_id,))
        con.commit()
        print(f"Item ID {item_id} deleted successfully!")

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Display Customers
def admin_display_customers():
    try:
        cur.execute("SELECT DISTINCT CustomerName FROM Orders")
        customers = cur.fetchall()
        table = PrettyTable(["Customer Name"])
        for customer in customers:
            table.add_row(customer)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - View Customer Orders
def admin_view_customer_orders():
    try:
        admin_display_customers()
        customer_name = input("Enter Customer Name to view orders: ")
        cur.execute("SELECT Menu.ItemName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "WHERE CustomerName = %s", (customer_name,))
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Total Revenue
def admin_total_revenue():
    try:
        cur.execute("SELECT SUM(Price) FROM Menu "
                    "JOIN Orders ON Menu.ItemID = Orders.ItemID")
        total_revenue = cur.fetchone()[0]
        print(f"Total Revenue: ${total_revenue}")

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Statistics
def admin_statistics():
    try:
        cur.execute("SELECT Category, COUNT(*) FROM Menu GROUP BY Category")
        statistics = cur.fetchall()
        table = PrettyTable(["Category", "ItemCount"])
        for stat in statistics:
            table.add_row(stat)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Admin Functionalities
def admin_menu():
    while True:
        print("\n\t\t\t\tAdmin Menu")
        x = int(input('''
1. View Menu
2. Add Item to Menu
3. Update Item in Menu
4. Delete Item from Menu
5. View Orders
6. View Customer Orders
7. Display Customers
8. Total Revenue
9. Statistics
10. Exit
'''))
        if x == 1:
            display_menu()
        elif x == 2:
            add_item_to_menu()
        elif x == 3:
            admin_update_menu_item()
        elif x == 4:
            admin_delete_menu_item()
        elif x == 5:
            view_orders()
        elif x == 6:
            admin_view_customer_orders()
        elif x == 7:
            admin_display_customers()
        elif x == 8:
            admin_total_revenue()
        elif x == 9:
            admin_statistics()
        elif x == 10:
            break
        else:
            print('Please enter a valid input. ')

# Admin - Add Item to Menu
def add_item_to_menu():
    try:
        item_name = input("Enter Item Name: ")
        category = input("Enter Category: ")
        price = int(input("Enter Price: "))

        cur.execute("INSERT INTO Menu (ItemName, Category, Price) VALUES (%s, %s, %s)",
                    (item_name, category, price))
        con.commit()
        print("Item added to the menu successfully!")

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# View Orders
def view_orders():
    try:
        cur.execute("SELECT Menu.ItemName, Orders.CustomerName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID")
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Customer Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Display Menu
def display_menu():
    try:
        cur.execute("SELECT * FROM Menu")
        menu_items = cur.fetchall()
        table = PrettyTable(["Item ID", "Item Name", "Category", "Price"])
        for item in menu_items:
            table.add_row(item)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Place Order
def place_order(customer_name):
    try:
        display_menu()
        item_id = int(input("\nEnter the Item ID to order: "))
        order_date = time.strftime('%Y-%m-%d')
        cur.execute('SELECT ItemID FROM Menu')
        data = cur.fetchall()
        if (item_id,) in data:
            cur.execute("INSERT INTO Orders (ItemID, CustomerName, OrderDate) VALUES (%s, %s, %s)",
                        (item_id, customer_name, order_date))
            print(f"\nOrder placed successfully for {customer_name}!")
            con.commit()
        else:
            print('Item not found. ')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Display Customer Orders
def display_customer_orders(customer_name):
    try:
        cur.execute("SELECT Menu.ItemName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "WHERE CustomerName = %s", (customer_name,))
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Main Function
def main():
    while True:
        print("\n\t\t\t\tRestaurant Management System")
        print("1. Login as Admin")
        print("2. Customer Menu")
        print("3. Exit\n")

        user_type = input("Enter your choice (Integer only): ")

        if user_type == '1':
            admin_menu()
        elif user_type == '2':
            customer_name = input("Enter your name: ")
            while True:
                print("\n\t\t\t\tCustomer Menu")
                print("1. Display Menu")
                print("2. Place an Order")
                print("3. View My Orders")
                print("4. Exit\n")

                user_choice = input("Enter your choice: ")

                if user_choice == '1':
                    display_menu()
                elif user_choice == '2':
                    place_order(customer_name)
                elif user_choice == '3':
                    display_customer_orders(customer_name)
                elif user_choice == '4':
                    print("Thank you for using the Restaurant Management System!")
                    break
                else:
                    print("Invalid input! Please choose a valid option.")
        elif user_type == '3':
            print("Thank you for using the Restaurant Management System!")
            break
        else:
            print("Invalid input! Please choose a valid option.")

# Display Customer Orders
def display_customer_orders(customer_name):
    try:
        cur.execute("SELECT Menu.ItemName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "WHERE CustomerName = %s", (customer_name,))
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Customer - Customer Functionalities
def customer_menu(customer_name):
    while True:
        print("\n\t\t\t\tCustomer Menu")
        print("1. Display Menu")
        print("2. Place an Order")
        print("3. View My Orders")
        print("4. Exit\n")

        user_choice = input("Enter your choice: ")

        if user_choice == '1':
            display_menu()
        elif user_choice == '2':
            place_order(customer_name)
        elif user_choice == '3':
            display_customer_orders(customer_name)
        elif user_choice == '4':
            print("Thank you for using the Restaurant Management System!")
            break
        else:
            print("Invalid input! Please choose a valid option.")

# Admin - Admin Functionalities
def admin_menu():
    while True:
        print("\n\t\t\t\tAdmin Menu")
        x = int(input('''
1. View Menu
2. Add Item to Menu
3. Update Item in Menu
4. Delete Item from Menu
5. View Orders
6. View Customer Orders
7. Display Customers
8. Total Revenue
9. Statistics
10. Exit
'''))
        if x == 1:
            display_menu()
        elif x == 2:
            add_item_to_menu()
        elif x == 3:
            admin_update_menu_item()
        elif x == 4:
            admin_delete_menu_item()
        elif x == 5:
            view_orders()
        elif x == 6:
            admin_view_customer_orders()
        elif x == 7:
            admin_display_customers()
        elif x == 8:
            admin_total_revenue()
        elif x == 9:
            admin_statistics()
        elif x == 10:
            break
        else:
            print('Please enter a valid input. ')

# Admin - Add Item to Menu
def add_item_to_menu():
    try:
        item_name = input("Enter Item Name: ")
        category = input("Enter Category: ")
        price = int(input("Enter Price: "))

        cur.execute("INSERT INTO Menu (ItemName, Category, Price) VALUES (%s, %s, %s)",
                    (item_name, category, price))
        con.commit()
        print("Item added to the menu successfully!")

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# View Orders
def view_orders():
    try:
        cur.execute("SELECT Menu.ItemName, Orders.CustomerName, Orders.OrderDate FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID")
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Customer Name", "Order Date"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Display Menu
def display_menu():
    try:
        cur.execute("SELECT * FROM Menu")
        menu_items = cur.fetchall()
        table = PrettyTable(["Item ID", "Item Name", "Category", "Price"])
        for item in menu_items:
            table.add_row(item)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Place Order
def place_order(customer_name):
    try:
        display_menu()
        item_id = int(input("\nEnter the Item ID to order: "))
        order_date = time.strftime('%Y-%m-%d')
        cur.execute('SELECT ItemID FROM Menu')
        data = cur.fetchall()
        if (item_id,) in data:
            cur.execute("INSERT INTO Orders (ItemID, CustomerName, OrderDate) VALUES (%s, %s, %s)",
                        (item_id, customer_name, order_date))
            print(f"\nOrder placed successfully for {customer_name}!")
            con.commit()
        else:
            print('Item not found. ')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Main Function
def main():
    while True:
        print("\n\t\t\t\tRestaurant Management System")
        print("1. Login as Admin")
        print("2. Customer Menu")
        print("3. Exit\n")

        user_type = input("Enter your choice (Integer only): ")

        if user_type == '1':
            admin_menu()
        elif user_type == '2':
            customer_name = input("Enter your name: ")
            customer_menu(customer_name)
        elif user_type == '3':
            print("Thank you for using the Restaurant Management System!")
            break
        else:
            print("Invalid input! Please choose a valid option.")

main()
Leave a Comment