Designing a Hotel Management System


Requirements:
Sequnece Diagram
cache
Low-Level Design:

The system includes the following key classes:


Code Implementation:
import java.util.*;

enum RoomType {
    SINGLE, DOUBLE, DELUXE, SUITE
}
class Room {
    private int roomNumber;
    private RoomType roomType;
    private boolean isAvailable;

    public Room(int roomNumber, RoomType roomType) {
        this.roomNumber = roomNumber;
        this.roomType = roomType;
        this.isAvailable = true;
    }

    public int getRoomNumber() {
        return roomNumber;
    }

    public RoomType getRoomType() {
        return roomType;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void setAvailable(boolean available) {
        isAvailable = available;
    }
}
class Guest {
    private String name;
    private String contact;
    private Reservation reservation;

    public Guest(String name, String contact) {
        this.name = name;
        this.contact = contact;
    }

    public String getName() {
        return name;
    }

    public String getContact() {
        return contact;
    }

    public Reservation getReservation() {
        return reservation;
    }

    public void setReservation(Reservation reservation) {
        this.reservation = reservation;
    }
}
class Reservation {
    private Room room;
    private Date checkInDate;
    private Date checkOutDate;

    public Reservation(Room room, Date checkInDate, Date checkOutDate) {
        this.room = room;
        this.checkInDate = checkInDate;
        this.checkOutDate = checkOutDate;
    }

    public Room getRoom() {
        return room;
    }

    public Date getCheckInDate() {
        return checkInDate;
    }

    public Date getCheckOutDate() {
        return checkOutDate;
    }
}
class HotelManagementSystem {
    private List<Room> rooms = new ArrayList<>();
    private Map<Integer, Guest> guestMap = new HashMap<>();

    public void addRoom(Room room) {
        rooms.add(room);
    }

    public Room findAvailableRoom(RoomType roomType) {
        for (Room room : rooms) {
            if (room.getRoomType() == roomType && room.isAvailable()) {
                return room;
            }
        }
        return null;
    }

    public void bookRoom(String guestName, String contact, RoomType roomType, Date checkInDate, Date checkOutDate) {
        Room room = findAvailableRoom(roomType);
        if (room == null) {
            System.out.println("No rooms available of type " + roomType);
            return;
        }
        Guest guest = new Guest(guestName, contact);
        Reservation reservation = new Reservation(room, checkInDate, checkOutDate);
        guest.setReservation(reservation);
        guestMap.put(room.getRoomNumber(), guest);
        room.setAvailable(false);
        System.out.println("Room " + room.getRoomNumber() + " booked for " + guestName);
    }

    public void checkOut(int roomNumber) {
        Guest guest = guestMap.get(roomNumber);
        if (guest == null) {
            System.out.println("No reservation found for room " + roomNumber);
            return;
        }
        Room room = guest.getReservation().getRoom();
        room.setAvailable(true);
        guestMap.remove(roomNumber);
        System.out.println("Checked out guest: " + guest.getName() + " from room " + roomNumber);
    }

    public void listAvailableRooms() {
        System.out.println("Available Rooms:");
        for (Room room : rooms) {
            if (room.isAvailable()) {
                System.out.println("Room " + room.getRoomNumber() + " - " + room.getRoomType());
            }
        }
    }
}
public class HotelManagementApp {
    public static void main(String[] args) {
        HotelManagementSystem system = new HotelManagementSystem();

        // Add rooms
        system.addRoom(new Room(101, RoomType.SINGLE));
        system.addRoom(new Room(102, RoomType.DOUBLE));
        system.addRoom(new Room(103, RoomType.DELUXE));
        system.addRoom(new Room(104, RoomType.SUITE));

        // List available rooms
        system.listAvailableRooms();

        // Book a room
        system.bookRoom("Alice", "123-456-7890", RoomType.SINGLE, new Date(), new Date());

        // List available rooms after booking
        system.listAvailableRooms();

        // Check out a guest
        system.checkOut(101);

        // List available rooms after check-out
        system.listAvailableRooms();
    }
}
Sample Input and Output:
Input: 
    Add rooms: [101-SINGLE, 102-DOUBLE, 103-DELUXE, 104-SUITE]
    Book Room: Alice (RoomType: SINGLE)
    Check Out: Room 101

Output:
    Available Rooms:
    Room 101 - SINGLE
    Room 102 - DOUBLE
    Room 103 - DELUXE
    Room 104 - SUITE
    Room 101 booked for Alice
    Available Rooms:
    Room 102 - DOUBLE
    Room 103 - DELUXE
    Room 104 - SUITE
    Checked out guest: Alice from room 101
    Available Rooms:
    Room 101 - SINGLE
    Room 102 - DOUBLE
    Room 103 - DELUXE
    Room 104 - SUITE
    

Design Considerations:
Improvements & Extensions: