Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #working backwards, worked on another decorator class lined out in fluent, which refers to an earlier promotion class implemenation
  2. #which is being refactored by the decorator class. Will have to come back to review, didn't make it in time for work
  3.  
  4. promos = []
  5. def promotion(promo_func):
  6. promos.append(promo_func)
  7. return promo_func
  8. @promotion
  9. def fidelity(order):
  10. """5% discount for customers with 1000 or more fidelity points"""
  11. return order.total() * .05 if order.customer.fidelity >= 1000 else 0
  12. @promotion
  13. def bulk_item(order):
  14. """10% discount for each LineItem with 20 or more units"""
  15. discount = 0
  16. for item in order.cart:
  17. if item.quantity >= 20:
  18. discount += item.total() * .1
  19. return discount
  20. @promotion
  21. def large_order(order):
  22. """7% discount for orders with 10 or more distinct items"""
  23. distinct_items = {item.product for item in ordr.cart}
  24. if len(distinct_items) >= 10:
  25. return order.total() * .07
  26. return 0
  27. def best_promo(order):
  28. """Select best discount available
  29. """
  30. return max(promo(order) for promo in promos)
  31.  
  32.  
  33. class Order:
  34. def __init__(self, customer, cart, promotion=None):
  35. self.customer = customer
  36. self.cart = list(cart)
  37. self.promotion = promotion
  38. def total(self):
  39. if not hasattr(self, '__total'):
  40. self.__total = sum(item.total() for item in self.cart)
  41. return self.__total
  42. def due(self):
  43. if self.promotion is None:
  44. discount = 0
  45. else:
  46. discount = self.promotion.discount(self)
  47. return self.total() - discount
  48. def __repr__(self):
  49. fmt = '<Order total: {:.2f} due: {:.2f}>'
  50. return fmt.format(self.total(), self.due())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement