Guest User

Untitled

a guest
Jun 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class ShoppingCart(object):
  2. def __init__(self):
  3. self.items = []
  4.  
  5. def add(self, item, price):
  6. for cart_item in self.items:
  7. # Since we found the item, we increment
  8. # instead of append
  9. if cart_item.item == item:
  10. cart_item.q += 1
  11. return self
  12.  
  13. # If we didn't find, then we append
  14. self.items.append(Item(item, price))
  15. return self
  16.  
  17. def item(self, index):
  18. return self.items[index-1].item
  19.  
  20. def price(self, index):
  21. return self.items[index-1].price * self.items[index-1].q
  22.  
  23. def total(self, sales_tax):
  24. sum_price = sum([item.price*item.q for item in self.items])
  25. return sum_price*(1.0 + sales_tax/100.0)
  26.  
  27. def __len__(self):
  28. return sum([item.q for item in self.items])
  29.  
  30. class Item(object):
  31. def __init__(self, item, price, q=1):
  32. self.item = item
  33. self.price = price
  34. self.q = q
Add Comment
Please, Sign In to add comment