Functional Polymorphism (Case Study: Restaurant Order System)
Achieve polymorphism using default arguments to handle flexible customer orders in a restaurant system.
- Define Function: Create a function
take_orderthat acceptsitem_name(mandatory),quantity(default=1), andcustomization(default=None). - Handle Defaults:
- If only
item_nameis provided, assume quantity is 1 and no customization. - If
quantityis provided, update the count. - If
customizationis provided, add the special note.
- If only
- Simulate Orders: Call the function with different combinations of arguments to simulate various customer scenarios.
def take_order(item_name, quantity=1, customization=None):
"""
Simulates taking a restaurant order with varying levels of detail.
"""
order_summary = f"Order: {quantity} x {item_name}"
if customization:
order_summary += f" [Note: {customization}]"
return order_summary
# Case Study: Restaurant Order Processing
print("--- Kitchen Ticket System ---")
# Scenario 1: Quick coffee order (Defaults used)
print(take_order("Cappuccino"))
# Scenario 2: Ordering specifically 2 burgers (Quantity specified)
print(take_order("Cheeseburger", 2))
# Scenario 3: Specific pizza order (All arguments specified)
print(take_order("Margherita Pizza", 1, "Extra Cheese & Basil"))
# Scenario 4: Large custom order
print(take_order("Pasta Alfredo", 3, "No Parsley"))--- Kitchen Ticket System ---
Order: 1 x Cappuccino
Order: 2 x Cheeseburger
Order: 1 x Margherita Pizza [Note: Extra Cheese & Basil]
Order: 3 x Pasta Alfredo [Note: No Parsley]