Skip to content

Commit 718d04e

Browse files
committed
exemplo de Composite: cardapio composto
1 parent bc27d41 commit 718d04e

File tree

3 files changed

+240
-38
lines changed

3 files changed

+240
-38
lines changed

composite/demo.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
from menu import Menu, MenuItem
2+
3+
pancake_house_menu = Menu("PANCAKE HOUSE MENU", "Breakfast")
4+
diner_menu = Menu("DINER MENU", "Lunch")
5+
cafe_menu = Menu("CAFE MENU", "Dinner")
6+
dessert_menu = Menu("DESSERT MENU", "Dessert of course!")
7+
coffee_menu = Menu("COFFEE MENU", "Stuff to go with your afternoon coffee")
8+
9+
all_menus = Menu("ALL MENUS", "All menus combined")
10+
11+
all_menus.append(pancake_house_menu)
12+
all_menus.append(diner_menu)
13+
all_menus.append(cafe_menu)
14+
15+
pancake_house_menu.append(MenuItem(
16+
"K&B's Pancake Breakfast",
17+
"Pancakes with scrambled eggs, and toast",
18+
True,
19+
2.99))
20+
pancake_house_menu.append(MenuItem(
21+
"Regular Pancake Breakfast",
22+
"Pancakes with fried eggs, sausage",
23+
False,
24+
2.99))
25+
pancake_house_menu.append(MenuItem(
26+
"Blueberry Pancakes",
27+
"Pancakes made with fresh blueberries, and blueberry syrup",
28+
True,
29+
3.49))
30+
pancake_house_menu.append(MenuItem(
31+
"Waffles",
32+
"Waffles, with your choice of blueberries or strawberries",
33+
True,
34+
3.59))
35+
36+
diner_menu.append(MenuItem(
37+
"Vegetarian BLT",
38+
"(Fakin') Bacon with lettuce & tomato on whole wheat",
39+
True,
40+
2.99))
41+
diner_menu.append(MenuItem(
42+
"BLT",
43+
"Bacon with lettuce & tomato on whole wheat",
44+
False,
45+
2.99))
46+
diner_menu.append(MenuItem(
47+
"Soup of the day",
48+
"A bowl of the soup of the day, with a side of potato salad",
49+
False,
50+
3.29))
51+
diner_menu.append(MenuItem(
52+
"Hotdog",
53+
"A hot dog, with saurkraut, relish, onions, topped with cheese",
54+
False,
55+
3.05))
56+
diner_menu.append(MenuItem(
57+
"Steamed Veggies and Brown Rice",
58+
"Steamed vegetables over brown rice",
59+
True,
60+
3.99))
61+
62+
diner_menu.append(MenuItem(
63+
"Pasta",
64+
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
65+
True,
66+
3.89))
67+
68+
diner_menu.append(dessert_menu)
69+
70+
dessert_menu.append(MenuItem(
71+
"Apple Pie",
72+
"Apple pie with a flakey crust, topped with vanilla icecream",
73+
True,
74+
1.59))
75+
76+
dessert_menu.append(MenuItem(
77+
"Cheesecake",
78+
"Creamy New York cheesecake, with a chocolate graham crust",
79+
True,
80+
1.99))
81+
dessert_menu.append(MenuItem(
82+
"Sorbet",
83+
"A scoop of raspberry and a scoop of lime",
84+
True,
85+
1.89))
86+
87+
cafe_menu.append(MenuItem(
88+
"Veggie Burger and Air Fries",
89+
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
90+
True,
91+
3.99))
92+
cafe_menu.append(MenuItem(
93+
"Soup of the day",
94+
"A cup of the soup of the day, with a side salad",
95+
False,
96+
3.69))
97+
cafe_menu.append(MenuItem(
98+
"Burrito",
99+
"A large burrito, with whole pinto beans, salsa, guacamole",
100+
True,
101+
4.29))
102+
103+
cafe_menu.append(coffee_menu)
104+
105+
coffee_menu.append(MenuItem(
106+
"Coffee Cake",
107+
"Crumbly cake topped with cinnamon and walnuts",
108+
True,
109+
1.59))
110+
coffee_menu.append(MenuItem(
111+
"Bagel",
112+
"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
113+
False,
114+
0.69))
115+
coffee_menu.append(MenuItem(
116+
"Biscotti",
117+
"Three almond or hazelnut biscotti cookies",
118+
True,
119+
0.89))
120+
121+
class Garconete:
122+
123+
def __init__(self, menu):
124+
self.menu = menu
125+
126+
def display_menu(self):
127+
self.menu.display()
128+
129+
jane = Garconete(all_menus)
130+
131+
jane.display_menu()

composite/menu.py

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,68 @@
1+
from menuabc import MenuComponent
12

2-
import abc
3-
4-
class ItemCardapio(abc.ABC):
5-
6-
@abc.abstractmethod
7-
def incluir(self, item):
8-
'''incluir
9-
10-
11-
public abstract class MenuComponent {
12-
13-
public void add(MenuComponent menuComponent) {
14-
throw new UnsupportedOperationException();
15-
}
16-
public void remove(MenuComponent menuComponent) {
17-
throw new UnsupportedOperationException();
18-
}
19-
public MenuComponent getChild(int i) {
20-
throw new UnsupportedOperationException();
21-
}
22-
23-
public String getName() {
24-
throw new UnsupportedOperationException();
25-
}
26-
public String getDescription() {
27-
throw new UnsupportedOperationException();
28-
}
29-
public double getPrice() {
30-
throw new UnsupportedOperationException();
31-
}
32-
public boolean isVegetarian() {
33-
throw new UnsupportedOperationException();
34-
}
35-
36-
public void print() {
37-
throw new UnsupportedOperationException();
38-
}
39-
}
3+
class Menu(MenuComponent):
4+
5+
def __init__(self, name, description):
6+
self.menu_components = []
7+
self.__name = name
8+
self.__description = description
9+
10+
def append(self, menu_component):
11+
self.menu_components.append(menu_component)
12+
13+
def remove(self, menu_component):
14+
self.menu_components.remove(menu_component)
15+
16+
def __getitem__(self, index):
17+
return self.menu_components[index]
18+
19+
@property
20+
def name(self):
21+
return self.__name
22+
23+
@property
24+
def description(self):
25+
return self.__description
26+
27+
@property
28+
def is_vegetarian(self):
29+
return all(sub_menu.is_vegetarian for sub_menu in self.menu_components)
30+
31+
32+
def display(self):
33+
print('\n' + self.name, ', ', self.description, sep='')
34+
print('-' * 60);
35+
for menu_component in self.menu_components:
36+
menu_component.display()
37+
38+
39+
class MenuItem(MenuComponent):
40+
41+
def __init__(self, name, description, vegetarian, price):
42+
self.__name = name
43+
self.__description = description
44+
self.__vegetarian = vegetarian
45+
self.__price = price
46+
47+
@property
48+
def name(self):
49+
return self.__name
50+
51+
@property
52+
def description(self):
53+
return self.__description
54+
55+
@property
56+
def is_vegetarian(self):
57+
return self.__vegetarian
58+
59+
@property
60+
def price(self):
61+
return self.__price
62+
63+
def display(self):
64+
print(' ' + self.name, end='')
65+
if self.is_vegetarian:
66+
print('(v)', end='')
67+
print(',', self.price)
68+
print(' --', self.description);

composite/menuabc.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
from abc import ABC, abstractmethod
3+
4+
class MenuComponent(ABC):
5+
'''Item do cardápio, com sub-itens'''
6+
7+
def append(self, item):
8+
'''incluir sub-item'''
9+
raise NotImplementedError()
10+
11+
def remove(self, item):
12+
'''remover sub-item'''
13+
raise NotImplementedError()
14+
15+
def __getitem__(self, i):
16+
'''obter sub-item pelo índice'''
17+
raise NotImplementedError()
18+
19+
@property
20+
@abstractmethod
21+
def name(self):
22+
'''obter nome do item'''
23+
24+
@property
25+
@abstractmethod
26+
def description(self):
27+
'''obter descrição do item'''
28+
29+
@property
30+
@abstractmethod
31+
def description(self):
32+
'''obter preço do item'''
33+
34+
@property
35+
@abstractmethod
36+
def is_vegetarian(self):
37+
'''obter booleano indicador de item vegetariano'''
38+
39+
@abstractmethod
40+
def display(self):
41+
'''exibir item e sub-itens'''
42+

0 commit comments

Comments
 (0)