HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Pizza Ordering Program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
programorderingpizza

Problem

Updated code here on repl.it
I have changed the code so the get-input logic is in the class, as separate methods. This means the loop to input multiple orders is much more concise than before.

The full code:

```
"""Onehunga Pizzas phone orders"""

import re
import sys

# CONFIG #
# maximum number of pizzas in one order
MAX_PIZZAS = 5
# delivery charge (in $)
DELIVERY_CHARGE = 3.00
# list of dictionaries (pizzas) with name and price
PIZZAS_AVAILABLE = (
{"name": "Hawaiian", "price": 8.5},
{"name": "Meat Lovers", "price": 8.5},
{"name": "Pepperoni", "price": 8.5},
{"name": "Ham & Cheese", "price": 8.5},
{"name": "Classic Cheese", "price": 8.5},
{"name": "Veg Hot 'n' Spicy", "price": 8.5},
{"name": "Beef & Onion", "price": 8.5},
{"name": "Seafood Deluxe", "price": 13.5},
{"name": "Summer Shrimp", "price": 13.5},
{"name": "BBQ Bacon & Mushroom", "price": 13.5},
{"name": "BBQ Hawaiian", "price": 13.5},
{"name": "Italiano", "price": 13.5},
)
# END CONFIG #

def get_input(regex, input_message=None, error_message=None):
"""Gets valid input, validated using regular expressions."""
# loops until input is valid ("break" is called)
while True:
# input to validate, input prompt is as specified
user_input = input(str(input_message))
user_input = user_input.lower().strip()
# check if the user wants to quit or cancel the order
if user_input == "qq" or user_input == "quit":
sys.exit()
elif user_input == "cc" or user_input == "cancel":
return "CANCEL"

# check if the input matches the regex provided
if re.match(regex, user_input, re.IGNORECASE):
break

# if it doesn't match, and an error message has been specified
if error_message:
print(str(error_message))

return user_input

def print_line(line):
""

Solution


  • The configuration is really a set of constants, so their names should be uppercase.



  • You shouldn't use exceptions for control flow. Cancelling an order is not an exceptional event, it is part of the normal flow of your application, just like removing an item from an order would be.



  • I generally consider it an anti-pattern to create an object with no parameters and filling it in gradually. Instead



  • Accumulate everything you need to create an Order in variables.



  • Create the Order when everything is ready.



  • Specify any defaults (if necessary) in the constructor parameters.



  • Order is a data class. Shouldn't it have methods like cancel and submit?



  • I would rip out all the print and get_input stuff into a separate user interface component. That would greatly simplify reading the actual logic. Today, interactive text interfaces like this is usually only found in homework assignments. In a real-life system you are much more likely to have a non-interactive tool which takes everything it needs (removing the need to be able to cancel an order and all the print/get_input stuff) and some sort of storage to persist an order.

Context

StackExchange Code Review Q#159588, answer score: 4

Revisions (0)

No revisions yet.