0% found this document useful (0 votes)
17 views3 pages

PROGRAM2

The document is a Python script for a restaurant billing system that connects to a MySQL database. It includes functions to add menu items, add customers, list menu items, place orders, and generate bills. The main function provides a user interface for interacting with these features.

Uploaded by

Durga Praveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

PROGRAM2

The document is a Python script for a restaurant billing system that connects to a MySQL database. It includes functions to add menu items, add customers, list menu items, place orders, and generate bills. The main function provides a user interface for interacting with these features.

Uploaded by

Durga Praveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import mysql.

connector
from mysql.connector import Error

def connect_to_database():
return mysql.connector.connect(
host='localhost',
user='root',
password='Arya@123',
database='MuffinHouse'
)

def add_menu_item():
name = input("Enter menu item name: ")
while True:
try:
price = float(input("Enter menu item price: "))
if price <= 0:
raise ValueError("Price must be positive.")
break
except ValueError as e:
print(f"Invalid input: {e}. Please enter a valid price.")

connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("INSERT INTO menu_items (name, price) VALUES (%s, %s)", (name, price))
connection.commit()
cursor.close()
connection.close()
print("Menu item added successfully")

def add_customer():
name = input("Enter customer name: ")
phone = input("Enter customer phone number: ")
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("INSERT INTO customers (name, phone) VALUES (%s, %s)", (name, phone))
connection.commit()
cursor.close()
connection.close()
print("Customer added successfully")

def list_menu_items():
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("SELECT id, name, price FROM menu_items")
items = cursor.fetchall()
cursor.close()
connection.close()
return items

def place_order():
items = list_menu_items()
if not items:
print("No menu items available.")
return

print("Available Menu Items:")


for item in items:
print(f"ID: {item[0]}, Name: {item[1]}, Price: ${item[2]:.2f}")
order_items = []
while True:
try:
item_id = int(input("Enter menu item ID to order (or 0 to finish): "))
if item_id == 0:
break
if not any(item[0] == item_id for item in items):
print("Invalid item ID. Please choose from the available items.")
continue
quantity = int(input("Enter quantity: "))
if quantity <= 0:
print("Quantity must be positive.")
continue
item_price = next(item[2] for item in items if item[0] == item_id)
order_items.append({'id': item_id, 'price': item_price, 'quantity': quantity})
except ValueError:
print("Invalid input. Please enter a valid number.")

if not order_items:
print("No items ordered.")
return

customer_id = int(input("Enter customer ID: "))


connection = connect_to_database()
cursor = connection.cursor()
connection.start_transaction()
total = sum(item['price'] * item['quantity'] for item in order_items)
cursor.execute("INSERT INTO orders (customer_id, total) VALUES (%s, %s)", (customer_id, total))
order_id = cursor.lastrowid
for item in order_items:
cursor.execute("INSERT INTO order_items (order_id, menu_item_id, quantity) VALUES (%s, %s, %s)",
(order_id, item['id'], item['quantity']))
connection.commit()
cursor.close()
connection.close()
print(f"Order placed successfully with ID: {order_id}")

def generate_bill():
order_id = int(input("Enter order ID: "))
connection = connect_to_database()
cursor = connection.cursor()
query = """
SELECT o.id, c.name, o.order_date, o.total, mi.name, oi.quantity, mi.price
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN order_items oi ON o.id = oi.order_id
JOIN menu_items mi ON oi.menu_item_id = mi.id
WHERE o.id = %s
"""
cursor.execute(query, (order_id,))
results = cursor.fetchall()
cursor.close()
connection.close()

if results:
print(f"Bill for Order ID: {order_id}")
customer_name = results[0][1]
order_date = results[0][2]
total = results[0][3]
print(f"Customer: {customer_name}")
print(f"Order Date: {order_date}")
print("Items:")
for row in results:
item_name = row[4]
quantity = row[5]
price = row[6]
print(f" {item_name} x {quantity} @ ${price:.2f} each")

print(f"Total: ${total:.2f}")
else:
print(f"No order found with ID: {order_id}")

def main():
while True:
print("\nRestaurant Billing System")
print("1. Add Menu Item")
print("2. Add Customer")
print("3. Place Order")
print("4. Generate Bill")
print("5. Exit")
choice = input("Enter your choice: ")

if choice == '1':
add_menu_item()
elif choice == '2':
add_customer()
elif choice == '3':
place_order()
elif choice == '4':
generate_bill()
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

You might also like