-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathE_Commerce_System.py
More file actions
87 lines (77 loc) · 2.44 KB
/
E_Commerce_System.py
File metadata and controls
87 lines (77 loc) · 2.44 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
import csv
from datetime import datetime
FILENAME = "orders.csv"
products = {
1: {"name": "Laptop", "price": 55000},
2: {"name": "Smartphone", "price": 25000},
3: {"name": "Headphones", "price": 1500},
4: {"name": "Smartwatch", "price": 5000},
5: {"name": "Keyboard", "price": 800}
}
cart = []
def save_order(order_items, total):
with open(FILENAME, mode="a", newline="") as file:
writer = csv.writer(file)
for item in order_items:
writer.writerow([datetime.now().strftime("%Y-%m-%d %H:%M:%S"), item['name'], item['price'], item['qty'], total])
def show_products():
print("\n===== Product List =====")
for pid, info in products.items():
print(f"{pid}. {info['name']} - ₹{info['price']}")
def add_to_cart():
show_products()
pid = int(input("Enter product ID to add: "))
if pid in products:
qty = int(input("Enter quantity: "))
cart.append({"name": products[pid]["name"], "price": products[pid]["price"], "qty": qty})
print("Added to cart!")
else:
print("Invalid product ID!")
def view_cart():
if not cart:
print("Cart is empty!")
return
total = 0
print("\n===== Your Cart =====")
for item in cart:
subtotal = item['price'] * item['qty']
total += subtotal
print(f"{item['name']} x {item['qty']} = ₹{subtotal}")
print(f"Total Amount: ₹{total}")
def checkout():
if not cart:
print("Cart is empty!")
return
total = sum(item['price'] * item['qty'] for item in cart)
print(f"\nTotal Bill: ₹{total}")
confirm = input("Confirm checkout? (Y/N): ").upper()
if confirm == "Y":
save_order(cart, total)
cart.clear()
print("Order placed successfully!")
else:
print("Checkout cancelled.")
def main():
while True:
print("\n===== E-Commerce System =====")
print("1. View Products")
print("2. Add to Cart")
print("3. View Cart")
print("4. Checkout")
print("5. Exit")
choice = input("Enter choice (1-5): ")
if choice == "1":
show_products()
elif choice == "2":
add_to_cart()
elif choice == "3":
view_cart()
elif choice == "4":
checkout()
elif choice == "5":
print("Exiting... Goodbye!")
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()