-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcomposite.py
178 lines (151 loc) · 4.32 KB
/
composite.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# package headfirst.designpatterns.iterator.dinermergercafe
class MenuComponent:
def add(self, menu_component):
raise NotImplementedError
def remove(self, menu_component):
raise NotImplementedError
def get_child(self, i: int):
raise NotImplementedError
def print(self):
raise NotImplementedError
class MenuItem(MenuComponent):
def __init__(
self,
name: str,
description: str,
vegetarian: bool,
price: float,
):
self.name = name
self.description = description
self.vegetarian = vegetarian
self.price = price
def print(self):
print(" " + self.name + "(v)" * self.vegetarian + ", " + str(self.price))
print(" -- " + self.description)
class Menu(MenuComponent):
name: str
description: str
def __init__(self, name: str, description: str):
self.menu_components: list[MenuComponent] = []
self.name = name
self.description = description
def add(self, menu_component: MenuComponent):
self.menu_components.append(menu_component)
def remove(self, menu_component: MenuComponent):
self.menu_components.remove(menu_component)
def get_child(self, i: int):
return self.menu_components[i]
def print(self):
print('\n' + self.name + ", " + self.description)
print('--------------------')
for menu_component in self.menu_components:
menu_component.print()
def __iter__(self):
raise NotImplementedError
class CafeMenu(Menu):
def __iter__(self):
return iter(self.menu_items.values())
class DinerMenu(Menu):
def __iter__(self):
return iter(self.menu_items)
class Waitress:
def __init__(self, all_menus: MenuComponent):
self.all_menus = all_menus
def print_menu(self):
self.all_menus.print()
def menu_test_drive():
cafe_menu = CafeMenu('Cafe Menu', 'For Dinner')
diner_menu = DinerMenu('Diner Menu', 'For Lunch')
all_menus: MenuComponent = Menu('all menus', 'all menus')
all_menus.add(cafe_menu)
all_menus.add(diner_menu)
cafe_menu.add(
MenuItem(
"Veggie Burger and Air Fries",
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
True,
3.99,
)
)
cafe_menu.add(
MenuItem(
"Soup of the day",
"A cup of the soup of the day, with a side salad",
False,
3.69,
)
)
cafe_menu.add(
MenuItem(
"Burrito",
"A large burrito, with whole pinto beans, salsa, guacamole",
True,
4.29,
)
)
diner_menu.add(
MenuItem(
"Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat",
True,
2.99,
)
)
diner_menu.add(
MenuItem(
"BLT",
"Bacon with lettuce & tomato on whole wheat",
False,
2.99,
)
)
diner_menu.add(
MenuItem(
"Soup of the day",
"Soup of the day, with a side of potato salad",
False,
3.29,
)
)
diner_menu.add(
MenuItem(
"Hotdog",
"A hot dog, with sauerkraut, relish, onions, topped with cheese",
False,
3.05,
)
)
diner_menu.add(
MenuItem(
"Steamed Veggies and Brown Rice",
"A medly of steamed vegetables over brown rice",
True,
3.99,
)
)
diner_menu.add(
MenuItem(
"Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
True,
3.89,
)
)
cafe_menu.print()
waitress = Waitress(all_menus)
waitress.print_menu()
# print("\nCustomer asks, is the Hotdog vegetarian?")
# print("Waitress says: ", end="")
# if waitress.is_item_vegetarian("Hotdog"):
# print("Yes")
# else:
# print("No")
""" print("\nCustomer asks, are the Waffles vegetarian?") # Not implemented
print("Waitress says: ", end="")
if (waitress.is_item_vegetarian("Waffles")):
print("Yes")
else:
print("No")"""
if __name__ == "__main__":
menu_test_drive()