-
Notifications
You must be signed in to change notification settings - Fork 12
/
easy_cart.py
58 lines (46 loc) · 1.16 KB
/
easy_cart.py
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
cart = [
{
"name": "pencil",
"price": 0.25,
"quantity": 144
},
{
"name": "paper",
"price": 0.05,
"quantity": 10000
},
]
class CartItem:
def __init__(self, item_dict):
self.name = item_dict["name"]
self.price = item_dict["price"]
self.quantity = item_dict["quantity"]
def get_subtotal(self):
return self.price * self.quantity
def __str__(self):
return f"{self.quantity} {self.name} @${self.price} ${self.get_subtotal():.2f}"
class Cart:
def __init__(self, cart_list):
self.items = []
for item_dict in cart_list:
self.items.append(CartItem(item_dict))
def display(self):
print("CART ITEMS:")
total = 0
for item in self.items:
print(f"\t{item}")
total += item.get_subtotal()
print(f"TOTAL: ${total:.2f}")
c = Cart(cart)
c.display()
# SOLVE
# 1. with script first,
# 2. then extra credit use a function on the cart,
# 3. extra extra credit use Product and Cart classes
"""
OUTPUT EXAMPLE:
CART ITEMS:
144 pencil @$0.25 $36.00
10000 paper @$0.05 $500.00
TOTAL: $536.00
"""