fork download
  1. import mysql.connector
  2.  
  3. # Function to connect to the database
  4. def connect_db():
  5. return mysql.connector.connect(
  6. host="localhost",
  7. user="root",
  8. password="",
  9. database="food"
  10. )
  11.  
  12. # Function to add a menu item
  13. def add_item(cursor, item_name, item_price):
  14. try:
  15. cursor.execute("INSERT INTO item (Food_Item, Food_Price) VALUES (%s, %s)", (item_name, item_price))
  16. print(f"{item_name} has been added to the menu with a price of {item_price}.")
  17. except mysql.connector.Error as err:
  18. print(f"Error: {err}")
  19.  
  20. # Function to display the menu
  21. def display_menu(cursor):
  22. cursor.execute("SELECT * FROM item")
  23. items = cursor.fetchall()
  24. print("********** Cafe Menu **********")
  25. for item in items:
  26. print(f"ID: {item[0]}, Food Item: {item[1]}, Price: {item[2]}")
  27. print("*******************************")
  28.  
  29. # Function to place an order
  30. def place_order(cursor, customer_name, item_id, quantity):
  31. cursor.execute("SELECT * FROM item WHERE S_no = %s", (item_id,))
  32. item = cursor.fetchone()
  33.  
  34. if item:
  35. item_name = item[1]
  36. item_price = item[2]
  37. total_price = item_price * quantity
  38.  
  39. # Insert order into the orders table
  40. cursor.execute("INSERT INTO orders (O_name, O_price, Quantity, Total_Price) VALUES (%s, %s, %s, %s)",
  41. (item_name, item_price, quantity, total_price))
  42. print("\nYour order has been placed successfully.")
  43. print(f"Food: {item_name}, Quantity: {quantity}, Total Price: {total_price}")
  44. else:
  45. print("Item not found in the menu.")
  46.  
  47. # Function to view all orders
  48. def view_orders(cursor):
  49. cursor.execute("SELECT * FROM orders")
  50. orders = cursor.fetchall()
  51. print("\n********** Order History **********")
  52. for order in orders:
  53. print(f"Order ID: {order[0]}, Food Name: {order[1]}, Price: {order[2]}, Quantity: {order[3]}, Total Price: {order[4]}")
  54. print("***********************************")
  55.  
  56. # Main function to control the flow of the Cafe Bill Management System
  57. def main():
  58. conn = connect_db()
  59. cursor = conn.cursor()
  60.  
  61. while True:
  62. print("\n********** WELCOME TO OPAL CAFE **********")
  63. print("1. Add Menu Item")
  64. print("2. View Menu")
  65. print("3. Place an Order")
  66. print("4. View Orders")
  67. print("5. Exit")
  68. choice = input("Please select an option (1-5): ")
  69.  
  70. if choice == "1":
  71. item_name = input("Enter food item name: ")
  72. item_price = float(input("Enter food price: "))
  73. add_item(cursor, item_name, item_price)
  74. conn.commit()
  75.  
  76. elif choice == "2":
  77. display_menu(cursor)
  78.  
  79. elif choice == "3":
  80. customer_name = input("Enter your name: ")
  81. item_id = int(input("Enter the item ID you want to order: "))
  82. quantity = int(input("Enter the quantity: "))
  83. place_order(cursor, customer_name, item_id, quantity)
  84. conn.commit()
  85.  
  86. elif choice == "4":
  87. view_orders(cursor)
  88.  
  89. elif choice == "5":
  90. print("Thank you for visiting our cafe!")
  91. break
  92.  
  93. else:
  94. print("Invalid option, please try again.")
  95.  
  96. cursor.close()
  97. conn.close()
  98.  
  99. # Run the program
  100. if __name__ == "__main__":
  101. main()
  102.  
Success #stdin #stdout 0.03s 25564KB
stdin
Standard input is empty
stdout
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()