-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_system.py
More file actions
352 lines (276 loc) · 10.8 KB
/
Copy pathorder_system.py
File metadata and controls
352 lines (276 loc) · 10.8 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
def place_order(menu):
"""
Displays a restaurant menu, asks customers for their order, then returns
their receipt and total price.
Parameters:
menu (dictionary): A nested dictionary containing the menu items and their
prices, using the following format:
{
"Food category": {
"Meal": price
}
}
Returns:
order (list): A list of dictionaries containing the menu item name, price,
and quantity ordered.
order_total (float): The total price of the order.
"""
# Set up order list. Order list will store a list of dictionaries for
# menu item name, item price, and quantity ordered
order = []
# Get the menu items mapped to the menu numbers
menu_items = get_menu_items_dict(menu)
# Launch the store and present a greeting to the customer
print("Welcome to the Generic Take Out Restaurant.")
print("What would you like to order? ")
place_order = True
while place_order:
index = 1
print_menu_heading()
for food_category, options in menu.items():
for meal, price in options.items():
print_menu_line(index, food_category, meal, price)
index += 1
# Ask customer to input menu item number
menu_selection = input("Type menu number: ")
# Update the order list using the update_order function
# Send the order list, menu selection, and menu items as arguments
order = update_order(order, menu_selection, menu_items)
# Ask the customer if they would like to order anything else
# Let the customer know if they should type 'n' or 'N' to quit
keep_ordering = input("Would you like to keep ordering? (N) to quit: ")
# TODO: Write a conditional statement that checks if the customer types
# 'n' or 'N'
if keep_ordering.lower() =='n':
# Since the customer decided to stop ordering, thank them for
# their order
print("Thank you for your order.")
# TODO: Use a list comprehension to create a list called prices_list,
# which contains the total prices for each item in the order list:
# The total price for each item should multiply the price by quantity
prices_list = [item["Price"] * item["Quantity"] for item in order]
# TODO: Create an order_total from the prices list using sum()
# and round the prices to 2 decimal places.
order_total = round(sum(prices_list), 2)
# Write a break statement or set the condition to False to exit
# the ordering loop
place_order = False
# TODO: Return the order list and the order total
return order, order_total
def update_order(order, menu_selection, menu_items):
"""
Checks if the customer menu selection is valid, then updates the order.
Parameters:
order (list): A list of dictionaries containing the menu item name, price,
and quantity ordered.
menu_selection (str): The customer's menu selection.
menu_items (dictionary): A dictionary containing the menu items and their
prices.
Returns:
order (list): A list of dictionaries containing the menu item name, price,
and quantity ordered (updated as needed).
"""
if not menu_selection.isdigit():
print(f"Invalid input: {menu_selection} was not a menu option.")
else:
menu_selection = int(menu_selection)
if menu_selection not in menu_items:
print(f"Invalid: {menu_selection} isn't valid menu item number.")
else:
item_name = menu_items[menu_selection]["Item name"]
# TODO: A prompt (input) to the customer that prints the name of the
# menu item to the user and asks the quantity they would like to order.
# Store the return in a quantity variable
quantity = input(f'What quantity of {item_name} would you like? \n(This will default to 1 if number is not entered)\n')
# TODO: Write a conditional statement that checks if the input quantity
# can be converted to an integer, then converts it to an integer.
# Have it default to 1 if it does not.
try:
quantity = int(quantity)
except ValueError:
quantity = 1
# TODO: Add a dictionary with the item name, price, and quantity to the
# order list. Use the following names for the dictionary keys:
# "Item name", "Price", "Quantity"
order.append({
"Item name": item_name,
"Price": menu_items[menu_selection]["Price"],
"Quantity": quantity
})
return order
# TODO: Return the updated order
def print_itemized_receipt(receipt):
"""
Prints an itemized receipt for the customer.
Parameters:
receipt (list): A list of dictionaries containing the menu item name,
and quantity ordered.
"""
# TODO: Loop through the items in the customer's receipt
for item in receipt:
item_name = item["Item name"]
price = item["Price"]
quantity = item["Quantity"]
print_receipt_line(item_name, price, quantity)
##################################################
# STARTER CODE
# Do not modify any of the code below this line:
##################################################
def print_receipt_line(item_name, price, quantity):
"""
Prints a line of the receipt.
Parameters:
item_name (str): The name of the meal item.
price (float): The price of the meal item.
quantity (int): The quantity of the meal item.
"""
# Calculate the number of spaces for formatted printing
num_item_spaces = 32 - len(item_name)
num_price_spaces = 6 - len(str(price))
# Create space strings
item_spaces = " " * num_item_spaces
price_spaces = " " * num_price_spaces
# Print the item name, price, and quantity
print(f"{item_name}{item_spaces}| ${price}{price_spaces}| {quantity}")
def print_receipt_heading():
"""
Prints the receipt heading.
"""
print("----------------------------------------------------")
print("Item name | Price | Quantity")
print("--------------------------------|--------|----------")
def print_receipt_footer(total_price):
"""
Prints the receipt footer with the total price of the order.
Parameters:
total_price (float): The total price of the order.
"""
print("----------------------------------------------------")
print(f"Total price: ${total_price:.2f}")
print("----------------------------------------------------")
def print_menu_heading():
"""
Prints the menu heading.
"""
print("--------------------------------------------------")
print("Item # | Item name | Price")
print("-------|----------------------------------|-------")
def print_menu_line(index, food_category, meal, price):
"""
Prints a line of the menu.
Parameters:
index (int): The menu item number.
food_category (str): The category of the food item.
meal (str): The name of the meal item.
price (float): The price of the meal item.
"""
# Print the menu item number, food category, meal, and price
num_item_spaces = 32 - len(food_category + meal) - 3
item_spaces = " " * num_item_spaces
if index < 10:
i_spaces = " " * 6
else:
i_spaces = " " * 5
print(f"{index}{i_spaces}| {food_category} - {meal}{item_spaces} | ${price}")
def get_menu_items_dict(menu):
"""
Creates a dictionary of menu items and their prices mapped to their menu
number.
Parameters:
menu (dictionary): A nested dictionary containing the menu items and their
prices.
Returns:
menu_items (dictionary): A dictionary containing the menu items and their
prices.
"""
# Create an empty dictionary to store the menu items
menu_items = {}
# Create a variable for the menu item number
i = 1
# Loop through the menu dictionary
for food_category, options in menu.items():
# Loop through the options for each food category
for meal, price in options.items():
# Store the menu item number, item name and price in the menu_items
menu_items[i] = {
"Item name": food_category + " - " + meal,
"Price": price
}
i += 1
return menu_items
def get_menu_dictionary():
"""
Returns a dictionary of menu items and their prices.
Returns:
meals (dictionary): A nested dictionary containing the menu items and their
prices in the following format:
{
"Food category": {
"Meal": price
}
}
"""
# Create a meal menu dictionary
#"""
meals = {
"Burrito": {
"Chicken": 4.49,
"Beef": 5.49,
"Vegetarian": 3.99
},
"Rice Bowl": {
"Teriyaki Chicken": 9.99,
"Sweet and Sour Pork": 8.99
},
"Sushi": {
"California Roll": 7.49,
"Spicy Tuna Roll": 8.49
},
"Noodles": {
"Pad Thai": 6.99,
"Lo Mein": 7.99,
"Mee Goreng": 8.99
},
"Pizza": {
"Cheese": 8.99,
"Pepperoni": 10.99,
"Vegetarian": 9.99
},
"Burger": {
"Chicken": 7.49,
"Beef": 8.49
}
}
"""
# This menu is just for testing purposes
meals = {
"Cake": {
"Kuih Lapis": 3.49,
"Strawberry Cheesecake": 6.49,
"Chocolate Crepe Cake": 6.99
},
"Pie": {
"Apple": 4.99,
"Lemon Meringue": 5.49
},
"Ice-cream": {
"2-Scoop Vanilla Cone": 3.49,
"Banana Split": 8.49,
"Chocolate Sundae": 6.99
}
}
"""
return meals
# Run the program
if __name__ == "__main__":
# Get the menu dictionary
meals = get_menu_dictionary()
receipt, total_price = place_order(meals)
# Print out the customer's order
print("This is what we are preparing for you.\n")
# Print the receipt heading
print_receipt_heading()
# Print the customer's itemized receipt
print_itemized_receipt(receipt)
# Print the receipt footer with the total price
print_receipt_footer(total_price)