fork download
  1. # your code goes here
  2.  
  3. class MenuItem:
  4. def __init__(self, name, price):
  5. self.name = name
  6. self.price = price
  7.  
  8. class Bill:
  9. def __init__(self, table_number, server_name):
  10. self.table_number = table_number
  11. self.server_name = server_name
  12. self.items = []
  13. self.gst_rate = 0.125
  14.  
  15. def add_item(self, item):
  16. self.items.append(item)
  17.  
  18. def calculate_total(self):
  19. subtotal = sum(item.price for item in self.items)
  20. gst = subtotal * self.gst_rate
  21. total = subtotal
  22. return subtotal, gst, total
  23.  
  24. def print_bill(self):
  25. print(f"Table Number: {self.table_number}")
  26. print(f"Server: {self.server_name}")
  27. print("Items:")
  28. for item in self.items:
  29. print(f" - {item.name}: ${item.price:.2f}")
  30. subtotal, gst, total = self.calculate_total()
  31. print(f"Subtotal: ${subtotal:.2f}")
  32. print(f"GST (12.5%): ${gst:.2f}")
  33. print(f"Total: ${total:.2f}")
  34.  
  35. class Restaurant:
  36. def __init__(self):
  37. self.menu = []
  38. self.bills = []
  39.  
  40. def add_menu_item(self, name, price):
  41. self.menu.append(MenuItem(name, price))
  42.  
  43. def create_bill(self, table_number, server_name):
  44. bill = Bill(table_number, server_name)
  45. self.bills.append(bill)
  46. return bill
  47.  
  48. def split_bill(self, bill, num_splits):
  49. split_bills = []
  50. for _ in range(num_splits):
  51. split_bills.append(Bill(bill.table_number, bill.server_name))
  52. for item in bill.items:
  53. for split_bill in split_bills:
  54. split_bill.add_item(item)
  55. return split_bills
  56.  
  57. # Example usage
  58. restaurant = Restaurant()
  59. restaurant.add_menu_item("Pasta", 12.00)
  60. restaurant.add_menu_item("Pizza", 15.00)
  61. restaurant.add_menu_item("Salad", 8.00)
  62.  
  63. # Create a bill for table 1
  64. bill = restaurant.create_bill(1, "John")
  65.  
  66. # Add items to the bill
  67. bill.add_item(restaurant.menu[0]) # Pasta
  68. bill.add_item(restaurant.menu[1]) # Pizza
  69.  
  70. # Print the bill
  71. bill.print_bill()
  72.  
  73. # Split the bill into 2
  74. split_bills = restaurant.split_bill(bill, 2)
  75. for i, split_bill in enumerate(split_bills, 1):
  76. print(f"\nSplit Bill {i}:")
  77. split_bill.print_bill()
Success #stdin #stdout 0.03s 9712KB
stdin
Standard input is empty
stdout
Table Number: 1
Server: John
Items:
 - Pasta: $12.00
 - Pizza: $15.00
Subtotal: $27.00
GST (12.5%): $3.38
Total: $27.00

Split Bill 1:
Table Number: 1
Server: John
Items:
 - Pasta: $12.00
 - Pizza: $15.00
Subtotal: $27.00
GST (12.5%): $3.38
Total: $27.00

Split Bill 2:
Table Number: 1
Server: John
Items:
 - Pasta: $12.00
 - Pizza: $15.00
Subtotal: $27.00
GST (12.5%): $3.38
Total: $27.00