-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.py
24 lines (24 loc) · 826 Bytes
/
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
class ShoppingCart(object):
def __init__(self):
self.items=[]
def add(self,item,price):
for cart_item in self.items:
if cart_item.item == item:
cart_item.q+=1
return self
self.items.append(Item(item,price))
return self
def item(self,index):
return self.items[index-1].item
def price(self,index):
return self.items[index-1].price*self.items[index-1].q
def total(self,sales_tax):
sum_price=sum([item.price*item.q for item in self.items])
return sum_price*(1.0+sales_tax/100.0)
def __len__(self):
return sum([item.q for item in self.items])
class Item(object):
def __init__(self,item,price,q=1):
self.item=item
self.price=price
self.q=q