-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducts.py
More file actions
77 lines (62 loc) · 2 KB
/
products.py
File metadata and controls
77 lines (62 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from storage import load_products, save_products
from utils import pause, get_menu_choice, get_positive_number, get_non_negative_int
def product_menu():
while True:
print("\nProduct Management")
print("-" * 18)
print("1. Add Product")
print("2. View Products")
print("3. Delete Product")
print("4. Back to Main Menu")
choice = get_menu_choice(1, 4)
if choice == 1:
add_product()
elif choice == 2:
view_products()
elif choice == 3:
delete_product()
else:
break
def add_product():
df = load_products()
product_id = input("Enter Product ID: ")
if product_id in df["ID"].values:
print("Product ID already exists")
pause()
return
name = input("Enter Product Name: ")
category = input("Enter Category: ")
price = get_positive_number("Enter Price: ")
qty = get_non_negative_int("Enter Quantity: ")
df.loc[len(df)] = [product_id, name, category, price, qty]
save_products(df)
print("Product added successfully")
pause()
def view_products():
df = load_products()
if df.empty:
print("\nNo products found.")
else:
print("\nPRODUCT LIST")
print("-" * 60)
print(f"{'ID':<8}{'Name':<15}{'Category':<15}{'Price':<10}{'Qty'}")
print("-" * 60)
for _, row in df.iterrows():
print(f"{row.ID:<8}{row.Name:<15}{row.Category:<15}{row.Price:<10}{row.Quantity}")
pause()
def delete_product():
df = load_products()
df["ID"] = df["ID"].astype(str)
pid = input("Enter Product ID to delete: ").strip()
if pid not in df["ID"].values:
print(" Product not found")
pause()
return
confirm = input("Are you sure you want to delete this product? (y/n): ").lower()
if confirm == "y":
df = df[df["ID"] != pid]
save_products(df)
print("Product deleted successfully")
else:
print("Cancelled")
pause()