import mysql.
connector
from datetime import datetime
conn = mysql.connector.connect(
host="localhost",
user="root",
password="rud123",
database="grocery_shop"
)
cursor = conn.cursor()
def add_product():
name = input("Enter product name: ")
price = float(input("Enter product price:
"))
stock = int(input("Enter stock quantity:
"))
cursor.execute("INSERT INTO products
(name, price, stock) VALUES (%s, %s, %s)",
(name, price, stock))
conn.commit()
print(" Product added successfully!")
def view_products():
cursor.execute("SELECT * FROM
products")
rows = cursor.fetchall()
print("\n--- Product List ---")
print("ID | Name | Price | Stock")
for row in rows:
print(row[0], "|", row[1], "|", row[2],
"|", row[3])
def update_product():
view_products()
pid = int(input("Enter product ID to
update: "))
new_price = float(input("Enter new
price: "))
new_stock = int(input("Enter new stock:
"))
cursor.execute("UPDATE products SET
price=%s, stock=%s WHERE id=%s",
(new_price, new_stock, pid))
conn.commit()
print(" Product updated successfully!")
def delete_product():
view_products()
pid = int(input("Enter product ID to
delete: "))
cursor.execute("DELETE FROM products
WHERE id=%s", (pid,))
conn.commit()
print("Product deleted successfully!")
def generate_bill(cart):
print("\n=========== CUSTOMER
BILL ===========")
print("Date:",
datetime.now().strftime("%Y-%m-%d %H:
%M:%S"))
print("-------------------------------------")
print("Product Qty Price Total")
print("-------------------------------------")
subtotal = 0
for item in cart:
pname, qty, price = item
total = qty * price
subtotal += total
print(f"{pname:12} {qty:<5}
{price:<7} {total}")
tax = round(subtotal * 0.05, 2) # 5%
tax
grand_total = subtotal + tax
print("-------------------------------------")
print(f"Subtotal: Rs.{subtotal}")
print(f"Tax (5%): Rs.{tax}")
print(f"Grand Total: Rs.{grand_total}")
print("=========== THANK YOU!
=============")
def billing_system():
cart = []
while True:
view_products()
pid = int(input("Enter product ID to
buy (0 to checkout): "))
if pid == 0:
break
qty = int(input("Enter quantity: "))
cursor.execute("SELECT name, price,
stock FROM products WHERE id=%s",
(pid,))
result = cursor.fetchone()
if result:
pname, price, stock = result
if qty <= stock:
cart.append((pname, qty, price))
# update stock
cursor.execute("UPDATE
products SET stock=stock-%s WHERE id=
%s", (qty, pid))
# record sale
total = price * qty
cursor.execute("INSERT INTO
sales (product_id, quantity, total) VALUES
(%s, %s, %s)",
(pid, qty, total))
conn.commit()
print(f" Added {qty} {pname}(s)
to cart.")
else:
print(" Not enough stock!")
else:
print(" Product not found!")
if cart:
generate_bill(cart)
else:
print(" Cart is empty, no bill
generated!")
def view_sales():
cursor.execute("SELECT * FROM sales")
rows = cursor.fetchall()
print("\n--- Sales Records ---")
print("SaleID | ProductID | Qty | Total |
Date")
for row in rows:
print(row)
while True:
print("\n=== Grocery Shop
Management ===")
print("1. Add Product")
print("2. View Products")
print("3. Update Product")
print("4. Delete Product")
print("5. Billing System")
print("6. View Sales")
print("7. Exit")
choice = input("Enter choice: ")
if choice == "1":
add_product()
elif choice == "2":
view_products()
elif choice == "3":
update_product()
elif choice == "4":
delete_product()
elif choice == "5":
billing_system()
elif choice == "6":
view_sales()
elif choice == "7":
print("Exiting... Thank you!")
break
else:
print(" Invalid choice, try again!")
Output ................