-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpizza_lib.py
More file actions
331 lines (275 loc) · 8.58 KB
/
Copy pathpizza_lib.py
File metadata and controls
331 lines (275 loc) · 8.58 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from collections import UserList
from math import pi, ceil
from pathlib import Path
from typing import Iterable, List, Text, Union
from PIL import Image
class PizzaPart:
name: Text
base_price: float
diameter: int
image_path: Path
def __init__(
self,
name: Text,
base_price: float,
diameter: int,
image_path: Union[Text, Path],
):
self.name = name
self.base_price = base_price
self.diameter = diameter
self.image_path = Path(image_path)
@property
def area(self) -> float:
return (pi / 4) * self.diameter**2
@property
def price(self) -> float:
return self.base_price * self.area
@property
def image(self) -> Text:
return self.image_path.as_posix()
def __str__(self) -> Text:
return f"{self.name} (⌀{self.diameter}cm, {self.price:.2f}€)"
class PizzaCrust(PizzaPart):
base_duration: float
def __init__(
self,
name: Text,
base_price: float,
diameter: int,
image_path: Union[Text, Path],
base_duration: float,
):
super().__init__(name, base_price, diameter, image_path)
self.diameter = diameter
self.base_duration = base_duration
@property
def duration(self) -> int:
return ceil(self.base_duration * self.area**0.75)
class PizzaSauce(PizzaPart):
pass
class PizzaTopping(PizzaPart):
pass
class Pizza:
name: Text
crust: PizzaCrust
sauce: PizzaSauce
toppings: List[PizzaTopping]
def __init__(
self, name: Text, crust: PizzaCrust, sauce: PizzaSauce, *toppings: PizzaTopping
):
self.name = name
self.crust = crust
self.sauce = sauce
self.toppings = list(toppings)
@property
def price(self) -> float:
pizza_price = self.crust.price
if self.sauce:
pizza_price += self.sauce.price
for topping in self.toppings:
pizza_price += topping.price
return float(f"{pizza_price:.2f}")
@property
def image(self) -> Image:
crust = Image.open(self.crust.image_path) if self.crust else None
sauce = Image.open(self.sauce.image_path) if self.sauce else None
toppings = [
Image.open(topping.image_path) for topping in self.toppings if topping
]
stack = [crust, sauce, *toppings]
stack = [part for part in stack if part]
pizza: Image
pizza, *remaining = stack
for layer in remaining:
pizza.alpha_composite(layer)
return crust
def bake(self) -> List[float]:
return [(self.crust.duration / 100) for index in range(0, 100 + 1)]
def __str__(self) -> Text:
return f"{self.name} (⌀{self.crust.diameter}cm, {self.price}€)"
class PizzaOrder(UserList):
customer_name: Text
customer_phone_number: Text
def __init__(
self, customer_name: Text, customer_phone_number: Text, pizzas: List[Pizza]
):
self.data = pizzas
self.customer_name = customer_name
self.customer_phone_number = customer_phone_number
@property
def price(self) -> float:
order_price = 0.0
for pizza in self.data:
order_price += pizza.price
return float(f"{order_price:.2f}")
SMALL_DIAMETER = 20
MEDIUM_DIAMETER = 26
FAMILY_DIAMETER = 40
PIZZA_SIZES = [SMALL_DIAMETER, MEDIUM_DIAMETER, FAMILY_DIAMETER]
LIGHT_CRUST_PRICE = 0.025
DARK_CRUST_PRICE = 0.035
LIGHT_CRUST_DURATION = 0.05
DARK_CRUST_DURATION = 0.075
TOMATO_SAUCE_PRICE = 0.0005
JOGHURT_SAUCE_PRICE = 0.0007
TOMATO_TOPPING_PRICE = 0.0020
PINEAPPLE_TOPPING_PRICE = 0.0025
CHEESE_TOPPING_PRICE = 0.010
MOZZARELLA_TOPPING_PRICE = 0.025
PIZZA_CRUSTS = [
PizzaCrust(
name="Small Crust",
base_price=LIGHT_CRUST_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/crusts/crust-20cm.png",
base_duration=LIGHT_CRUST_DURATION,
),
PizzaCrust(
name="Medium Crust",
base_price=LIGHT_CRUST_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/crusts/crust-26cm.png",
base_duration=LIGHT_CRUST_DURATION,
),
PizzaCrust(
name="Family Crust",
base_price=LIGHT_CRUST_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/crusts/crust-40cm.png",
base_duration=LIGHT_CRUST_DURATION,
),
PizzaCrust(
name="Small Dark Crust",
base_price=DARK_CRUST_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/crusts/dark-crust-20cm.png",
base_duration=DARK_CRUST_DURATION,
),
PizzaCrust(
name="Medium Dark Crust",
base_price=DARK_CRUST_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/crusts/dark-crust-26cm.png",
base_duration=DARK_CRUST_DURATION,
),
PizzaCrust(
name="Family Dark Crust",
base_price=DARK_CRUST_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/crusts/dark-crust-40cm.png",
base_duration=DARK_CRUST_DURATION,
),
]
PIZZA_SAUCES = [
PizzaSauce(
name="Tomato Sauce (Small)",
base_price=TOMATO_SAUCE_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/sauces/tomato-20cm.png",
),
PizzaSauce(
name="Tomato Sauce (Medium)",
base_price=TOMATO_SAUCE_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/sauces/tomato-26cm.png",
),
PizzaSauce(
name="Tomato Sauce (Small)",
base_price=TOMATO_SAUCE_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/sauces/tomato-40cm.png",
),
PizzaSauce(
name="Joghurt Sauce (Small)",
base_price=JOGHURT_SAUCE_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/sauces/joghurt-20cm.png",
),
PizzaSauce(
name="Joghurt Sauce (Medium)",
base_price=JOGHURT_SAUCE_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/sauces/joghurt-26cm.png",
),
PizzaSauce(
name="Joghurt Sauce (Small)",
base_price=JOGHURT_SAUCE_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/sauces/joghurt-40cm.png",
),
]
PIZZA_TOPPINGS = [
PizzaTopping(
name="Tomato Slices (Small)",
base_price=TOMATO_TOPPING_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/toppings/tomato-20cm.png",
),
PizzaTopping(
name="Tomato Slices (Medium)",
base_price=TOMATO_TOPPING_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/toppings/tomato-26cm.png",
),
PizzaTopping(
name="Tomato Slices (Family)",
base_price=TOMATO_TOPPING_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/toppings/tomato-40cm.png",
),
PizzaTopping(
name="Pineapple Pieces (Small)",
base_price=PINEAPPLE_TOPPING_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/toppings/pineapple-20cm.png",
),
PizzaTopping(
name="Pineapple Pieces (Medium)",
base_price=PINEAPPLE_TOPPING_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/toppings/pineapple-26cm.png",
),
PizzaTopping(
name="Pineapple Pieces (Family)",
base_price=PINEAPPLE_TOPPING_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/toppings/pineapple-40cm.png",
),
PizzaTopping(
name="Cheese (Small)",
base_price=CHEESE_TOPPING_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/toppings/cheese-20cm.png",
),
PizzaTopping(
name="Cheese (Medium)",
base_price=CHEESE_TOPPING_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/toppings/cheese-26cm.png",
),
PizzaTopping(
name="Cheese (Family)",
base_price=CHEESE_TOPPING_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/toppings/cheese-40cm.png",
),
PizzaTopping(
name="Mozzarella Cheese (Small)",
base_price=MOZZARELLA_TOPPING_PRICE,
diameter=SMALL_DIAMETER,
image_path="ingredients/toppings/mozzarella-20cm.png",
),
PizzaTopping(
name="Mozzarella Cheese (Medium)",
base_price=MOZZARELLA_TOPPING_PRICE,
diameter=MEDIUM_DIAMETER,
image_path="ingredients/toppings/mozzarella-26cm.png",
),
PizzaTopping(
name="Mozzarella Cheese (Family)",
base_price=MOZZARELLA_TOPPING_PRICE,
diameter=FAMILY_DIAMETER,
image_path="ingredients/toppings/mozzarella-40cm.png",
),
]