Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #working backwards, worked on another decorator class lined out in fluent, which refers to an earlier promotion class implemenation
- #which is being refactored by the decorator class. Will have to come back to review, didn't make it in time for work
- promos = []
- def promotion(promo_func):
- promos.append(promo_func)
- return promo_func
- @promotion
- def fidelity(order):
- """5% discount for customers with 1000 or more fidelity points"""
- return order.total() * .05 if order.customer.fidelity >= 1000 else 0
- @promotion
- def bulk_item(order):
- """10% discount for each LineItem with 20 or more units"""
- discount = 0
- for item in order.cart:
- if item.quantity >= 20:
- discount += item.total() * .1
- return discount
- @promotion
- def large_order(order):
- """7% discount for orders with 10 or more distinct items"""
- distinct_items = {item.product for item in ordr.cart}
- if len(distinct_items) >= 10:
- return order.total() * .07
- return 0
- def best_promo(order):
- """Select best discount available
- """
- return max(promo(order) for promo in promos)
- class Order:
- def __init__(self, customer, cart, promotion=None):
- self.customer = customer
- self.cart = list(cart)
- self.promotion = promotion
- def total(self):
- if not hasattr(self, '__total'):
- self.__total = sum(item.total() for item in self.cart)
- return self.__total
- def due(self):
- if self.promotion is None:
- discount = 0
- else:
- discount = self.promotion.discount(self)
- return self.total() - discount
- def __repr__(self):
- fmt = '<Order total: {:.2f} due: {:.2f}>'
- return fmt.format(self.total(), self.due())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement