Absolutely! Below is a simple restaurant booking system written in Python. This is a console-based application that allows users to:
- View available tables
- Make a reservation
- View all reservations
- Cancel a reservation
This example uses in-memory data structures (lists and dictionaries). For a real-world application, you’d want to use a database.
import datetime
# Sample data: 10 tables, each with a capacity
tables = {
1: 2, 2: 2, 3: 4, 4: 4, 5: 4,
6: 6, 7: 6, 8: 8, 9: 8, 10: 10
}
# List to store reservations
reservations = []
def show_tables():
print("\nAvailable Tables:")
for table, capacity in tables.items():
print(f"Table {table}: Capacity {capacity}")
def show_reservations():
if not reservations:
print("\nNo reservations found.")
return
print("\nCurrent Reservations:")
for idx, res in enumerate(reservations, 1):
print(f"{idx}. Name: {res['name']}, Table: {res['table']}, Date: {res['date']}, Time: {res['time']}")
def is_table_available(table_num, date, time):
for res in reservations:
if res['table'] == table_num and res['date'] == date and res['time'] == time:
return False
return True
def make_reservation():
name = input("Enter your name: ")
show_tables()
try:
table_num = int(input("Enter table number: "))
if table_num not in tables:
print("Invalid table number.")
return
except ValueError:
print("Invalid input.")
return
date = input("Enter date (YYYY-MM-DD): ")
time = input("Enter time (HH:MM, 24hr): ")
# Optional: Validate date and time format
try:
datetime.datetime.strptime(date, "%Y-%m-%d")
datetime.datetime.strptime(time, "%H:%M")
except ValueError:
print("Invalid date or time format.")
return
if not is_table_available(table_num, date, time):
print("Sorry, this table is already booked at that time.")
return
reservations.append({
'name': name,
'table': table_num,
'date': date,
'time': time
})
print("Reservation successful!")
def cancel_reservation():
show_reservations()
if not reservations:
return
try:
idx = int(input("Enter reservation number to cancel: "))
if 1 <= idx <= len(reservations):
removed = reservations.pop(idx - 1)
print(f"Reservation for {removed['name']} at Table {removed['table']} cancelled.")
else:
print("Invalid reservation number.")
except ValueError:
print("Invalid input.")
def main():
while True:
print("\n--- Restaurant Booking System ---")
print("1. Show available tables")
print("2. Make a reservation")
print("3. View reservations")
print("4. Cancel a reservation")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
show_tables()
elif choice == '2':
make_reservation()
elif choice == '3':
show_reservations()
elif choice == '4':
cancel_reservation()
elif choice == '5':
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
How to use:
- Copy and paste the code into a file, e.g.,
restaurant_booking.py.
- Run it with
python restaurant_booking.py.
- Follow the on-screen menu.
Features you can add:
- Save/load reservations to a file or database
- Support for multiple time slots per table
- User authentication
- Web interface (using Flask or Django)
Let me know if you want a more advanced version or a web-based system!