Low Level Design
Low Level Design
@dataclass
class Item:
name: str
price: float
quantity: int
@dataclass
class Combo:
name: str
items: Dict[str, int] # item_name -> quantity required
discount: float
class Order:
def __init__(self):
self.original_items: Dict[str, Item] = {}
self.current_items: Dict[str, Item] = {}
self.applied_combos: List[Tuple[str, float]] = [] # (combo_name, discount)
self.total_discount: float = 0.0
def add_item(self, name: str, price: float, quantity: int) -> None:
"""Add an item to both original and processed items."""
if name in self.original_items:
self.original_items[name].quantity += quantity
else:
self.original_items[name] = Item(name, price, quantity)
if name in self.current_items:
self.current_items[name].quantity += quantity
else:
self.current_items[name] = Item(name, price, quantity)
# Original order
summary.append("Original Order:")
for item in self.original_items.values():
summary.append(f"{item.quantity}x {item.name} (${item.quantity *
item.price:.2f})")
summary.append(f"Original Total: ${self.get_original_total():.2f}\n")
# Applied combos
if self.applied_combos:
summary.append("Applied Combos:")
for combo_name, discount in self.applied_combos:
summary.append(f"- {combo_name}: -${discount:.2f}")
summary.append(f"Total Discount: -${self.total_discount:.2f}\n")
# Remaining items
if self.current_items:
summary.append("Remaining Items:")
for item in self.current_items.values():
summary.append(f"{item.quantity}x {item.name} (${item.quantity *
item.price:.2f})")
# Final total
summary.append(f"\nFinal Total: ${self.get_final_total():.2f}")
return "\n".join(summary)
class OrderManager:
def __init__(self):
self.combos: List[Combo] = []
def add_combo(self, name: str, items: Dict[str, int], discount: float) -> None:
"""Add a combo definition to the system."""
self.combos.append(Combo(name, items, discount))
def can_apply_combo(self, combo: Combo, order: Order) -> bool:
"""Check if a combo can be applied to the order."""
for item_name, required_qty in combo.items.items():
if (item_name not in order.current_items or
order.current_items[item_name].quantity < required_qty):
return False
return True
order.total_discount += combo.discount
order.applied_combos.append((combo.name, combo.discount))
if not combo_applied:
break
def main():
# Initialize order manager and order
manager = OrderManager()
order = Order()
if __name__ == "__main__":
main()
@dataclass
class Reservation:
customer_name: str
table_id: int
time: datetime
num_guests: int
confirmed: bool = False
@dataclass
class MenuItem:
name: str
price: float
category: str # e.g., "Appetizer", "Main Course", "Dessert"
class Order:
def __init__(self):
self.items: Dict[str, Tuple[MenuItem, int]] = {} # {item_name: (MenuItem,
quantity)}
self.total: float = 0.0
self.discounts_applied: List[Tuple[str, float]] = [] # (description, discount
amount)
class DiscountManager:
def __init__(self):
self.rules: List[Tuple[str, callable]] = [] # (description, discount_rule
function)
class RestaurantSystem:
def __init__(self):
self.tables: List[Table] = []
self.reservations: List[Reservation] = []
self.menu: Dict[str, MenuItem] = {}
self.discount_manager = DiscountManager()
# Table Management
def add_table(self, table_id: int, capacity: int) -> None:
self.tables.append(Table(table_id, capacity))
# Menu Management
def add_menu_item(self, name: str, price: float, category: str) -> None:
self.menu[name] = MenuItem(name, price, category)
# Reservation Management
def make_reservation(self, customer_name: str, table_id: int, time: datetime,
num_guests: int) -> str:
table = next((t for t in self.tables if t.table_id == table_id), None)
if not table:
return "Error: Table not found."
if table.capacity < num_guests:
return f"Error: Table {table_id} cannot seat {num_guests} guests."
# Order Handling
def create_order(self) -> Order:
return Order()
# Add tables
restaurant.add_table(1, 4)
restaurant.add_table(2, 6)
# Make reservations
print(restaurant.make_reservation("Alice", 1, datetime(2025, 1, 17, 19, 0), 3))
print(restaurant.make_reservation("Bob", 2, datetime(2025, 1, 17, 19, 0), 5))
print(restaurant.process_order(order))
if __name__ == "__main__":
main()