viligen

pizza_delivery

Feb 25th, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. class PizzaDelivery:
  2.     def __init__(self, name, price, ingredients):
  3.         self.name = name
  4.         self.price = price
  5.         self.ingredients = ingredients
  6.         self.ordered = False
  7.  
  8.     def add_extra(self, ingredient: str, quantity: int, price_per_quantity: float):
  9.         if self.ordered:
  10.             return f"Pizza {self.name} already prepared, and we can't make any changes!"
  11.         if ingredient not in self.ingredients:
  12.             self.ingredients[ingredient] = 0
  13.         self.ingredients[ingredient] += quantity
  14.         self.price += price_per_quantity * quantity
  15.  
  16.     def remove_ingredient(self, ingredient: str, quantity: int, price_per_quantity: float):
  17.         if self.ordered:
  18.             return f"Pizza {self.name} already prepared, and we can't make any changes!"
  19.         if ingredient not in self.ingredients:
  20.             return f"Wrong ingredient selected! We do not use {ingredient} in {self.name}!"
  21.         elif self.ingredients[ingredient] < quantity:
  22.             return f"Please check again the desired quantity of {ingredient}!"
  23.         self.ingredients[ingredient] -= quantity
  24.         self.price -= price_per_quantity * quantity
  25.  
  26.     def make_order(self):
  27.         self.ordered = True
  28.         result = f"You've ordered pizza {self.name} prepared with "
  29.         # sorted_ingredients = sorted(self.ingredients.items(), key=lambda x: x[1])
  30.         result += ', '.join([f'{ingredient}: {quantity}' for ingredient, quantity in self.ingredients.items()])
  31.         result += f" and the price will be {self.price}lv."
  32.         return result
  33.  
  34.  
  35. margarita = PizzaDelivery('Margarita', 11, {'cheese': 2, 'tomatoes': 1})
  36. margarita.add_extra('mozzarella', 1, 0.5)
  37. margarita.add_extra('cheese', 1, 1)
  38. margarita.remove_ingredient('cheese', 1, 1)
  39. print(margarita.remove_ingredient('bacon', 1, 2.5))
  40. print(margarita.remove_ingredient('tomatoes', 2, 0.5))
  41. margarita.remove_ingredient('cheese', 2, 1)
  42. print(margarita.make_order())
  43. print(margarita.add_extra('cheese', 1, 1))
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment