-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
114 lines (80 loc) · 2.7 KB
/
Copy pathcli.py
File metadata and controls
114 lines (80 loc) · 2.7 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
cli.py - Command Line Interface for the E-Commerce Cart System
"""
from src.models import Product, ShoppingCart, NoDiscount, PercentageDiscount
def display_menu():
print("\n===== E-Commerce Cart Menu =====")
print("1. Add Product to Cart")
print("2. Remove Product from Cart")
print("3. View Cart")
print("4. Checkout")
print("5. Exit")
def main():
# Sample product catalog
catalog = {
"LAP123": Product("LAP123", "Laptop", 1000.0),
"MOU456": Product("MOU456", "Mouse", 50.0),
"KEY789": Product("KEY789", "Keyboard", 80.0)
}
print("Choose Pricing Strategy")
print("1. No Discount")
print("2. 10% Discount")
strategy_choice = input("Enter choice: ")
if strategy_choice == "2":
strategy = PercentageDiscount(10)
else:
strategy = NoDiscount()
cart = ShoppingCart(strategy)
while True:
display_menu()
choice = input("Enter option: ")
# Add product
if choice == "1":
sku = input("Enter product SKU: ")
if sku not in catalog:
print("Product not found.")
continue
try:
qty = int(input("Enter quantity: "))
cart.add(catalog[sku], qty)
print("Product added to cart.")
except ValueError as e:
print("Error:", e)
# Remove product
elif choice == "2":
sku = input("Enter product SKU to remove: ")
try:
cart.remove(sku)
print("Product removed.")
except KeyError:
print("Product not found in cart.")
# View cart
elif choice == "3":
items = cart.items()
if not items:
print("Cart is empty.")
continue
print("\nItems in Cart:")
for item in items:
print(f"{item.product.name} | Qty: {item.qty} | Subtotal: ${item.subtotal()}")
print("Cart Subtotal:", cart.subtotal())
# Checkout
elif choice == "4":
items = cart.get_items()
if not items:
print("Cart is empty.")
continue
print("\n===== Receipt =====")
for item in items:
print(f"{item.product.name} x {item.qty} = ${item.subtotal()}")
print("-----------------------")
print("Subtotal:", cart.subtotal())
print("Total after discount:", cart.total())
# Exit
elif choice == "5":
print("Thank you for shopping!")
break
else:
print("Invalid option.")
if __name__ == "__main__":
main()