Back

Food Ordering System

The code is a simple food order system that allows users to order meals based on a type of cuisine, the available options, then generates an order summary.

Code Block

This script simulates a basic food ordering system with two cuisine options: Italian and Indian. It starts by displaying a menu of available dishes based on the user's selected cuisine type. The user can then input the meal they want to order and the quantity. The script verifies if the selected meal is available, and if so, it generates a summary of the order, including the quantity and meal name.

Check out the Code

Code Analysis

Correcting the select_meal() Function:
  • There's a logic error in select_meal(). The parameters in the function call for select_meal() should be swapped because the food_type and name are passed in the wrong order. The correct call should be:

def create_summary(name, amount, food_type):
   order = select_meal(name, food_type)
   if order:
       return f"You ordered {amount} {name}"
   else:
       return "Meal not found"

Input Validation:
  • There’s no validation on user inputs for food type and meal name. If the user enters an invalid food type or meal name, the system may return unexpected results. Adding validation can improve user experience:

if type_input not in ["Italian", "Indian"]:
   print("Invalid food type selected.")
   exit()

  • Similarly, for amount_input, it should be validated to ensure it's a positive integer:

try:
   amount = int(amount_input)
   if amount <= 0:
       print("Please enter a positive number for the amount.")
       exit()
except ValueError:
   print("Please enter a valid number for the amount.")
   exit()

Enhancing User Experience:
  • The code could be enhanced by providing clear instructions and error messages, making it more user-friendly. For example, you could guide the user through the available food types before asking for their input.

Refactor for Reusability:
  • The code could be refactored to improve reusability. For instance, instead of using hardcoded food lists, consider using a dictionary to store multiple food types and their associated meals. This would make it easier to expand the menu in the future.

python

menu = {
   "Italian": ["Pasta Bolognese", "Pepperoni pizza", "Margherita pizza", "Lasagna"],
   "Indian": ["Curry", "Chutney", "Samosa", "Naan"]
}

def find_meal(name, menu):
   for food_type, meals in menu.items():
       if name in meals:
           return name
   return None

def display_available_meals(food_type):
   if food_type in menu:
       print(f"Available {food_type} Meals:")
       for item in menu[food_type]:
           print(item)
   else:
       print("Invalid food type")

Expandability:
  • The system could be expanded by allowing users to order from multiple food types at once or add more cuisines without much modification to the existing logic.

Debugging the Logic Flow:
  • The current implementation of create_summary() will fail if the select_meal() function returns None because the meal isn't found. The function should be enhanced to handle these cases more gracefully.

Final Notes:

The code provides a great starting point for a simple food ordering system. By enhancing input validation, restructuring data storage, and improving error handling, the system can be made more robust, scalable, and user-friendly.