import mysql.connector
# Function to connect to the database
def connect_db():
return mysql.connector.connect(
host="localhost",
user="root",
password="",
database="food"
)
# Function to add a menu item
def add_item(cursor, item_name, item_price):
try:
cursor.execute("INSERT INTO item (Food_Item, Food_Price) VALUES (%s, %s)", (item_name, item_price))
print(f"{item_name} has been added to the menu with a price of {item_price}.")
except mysql.connector.Error as err:
print(f"Error: {err}")
# Function to display the menu
def display_menu(cursor):
cursor.execute("SELECT * FROM item")
items = cursor.fetchall()
print("********** Cafe Menu **********")
for item in items:
print(f"ID: {item[0]}, Food Item: {item[1]}, Price: {item[2]}")
print("*******************************")
# Function to place an order
def place_order(cursor, customer_name, item_id, quantity):
cursor.execute("SELECT * FROM item WHERE S_no = %s", (item_id,))
item = cursor.fetchone()
if item:
item_name = item[1]
item_price = item[2]
total_price = item_price * quantity
# Insert order into the orders table
cursor.execute("INSERT INTO orders (O_name, O_price, Quantity, Total_Price) VALUES (%s, %s, %s, %s)",
(item_name, item_price, quantity, total_price))
print("\nYour order has been placed successfully.")
print(f"Food: {item_name}, Quantity: {quantity}, Total Price: {total_price}")
else:
print("Item not found in the menu.")
# Function to view all orders
def view_orders(cursor):
cursor.execute("SELECT * FROM orders")
orders = cursor.fetchall()
print("\n********** Order History **********")
for order in orders:
print(f"Order ID: {order[0]}, Food Name: {order[1]}, Price: {order[2]}, Quantity: {order[3]}, Total Price: {order[4]}")
print("***********************************")
# Main function to control the flow of the Cafe Bill Management System
def main():
conn = connect_db()
cursor = conn.cursor()
while True:
print("\n********** WELCOME TO OPAL CAFE **********")
print("1. Add Menu Item")
print("2. View Menu")
print("3. Place an Order")
print("4. View Orders")
print("5. Exit")
choice = input("Please select an option (1-5): ")
if choice == "1":
item_name = input("Enter food item name: ")
item_price = float(input("Enter food price: "))
add_item(cursor, item_name, item_price)
conn.commit()
elif choice == "2":
display_menu(cursor)
elif choice == "3":
customer_name = input("Enter your name: ")
item_id = int(input("Enter the item ID you want to order: "))
quantity = int(input("Enter the quantity: "))
place_order(cursor, customer_name, item_id, quantity)
conn.commit()
elif choice == "4":
view_orders(cursor)
elif choice == "5":
print("Thank you for visiting our cafe!")
break
else:
print("Invalid option, please try again.")
cursor.close()
conn.close()
# Run the program
if __name__ == "__main__":
main()