MeGaDeTH_91

Untitled

Apr 5th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. from project.factory.factory import Factory
  2.  
  3.  
  4. class ChocolateFactory(Factory):
  5. def __init__(self, name: str, capacity: int):
  6. super().__init__(name, capacity)
  7. self.recipes = {}
  8. self.products = {}
  9.  
  10. def add_ingredient(self, ingredient_type: str, quantity: int):
  11. allowed_ingredients = ["white chocolate", "dark chocolate", "milk chocolate", "sugar"]
  12.  
  13. if not self.can_add(quantity):
  14. raise ValueError("Not enough space in factory")
  15. elif ingredient_type not in allowed_ingredients:
  16. raise TypeError(f'Ingredient of type {ingredient_type} not allowed in {self.__class__.__name__}')
  17.  
  18. if ingredient_type not in self.ingredients:
  19. self.ingredients[ingredient_type] = 0
  20.  
  21. self.ingredients[ingredient_type] += quantity
  22.  
  23. def remove_ingredient(self, ingredient_type: str, quantity: int):
  24. if ingredient_type not in self.ingredients:
  25. raise KeyError("No such product in the factory")
  26. elif self.ingredients[ingredient_type] < quantity:
  27. raise ValueError("Ingredient quantity cannot be less than zero")
  28.  
  29. self.ingredients[ingredient_type] -= quantity
  30.  
  31. def add_recipe(self, recipe_name: str, recipe: dict):
  32. self.recipes[recipe_name] = recipe
  33.  
  34. def make_chocolate(self, recipe_name: str):
  35. if recipe_name not in self.recipes:
  36. raise TypeError("No such recipe")
  37.  
  38. if recipe_name not in self.products:
  39. self.products[recipe_name] = 0
  40.  
  41. self.products[recipe_name] += 1
Advertisement
Add Comment
Please, Sign In to add comment