A hotel booking system can be quite a complex task, but let's simplify it and build a very basic version of it in Python. We will focus on the core functionalities i.e., booking a room, checking availability of rooms and displaying booking information. Our hotel will have a fixed number of rooms and every room will be identified by a unique number.
Step 1: Define the basic Hotel class
The Hotel class will have list of booked rooms as well as the total number of rooms as its attribute. Let's create a constructor for initializing the total number of rooms and a dictionary to represent the room booking. In our scenario, room number will work as the key and the user's name will be the value.
class Hotel:
def __init__(self, total_rooms):
self.total_rooms = int(total_rooms)
self.booked_rooms = dict()
Ask your specific question in Mate AI
In Mate you can connect your project, ask questions about your repository, and use AI Agent to solve programming tasks
Step 2: Add methods to check availability and make a booking
Now, we will add methods to our Hotel class that would allow us to check if a particular room is available and to book a room if it's available.
class Hotel:
def __init__(self, total_rooms):
self.total_rooms = int(total_rooms)
self.booked_rooms = dict()
def is_available(self, room_number):
if room_number in self.booked_rooms.keys():
return False
else:
return True
def book_room(self, room_number, user_name):
if self.is_available(room_number):
self.booked_rooms[room_number] = user_name
else:
print("Sorry, the room is already booked.")
Here, is_available()
function checks the room availability by seeing if the room number is a key in our dictionary. book_room()
function books a room if it's available.
Step 3: Add methods to display booking information
Now, we will add more methods that will display the information about the booked rooms. It's necessary to visualize our system, display the booking status by room number as well as by user name.
class Hotel:
def __init__(self, total_rooms):
self.total_rooms = total_rooms
self.booked_rooms = dict()
def is_available(self, room_number):
if room_number in self.booked_rooms.keys():
return False
else:
return True
def book_room(self, room_number, user_name):
if self.is_available(room_number):
self.booked_rooms[room_number] = user_name
else:
print("Sorry, the room is already booked.")
def display_booked_rooms(self):
for room_number, user_name in self.booked_rooms.items():
print(f"Room Number: {room_number}, Booked By: {user_name}")
def display_user_bookings(self, user_name):
user_rooms = [room for room, user in self.booked_rooms.items() if user == user_name]
print(f"User {user_name} booked rooms {user_rooms}")
This completes our very basic hotel booking system. Now you should instantiate the Hotel class, and then you can start booking rooms and checking information about booked rooms.
Please note this only a basic example and a real-world hotel booking system would be far more complex. They would need to take care of many other aspects like types of rooms, booking periods, prices, user authentication, payment processing, etc. Python has a number of libraries and frameworks such as Django, Flask, SQLite3, SQLAlchemy, etc. that can help you build an advanced and sophisticated hotel booking system.
You'd also typically use Python to run your back-end logic and another technology like HTML/CSS/JavaScript to create the front-end and present the system to the user in a friendly way through a webpage. Currently, since the problem statement asks to build the system using python the front-end part isn't covered here.
AI agent for developers
Boost your productivity with Mate:
easily connect your project, generate code, and debug smarter - all powered by AI.
Do you want to solve problems like this faster? Download now for free.