def is_applicable( self, order: Order ) -> bool: """ Check if the promotion 15% burrimenu is applicable. The same number of hamburgers, drinks and chips. :param Order order: order :return boolean: is promotion applicable """ return len(order.hamburgers()) == len(order.chips()) == len(order.drinks())
def apply( self, order: Order ): """ Apply the promotion euromania. :param Order order: order """ for chips in order.chips(): chips().set_price(price=1.0) order.promotions().append('euromania')
def apply( self, order: Order ): """ Apply the promotion burrimenu. 15% hamburgers. :param Order order: order :return order: order with promotion applied """ for hamburger in order.hamburgers(): hamburger.set_price(price=hamburger.price() * (100 - 15) / 100) order.promotions().append('burrimenu')
def is_applicable(self, order: Order) -> bool: """ Check if the promotion jarramania is applicable. - 2 burribeers and 1 chips. - Everyday to 30-09-2020. Between 16-19.30 h. :param Order order: order :return boolean: is promotion applicable """ if len(order.drinks()) != 2 \ and [drink.type() != 'burribeer' for drink in order.drinks()] \ and len(order.chips()) != 1: return False day_before = datetime.utcnow() <= datetime.strptime( '2020-09-30', '%Y-%m-%d') hour_between = datetime.strptime('16:00:00', '%H:%M:%S').time() \ <= datetime.now().time() \ <= datetime.strptime('19:30:00', '%H:%M:%S').time() return day_before is True and hour_between is True
def transform_request(self, request: ServerRequestInterface) -> Order: return Order( barista=request.parsed_body().get('barista'), hamburgers=[ Hamburger( bread_type=hamburger.get('bread_type'), meats=[ Meat( type=meat.get('type'), size=meat.get('size'), cooked=meat.get('cooked'), ) for meat in hamburger.get('meats') if hamburger.get('meats') ], extra_tomato=ExtraTomato( type=hamburger.get('extra_tomato').get('type') if (hamburger.get('extra_tomato') and hamburger.get('extra_tomato').get('type')) else None, price=hamburger.get('extra_tomato').get('price') if (hamburger.get('extra_tomato') and hamburger.get('extra_tomato').get('price')) else None, ), extra_cheese=ExtraCheese( type=hamburger.get('extra_cheese').get('type') if (hamburger.get('extra_cheese') and hamburger.get('extra_cheese').get('type')) else None, price=hamburger.get('extra_cheese').get('price') if (hamburger.get('extra_cheese') and hamburger.get('extra_cheese').get('price')) else None, ), ) for hamburger in request.parsed_body().get('hamburgers') if request.parsed_body().get('hamburgers') ], chips=[ Chips( type=chips.get('type'), size=chips.get('size'), ) for chips in request.parsed_body().get('chips') if request.parsed_body().get('chips') ], drinks=[ Drink(type=drink.get('type'), ) for drink in request.parsed_body().get('drinks') if request.parsed_body().get('drinks') ], )
def normalize(order: Order) -> Dict[str, Union[str, int]]: return dict( barista=order.barista() or None, hamburgers=[ HamburgerNormalizer.normalize(hamburger=hamburger) for hamburger in order.hamburgers() ], chips=[ ChipsNormalizer.normalize(chips=chips) for chips in order.chips() ], drinks=[ DrinkNormalizer.normalize(drink=drink) for drink in order.drinks() ], price=order.price(), dt_created=order.dt_created(), is_ready=order.is_ready(), promotions=order.promotions(), )
def hydrate(order: Dict) -> Order: return Order( barista=order.get('barista'), hamburgers=[ MongoDBHamburgerHydration.hydrate(hamburger=hamburger) for hamburger in order.get('hamburgers') if order.get('hamburgers') ], chips=[ MongoDBChipsHydration.hydrate(chips=chips) for chips in order.get('chips') if order.get('chips') ], drinks=[ MongoDBDrinkHydration.hydrate(drink=drink) for drink in order.get('drinks') if order.get('drinks') ], price=order.get('price'), dt_created=order.get('dt_created'), is_ready=order.get('is_ready'), promotions=order.get('promotions'), )
def calculate( self, order: Order ) -> float: """ Calculate the total price of all items. :param Order order: order :return float: price """ for drink in order.drinks(): drink.set_price(price=PriceDrink().calculate(drink=drink)) for chips in order.chips(): chips.set_price(price=PriceChips().calculate(chips=chips)) for hamburger in order.hamburgers(): hamburger.set_price(price=PriceHamburger().calculate(hamburger=hamburger)) if PromotionBurrimenu().is_applicable(order=order): PromotionBurrimenu().apply(order=order) if PromotionEuromania().is_applicable(order=order): PromotionEuromania().apply(order=order) total = 0.0 if PromotionJarramania().is_applicable(order=order): total = 3.0 PromotionJarramania().apply(order=order) else: for item in order.hamburgers() + order.drinks() + order.chips(): total += item.price() order.set_price(price=total) return total
def apply(self, order: Order): order.promotions().append('jarramania')