forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffee_machine.py
83 lines (71 loc) · 2.96 KB
/
coffee_machine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from coffee import Coffee
from ingredient import Ingredient
class CoffeeMachine:
_instance = None
def __init__(self):
if CoffeeMachine._instance is not None:
raise Exception("This class is a singleton!")
else:
CoffeeMachine._instance = self
self.coffee_menu = []
self.ingredients = {}
self._initialize_ingredients()
self._initialize_coffee_menu()
@staticmethod
def get_instance():
if CoffeeMachine._instance is None:
CoffeeMachine()
return CoffeeMachine._instance
def _initialize_coffee_menu(self):
espresso_recipe = {
self.ingredients["Coffee"]: 1,
self.ingredients["Water"]: 1
}
self.coffee_menu.append(Coffee("Espresso", 2.5, espresso_recipe))
cappuccino_recipe = {
self.ingredients["Coffee"]: 1,
self.ingredients["Water"]: 1,
self.ingredients["Milk"]: 1
}
self.coffee_menu.append(Coffee("Cappuccino", 3.5, cappuccino_recipe))
latte_recipe = {
self.ingredients["Coffee"]: 1,
self.ingredients["Water"]: 1,
self.ingredients["Milk"]: 2
}
self.coffee_menu.append(Coffee("Latte", 4.0, latte_recipe))
def _initialize_ingredients(self):
self.ingredients["Coffee"] = Ingredient("Coffee", 10)
self.ingredients["Water"] = Ingredient("Water", 10)
self.ingredients["Milk"] = Ingredient("Milk", 10)
def display_menu(self):
print("Coffee Menu:")
for coffee in self.coffee_menu:
print(f"{coffee.get_name()} - ${coffee.get_price()}")
def select_coffee(self, coffee_name):
for coffee in self.coffee_menu:
if coffee.get_name().lower() == coffee_name.lower():
return coffee
return None
def dispense_coffee(self, coffee, payment):
if payment.get_amount() >= coffee.get_price():
if self._has_enough_ingredients(coffee):
self._update_ingredients(coffee)
print(f"Dispensing {coffee.get_name()}...")
change = payment.get_amount() - coffee.get_price()
if change > 0:
print(f"Please collect your change: ${change}")
else:
print(f"Insufficient ingredients to make {coffee.get_name()}")
else:
print(f"Insufficient payment for {coffee.get_name()}")
def _has_enough_ingredients(self, coffee):
for ingredient, required_quantity in coffee.get_recipe().items():
if ingredient.get_quantity() < required_quantity:
return False
return True
def _update_ingredients(self, coffee):
for ingredient, required_quantity in coffee.get_recipe().items():
ingredient.update_quantity(-required_quantity)
if ingredient.get_quantity() < 3:
print(f"Low inventory alert: {ingredient.get_name()}")