Problem Statement
Design a hotel management system that allows guests to book rooms, check room availability, cancel bookings, and manage hotel rooms efficiently. The design should support multiple room types and ensure that a room cannot be booked by multiple guests at the same time.
Functional Requirements
- The design should support multiple room types such as standard, deluxe, and suite
- Guests should be able to search available rooms
- A guest should be able to book a room
- A booked room should not be assigned to another guest
- The design should support booking cancellation
- Cancelled rooms should become available again
- The design should maintain booking details for every guest
- The system should support multiple hotels and rooms
Objects Required
- Hotel
- Room
- RoomType
- Guest
- Booking
- BookingStatus
- HotelManagementSystem
RoomType Enum
public enum RoomType {
STANDARD,
DELUXE,
SUITE
}
The RoomType enum defines the supported room categories inside the hotel.
Using an enum avoids invalid room types and keeps comparisons simple throughout the booking flow.
BookingStatus Enum
public enum BookingStatus {
CONFIRMED,
CANCELLED
}
The BookingStatus enum represents the current state of a booking.
Room Class
The Room class represents an individual room inside the hotel.
public class Room {
private int roomNumber;
private RoomType type;
private boolean isAvailable;
public Room(int roomNumber, RoomType type) {
this.roomNumber = roomNumber;
this.type = type;
this.isAvailable = true;
}
public boolean isAvailable() {
return isAvailable;
}
public void bookRoom() {
isAvailable = false;
}
public void releaseRoom() {
isAvailable = true;
}
public RoomType getType() {
return type;
}
public int getRoomNumber() {
return roomNumber;
}
}
The constructor initializes the room number, room type, and marks the room as available initially.
The isAvailable() method checks whether the room is currently free.
The bookRoom() method marks the room as occupied after successful booking.
The releaseRoom() method makes the room available again when the booking is cancelled or completed.
The getter methods provide controlled access to room details.
Guest Class
The Guest class stores guest related information.
public class Guest {
private String guestId;
private String name;
public Guest(String guestId, String name) {
this.guestId = guestId;
this.name = name;
}
public String getName() {
return name;
}
}
The constructor initializes the guest with an id and name.
The getName() method is used whenever guest details are required during booking or display operations.
Booking Class
The Booking class stores details about a room reservation.
public class Booking {
private String bookingId;
private Guest guest;
private Room room;
private BookingStatus status;
public Booking(String bookingId,
Guest guest,
Room room) {
this.bookingId = bookingId;
this.guest = guest;
this.room = room;
this.status = BookingStatus.CONFIRMED;
}
public void cancelBooking() {
room.releaseRoom();
status = BookingStatus.CANCELLED;
}
}
The constructor creates a booking object and stores the associated guest and room information.
The booking status is initially marked as CONFIRMED.
The cancelBooking() method releases the booked room and updates the booking status to CANCELLED.
Hotel Class
The Hotel class manages rooms available inside a hotel.
import java.util.*;
public class Hotel {
private String hotelName;
private List<Room> rooms;
public Hotel(String hotelName,
List<Room> rooms) {
this.hotelName = hotelName;
this.rooms = rooms;
}
public List<Room> getAvailableRooms(RoomType type) {
List<Room> availableRooms =
new ArrayList<>();
for (Room room : rooms) {
if (room.isAvailable()
&& room.getType() == type) {
availableRooms.add(room);
}
}
return availableRooms;
}
}
The constructor initializes the hotel with its rooms.
The getAvailableRooms() method filters rooms based on room type and availability.
Only rooms matching the requested category and currently available are returned to the user.
HotelManagementSystem Class
The HotelManagementSystem class coordinates the booking process.
import java.util.*;
public class HotelManagementSystem {
private List<Hotel> hotels;
public HotelManagementSystem(List<Hotel> hotels) {
this.hotels = hotels;
}
public Booking bookRoom(Guest guest,
Hotel hotel,
RoomType roomType) {
List<Room> availableRooms =
hotel.getAvailableRooms(roomType);
if (availableRooms.isEmpty()) {
System.out.println("No rooms available");
return null;
}
Room room = availableRooms.get(0);
room.bookRoom();
return new Booking(
UUID.randomUUID().toString(),
guest,
room
);
}
}
The constructor initializes the system with available hotels.
The bookRoom() method first searches for available rooms of the requested type.
If no room is available, the booking request fails immediately.
If rooms are available, the first matching room is selected and marked as booked.
Finally, a booking object is created with a unique booking id.
Main Class
The Main class is used to simulate the hotel booking flow.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Room> rooms = Arrays.asList(
new Room(101, RoomType.STANDARD),
new Room(102, RoomType.DELUXE),
new Room(103, RoomType.SUITE)
);
Hotel hotel =
new Hotel("Grand Palace", rooms);
HotelManagementSystem system =
new HotelManagementSystem(
Arrays.asList(hotel)
);
Guest guest =
new Guest("G1", "Prasanna");
Booking booking =
system.bookRoom(
guest,
hotel,
RoomType.DELUXE
);
if (booking != null) {
System.out.println("Room booked successfully");
}
booking.cancelBooking();
System.out.println("Booking cancelled");
}
}
The main() method starts by creating hotel rooms of different types.
A hotel object is then initialized with those rooms.
The booking system receives a booking request from the guest and allocates a matching room if available.
Finally, the booking is cancelled to demonstrate how the room becomes available again.
Comments
Post a Comment