Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
SQL Queries
CREATE DATABASE restaurant_db;
USE restaurant_db;
CREATE TABLE customers (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
contact VARCHAR(15)
);
CREATE TABLE menu_items (
item_id INT AUTO_INCREMENT PRIMARY KEY,
item_name VARCHAR(100),
price DECIMAL(10,2)
);
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
CREATE TABLE order_items (
order_item_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT,
item_id INT,
quantity INT,
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (item_id) REFERENCES menu_items(item_id)
);
CREATE TABLE payments (
payment_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT,
payment_date DATE,
amount DECIMAL(10,2),
method VARCHAR(50),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
Program 1
# **************RestaurantBillingSystem********************
import mysql.connector
from datetime import date
connection = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="rest_billing_system"
)
cursor = connection.cursor()
def show_menu():
cursor.execute("SELECT item_id, name, price FROM menu_items")
print("Menu Items:")
for item_id, name, price in cursor.fetchall():
print(f"{item_id}: {name} - Rs:{price}")
print("-------------------------------------------------------------")
def take_order():
show_menu()
order_items = [] # Order List (Empty)
while True:
item_id = input("Enter Item ID (or 'done' to finish): ")
if item_id.lower() == 'done':
break
qty = int(input("Enter quantity: "))
order_items.append((int(item_id), qty))
return order_items
def process_order(order_items):
cname=input("Enter Customer Name:")
cphone=input("Enter Customer Contact:")
cursor.execute("INSERT INTO customers (name, phone) VALUES(%s,%s)", (cname, cphone))
#cursor.execute("INSERT INTO customers (name, phone) VALUES ('Walk-in', 'N/A')")
customer_id = cursor.lastrowid
cursor.execute("INSERT INTO orders (customer_id, order_date) VALUES (%s, %s)", (customer_id, date.today()))
order_id = cursor.lastrowid
total = 0.0
for item_id, qty in order_items:
cursor.execute("SELECT price FROM menu_items WHERE item_id = %s", (item_id,))
price = float(cursor.fetchone()[0])
total=total+(price * qty)
cursor.execute("INSERT INTO order_items (order_id, item_id, quantity) VALUES (%s, %s, %s)", (order_id, item_id, qty))
print(f"-----Total Bill: Rs {total}")
method = input("Enter payment method (Cash/Card/UPI): ")
cursor.execute("INSERT INTO payments (order_id, amount_paid, payment_method,payment_date) VALUES (%s, %s, %s, %s)",
(order_id, total, method,date.today()))
connection.commit()
print("Order placed successfully!")
print(f"Total Bill: Rs {total}")
def main():
while True:
print("*****Restaurant Billing System*****")
print("\n1. Show Menu")
print("2. Take New Order")
print("3. Exit")
choice = input("Enter choice: ")
if choice == '1':
show_menu()
elif choice == '2':
items = take_order()
process_order(items)
elif choice == '3':
break
if __name__ == "__main__":
main()