Created
November 14, 2022 12:26
-
-
Save Bloofer/4e135c70bf2328df70885797fb86494e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
from collections import namedtuple | |
Customer = namedtuple('Customer', 'named fidelity') | |
class LineItem: | |
def __init__(self, product, quantity, price): | |
self.product = product | |
self.quantity = quantity | |
self.price = price | |
def total(self): | |
return self.price * self.quantity | |
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(self) | |
return self.total() - discount | |
def __repr__(self): | |
fmt = '<Order total: {:.2f} due: {:.2f}>' | |
return fmt.format(self.total(), self.due()) | |
def fidelity_promo(order): | |
return order.total() * .05 if order.customer.fidelity >= 1000 else 0 | |
def bulk_item_promo(order): | |
discount = 0 | |
for item in order.cart: | |
if item.quantity >= 20: | |
discount += item.total() * .1 | |
return discount | |
def large_order_promo(order): | |
distinct_items = {item.product for item in order.cart} | |
if len(distinct_items) >= 10: | |
return order.total() * .07 | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment