0% found this document useful (0 votes)
28 views2 pages

Cafe Management Code

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

Cafe Management Code

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

import mysql.

connector

# Connect to MySQL
def connect():
return mysql.connector.connect(
host="localhost",
user="root",
password="Tiger",
database="cafe_db"
)

# Create table if not exists


def create_table(cursor):
return
mysql.connector.connect(host="localhost",user="root",password="Tiger",database="sto
ck_management")
cursor.execute("CREATE TABLE IF NOT EXISTS menu (id INT AUTO_INCREMENT PRIMARY
KEY, name VARCHAR(255), quantity INT)")

# Add new item


def add_item(cursor):

mysql.connector.connect(host="localhost",user="root",password="Tiger",database="sto
ck_management")
name = input("Enter item name: ")
quantity = int(input("Enter quantity: "))
price=float(input("Enter price:"))
cursor.execute("INSERT INTO menu(name, quantity,price)VALUES(%s ,%s,%s)",(name,
quantity,price))
print("Item added successfully")

# Update existing item


def update_item(cursor):

mysql.connector.connect(host="localhost",user="root",password="Tiger",database="sto
ck_management")
item_id = int(input("Enter item ID to update: "))
new_quantity = int(input("Enter new quantity: "))
new_price=float(input("enter the new price:"))
cursor.execute("UPDATE menu SET quantity = %s WHERE id = %s", (new_quantity,
item_id))
cursor.execute("UPDATE menu SET price= %s WHERE id = %s", (new_price, item_id))
print("Item updated successfully")

# Delete item
def delete_item(cursor):

mysql.connector.connect(host="localhost",user="root",password="Tiger",database="sto
ck_management")
item_id = int(input("Enter item ID to delete: "))
cursor.execute("DELETE FROM menu WHERE id = %s", (item_id,))
print("Item deleted successfully")

# View all items


def view_items(cursor):

mysql.connector.connect(host="localhost",user="root",password="Tiger",database="sto
ck_management")
cursor.execute("SELECT * FROM menu")
menu = cursor.fetchall()
count=cursor.rowcount
print("total number of rows retrieved in resultset:",count)
if not menu:
print("No items found")
else:
for item in menu:
print(f"ID: {item[0]}, Name: {item[1]}, Quantity: {item[2]},price:
{item[3]}")

# Main menu
def main_menu():
print("\n Cafe Management System")
print("1.Add Items")
print("2. Update order")
print("3. Delete Item")
print("4. View Items")
print("5. Exit")

# Main function
def main():
conn = connect()
cursor = conn.cursor()
create_table(cursor)
while True:
main_menu()
choice = input("Enter your choice: ")
if choice == "1":
add_item(cursor)
elif choice == "2":
update_item(cursor)
elif choice == "3":
delete_item(cursor)
elif choice == "4":
view_items(cursor)
elif choice == "5":
break
else:
print("Invalid choice")
conn.commit()
conn.close()
print("Thank you for using the Cafe Management System")

if __name__ == "__main__":
main()

You might also like