Hotel Management System Simulation in Java

Requirements

Write a program to simulate a hotel manageemnt system: display a list of all rooms, book a room, cancel a booking, etc.
Define a Room class, a Hotel class, and a Client class.
Hotel dimenisons: 5 floors, 10 rooms per floor.
Floors 1 and 2 are standard rooms.
Floors 3 and 4 are double rooms.
Floor 5 is luxury rooms.
The Hotel class must provide methods to:
- Print the room list in format: [101, standard, available], [102, standard, booked]...
- Book a room.
- Cancel a booking.
The Room class should include:
- Room ID: format 101, 102, 201...
- Room type: standard (1), double (2), luxury (3).
- Occupancy status: true = booked, false = available. Default all rooms are available.
The Client class should:
- First print all rooms.
- Then wait for console input in format: book 101 or cancel 101.
- Based on input, perform the action and print the room list again.

Implementation

Hotel Class

package demo.hotel;

import java.util.Arrays;

public class Hotel {
    private Room[][] rooms;

    public Hotel() {
        rooms = new Room[5][10];
        for (int floor = 0; floor < rooms.length; floor++) {
            for (int roomNum = 0; roomNum < rooms[floor].length; roomNum++) {
                int roomId = (floor + 1) * 100 + (roomNum + 1);
                int roomType;
                if (floor == 0) {
                    roomType = 1; // standard
                } else if (floor < 3) {
                    roomType = 2; // double
                } else {
                    roomType = 3; // luxury
                }
                rooms[floor][roomNum] = new Room(roomId, roomType, false);
            }
        }
    }

    public void printAllRooms() {
        for (Room[] floor : rooms) {
            for (Room room : floor) {
                System.out.println(room);
            }
        }
    }

    public boolean bookRoom(int roomId) {
        int floorIndex = roomId / 100 - 1;
        int roomIndex = roomId % 100 - 1;
        if (floorIndex < 0 || floorIndex >= rooms.length ||
            roomIndex < 0 || roomIndex >= rooms[floorIndex].length) {
            System.out.println("Invalid room ID.");
            return false;
        }
        Room room = rooms[floorIndex][roomIndex];
        if (room.isOccupied()) {
            System.out.println("Room " + roomId + " is already booked.");
            return false;
        }
        room.setOccupied(true);
        System.out.println("Room " + roomId + " booked successfully.");
        return true;
    }

    public boolean cancelBooking(int roomId) {
        int floorIndex = roomId / 100 - 1;
        int roomIndex = roomId % 100 - 1;
        if (floorIndex < 0 || floorIndex >= rooms.length ||
            roomIndex < 0 || roomIndex >= rooms[floorIndex].length) {
            System.out.println("Invalid room ID.");
            return false;
        }
        Room room = rooms[floorIndex][roomIndex];
        if (!room.isOccupied()) {
            System.out.println("Room " + roomId + " is already available.");
            return false;
        }
        room.setOccupied(false);
        System.out.println("Booking for room " + roomId + " cancelled.");
        return true;
    }
}

Room Class

package demo.hotel;

public class Room {
    private final int roomId;
    private final int roomType; // 1=standard, 2=double, 3=luxury
    private boolean occupied;

    public Room(int roomId, int roomType, boolean occupied) {
        this.roomId = roomId;
        this.roomType = roomType;
        this.occupied = occupied;
    }

    public int getRoomId() {
        return roomId;
    }

    public int getRoomType() {
        return roomType;
    }

    public boolean isOccupied() {
        return occupied;
    }

    public void setOccupied(boolean occupied) {
        this.occupied = occupied;
    }

    @Override
    public String toString() {
        String typeStr;
        switch (roomType) {
            case 1: typeStr = "standard"; break;
            case 2: typeStr = "double"; break;
            case 3: typeStr = "luxury"; break;
            default: typeStr = "unknown";
        }
        String status = occupied ? "booked" : "available";
        return "[" + roomId + ", " + typeStr + ", " + status + "]";
    }
}

Client Class

package demo.hotel;

import java.util.Scanner;

public class HotelClient {
    public static void main(String[] args) {
        Hotel hotel = new Hotel();
        Scanner scanner = new Scanner(System.in);

        System.out.println("=== Hotel Management System ===");
        hotel.printAllRooms();

        while (true) {
            System.out.print("Enter command (book/cancel, e.g., 'book 101' or 'quit'): ");
            String input = scanner.nextLine().trim();
            if (input.equalsIgnoreCase("quit")) {
                System.out.println("System closed.");
                break;
            }

            String[] parts = input.split("\\s+");
            if (parts.length < 2) {
                System.out.println("Invalid format. Use: book <roomId> or cancel <roomId>");
                continue;
            }

            String action = parts[0].toLowerCase();
            int roomId;
            try {
                roomId = Integer.parseInt(parts[1]);
            } catch (NumberFormatException e) {
                System.out.println("Invalid room number.");
                continue;
            }

            if (action.equals("book")) {
                hotel.bookRoom(roomId);
            } else if (action.equals("cancel")) {
                hotel.cancelBooking(roomId);
            } else {
                System.out.println("Unknown action. Use 'book' or 'cancel'.");
                continue;
            }

            // Print updated room list
            hotel.printAllRooms();
        }

        scanner.close();
    }
}

Execution Example

Example output 1 Example output 2 Example output 3

Posted on Sat, 20 Jun 2026 17:04:07 +0000 by baby_g