Java Project – Hotel Management System

Free Java courses with 37 real-time projects - Learn Java

Welcome to our blog on Hotel Management System in Java! In today’s fast-paced world, efficient management of hotels is crucial for providing exceptional guest experiences. To streamline operations and enhance customer satisfaction, we have developed a comprehensive Hotel Management System using Java programming language.

Our Hotel Management System offers a user-friendly and interactive platform to effectively manage various hotel operations such as room bookings, guest check-in/check-out, and room availability. With its intuitive interface and robust functionality, this system serves as a powerful tool for hotel managers, staff, and guests alike. Let’s embark on this exciting journey together and discover the endless possibilities of the Hotel Management System in Java!

About Java Hotel Management System Project

We will implement the following functionalities in the Java Hotel Management System Project:

1)We will create a Hotel class that will represent a hotel and its operations. It will have attributes such as hotelName and rooms and methods to display available rooms, check-in guests, and check-out guests.

2)We will also develop a Room class that will represent a hotel room. It will have attributes like roomNumber, guestName, and occupied, along with getter and setter methods.

3)Furthermore, we will implement a HotelManagementSystem class that will contain the main() method as the entry point of the program. Within this class, we will create instances of Hotel and Room objects. This class will provide a menu-driven interface for users, allowing them to check in, check out, view available rooms, and exit the system.

Prerequisites for Java Hotel Management System Project

To write and run the Hotel Management System Project code, the following prerequisites are required:

1)Basic understanding of the Java programming language, including knowledge of loops, conditional statements, and object-oriented programming concepts.

2)Familiarity with array data structures and the Java.util package, which provides utility classes like Random and Arrays.

3)A working Java development environment, which includes installing the Java Development Kit (JDK) on your computer. You can download the JDK from the official Oracle website or use a package manager specific to your operating system.

4)An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans, which can greatly simplify Java development. Choose an IDE you are comfortable with and set it up.

Download Java Hotel Management System Project

Please download the source code of the Java Hotel Management System Project from the following link: Java Hotel Management System Project Code.

Code Breakdown

Hotel Class:

• Represents a hotel and its operations.
• Contains private variables hotelName and rooms to store the hotel name and an array of Room objects, respectively.
• The constructor Hotel(String hotelName, Room[] rooms) initializes the hotel name and rooms.
• The displayAvailableRooms() method displays the available rooms in the hotel by iterating over the rooms array and checking if each room is occupied or not.
• The checkIn() method handles the check-in process for guests:
• It prompts the user to enter a room number.
• It finds the room using the findRoom() method.
• If the room exists and is not already occupied, it prompts the user to enter the guest name, sets the guest name and occupancy status for the room, and displays a success message.
• If the room is already occupied, it displays an appropriate message.
• If the room doesn’t exist, it displays an error message.
• The checkOut() method handles the check-out process for guests:
• It prompts the user to enter a room number.
• It finds the room using the findRoom() method.
• If the room exists and is occupied, it clears the guest name and occupancy status for the room and displays a success message.
• If the room is not occupied, it displays an appropriate message.
• If the room doesn’t exist, it displays an error message.
• The findRoom(int roomNumber) method iterates over the rooms array and returns the Room object matching the given room number. If no matching room is found, it returns null.

class Hotel {
    private Room[] rooms;
    public Hotel(String hotelName, Room[] rooms) {
        this.rooms = rooms;
    }
    // Display available rooms in the hotel
    public void displayAvailableRooms() {
        System.out.println("Available Rooms:");
        for (Room room : rooms) {
            if (!room.isOccupied()) {
                System.out.println(room);
            }
        }
    }
    // Check-in a guest to a room
    public void checkIn() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter room number: ");
        int roomNumber = scanner.nextInt();
        // Find the room by its number
        Room room = findRoom(roomNumber);
        if (room != null) {
            if (room.isOccupied()) {
                System.out.println("Room " + roomNumber + " is already occupied.");
            } else {
                System.out.print("Enter guest name: ");
                String guestName = scanner.next();
                room.setGuestName(guestName);
                room.setOccupied(true);
                System.out.println("Guest " + guestName + " checked into room " + roomNumber + ".");
            }
        } else {
            System.out.println("Room " + roomNumber + " does not exist.");
        }
    }
    // Check-out a guest from a room
    public void checkOut() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter room number: ");
        int roomNumber = scanner.nextInt();
        // Find the room by its number
        Room room = findRoom(roomNumber);
        if (room != null) {
            if (room.isOccupied()) {
                String guestName = room.getGuestName();
                room.setGuestName("");
                room.setOccupied(false);
                System.out.println("Guest " + guestName + " checked out of room " + roomNumber + ".");
            } else {
                System.out.println("Room " + roomNumber + " is not occupied.");
            }
        } else {
            System.out.println("Room " + roomNumber + " does not exist.");
        }
    }
    // Find a room by its number
    private Room findRoom(int roomNumber) {
        for (Room room : rooms) {
            if (room.getRoomNumber() == roomNumber) {
                return room;
            }
        }
        return null;
    }
}

Room Class:

• Represents a hotel room.
• Contains private variables roomNumber, guestName, and occupied to store the room number, guest name, and occupancy status respectively.
• The constructor Room(int roomNumber) initializes the room number, guest name as an empty string, and occupancy status as false.
• Getter and setter methods are prov`ided for accessing and modifying the room number, guest name, and occupancy status.
• The toString() method returns a string representation of the room, indicating whether it is available or occupied and, if occupied, the guest name.

class Room {
    private int roomNumber;
    private String guestName;
    private boolean occupied;
    public Room(int roomNumber) {
        this.roomNumber = roomNumber;
        this.guestName = "";
        this.occupied = false;
    }
    public int getRoomNumber() {
        return roomNumber;
    }
    public String getGuestName() {
        return guestName;
    }
    public void setGuestName(String guestName) {
        this.guestName = guestName;
    }
    public boolean isOccupied() {
        return occupied;
    }
    public void setOccupied(boolean occupied) {
        this.occupied = occupied;
    }
    @Override
    public String toString() {
        return "Room " + roomNumber + ": " + (occupied ? "Occupied by " + guestName : "Available");
    }
}

HotelManagementSystem Class:

• Contains the main method to run the program.
• Creates an array of Room objects representing the hotel rooms.
• Creates a Hotel object with the hotel name and the array of rooms.
• Displays a menu with options for check-in, check-out, viewing available rooms, and exiting the system.
• Based on the user’s input, it calls the respective methods of the Hotel class to perform the chosen operation.
• This Hotel Management System provides a simple command-line interface for managing hotel operations, including checking in guests, checking out guests, and viewing available rooms.

public class HotelManagementSystem {
    public static void main(String[] args) {
        // Create an array of rooms
        Room[] rooms = {
            new Room(101),
            new Room(102),
            new Room(103),
            new Room(201),
            new Room(202),
            new Room(203),
        };
        // Create a hotel object
        Hotel hotel = new Hotel("Grand Hotel", rooms);
        Scanner scanner = new Scanner(System.in);
        // Display menu and handle user input
        while (true) {
            System.out.println("\n--- Hotel Management System by DataFlair ---");
            System.out.println("1. Check-in");
            System.out.println("2. Check-out");
            System.out.println("3. View available rooms");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    hotel.checkIn();
                    break;
                case 2:
                    hotel.checkOut();
                    break;
                case 3:
                    hotel.displayAvailableRooms();
                    break;
                case 4:
                    System.out.println("Exiting the system...");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }
}

Code to illustrate the Java Hotel Management System Project

import java.util.Scanner;
// Hotel class represents the hotel and its operations
class Hotel {
    private Room[] rooms;
    public Hotel(String hotelName, Room[] rooms) {
        this.rooms = rooms;
    }
    // Display available rooms in the hotel
    public void displayAvailableRooms() {
        System.out.println("Available Rooms:");
        for (Room room : rooms) {
            if (!room.isOccupied()) {
                System.out.println(room);
            }
        }
    }
    // Check-in a guest to a room
    public void checkIn() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter room number: ");
        int roomNumber = scanner.nextInt();
        // Find the room by its number
        Room room = findRoom(roomNumber);
        if (room != null) {
            if (room.isOccupied()) {
                System.out.println("Room " + roomNumber + " is already occupied.");
            } else {
                System.out.print("Enter guest name: ");
                String guestName = scanner.next();
                room.setGuestName(guestName);
                room.setOccupied(true);
                System.out.println("Guest " + guestName + " checked into room " + roomNumber + ".");
            }
        } else {
            System.out.println("Room " + roomNumber + " does not exist.");
        }
    }
    // Check-out a guest from a room
    public void checkOut() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter room number: ");
        int roomNumber = scanner.nextInt();
        // Find the room by its number
        Room room = findRoom(roomNumber);
        if (room != null) {
            if (room.isOccupied()) {
                String guestName = room.getGuestName();
                room.setGuestName("");
                room.setOccupied(false);
                System.out.println("Guest " + guestName + " checked out of room " + roomNumber + ".");
            } else {
                System.out.println("Room " + roomNumber + " is not occupied.");
            }
        } else {
            System.out.println("Room " + roomNumber + " does not exist.");
        }
    }
    // Find a room by its number
    private Room findRoom(int roomNumber) {
        for (Room room : rooms) {
            if (room.getRoomNumber() == roomNumber) {
                return room;
            }
        }
        return null;
    }
}
// Room class represents a hotel room
class Room {
    private int roomNumber;
    private String guestName;
    private boolean occupied;
    public Room(int roomNumber) {
        this.roomNumber = roomNumber;
        this.guestName = "";
        this.occupied = false;
    }
    public int getRoomNumber() {
        return roomNumber;
    }
    public String getGuestName() {
        return guestName;
    }
    public void setGuestName(String guestName) {
        this.guestName = guestName;
    }
    public boolean isOccupied() {
        return occupied;
    }
    public void setOccupied(boolean occupied) {
        this.occupied = occupied;
    }
    @Override
    public String toString() {
        return "Room " + roomNumber + ": " + (occupied ? "Occupied by " + guestName : "Available");
    }
}
// HotelManagementSystem class contains the main method to run the program
public class HotelManagementSystem {
    public static void main(String[] args) {
        // Create an array of rooms
        Room[] rooms = {
            new Room(101),
            new Room(102),
            new Room(103),
            new Room(201),
            new Room(202),
            new Room(203),
        };
        // Create a hotel object
        Hotel hotel = new Hotel("Grand Hotel", rooms);
        Scanner scanner = new Scanner(System.in);
        // Display menu and handle user input
        while (true) {
            System.out.println("\n--- Hotel Management System by DataFlair ---");
            System.out.println("1. Check-in");
            System.out.println("2. Check-out");
            System.out.println("3. View available rooms");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    hotel.checkIn();
                    break;
                case 2:
                    hotel.checkOut();
                    break;
                case 3:
                    hotel.displayAvailableRooms();
                    break;
                case 4:
                    System.out.println("Exiting the system...");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }
}

Java Hotel Management System Output

java hotel management system output

hotel management system output

java hotel management system project output

Summary

As we come to the end of our blog journey exploring the Hotel Management System in Java, we hope you have gained valuable insights into the power and potential of this robust software solution.

Throughout this blog, we have examined the functionalities and components of our system, showcasing how it simplifies complex hotel operations and enhances guest experiences. We hope this blog has sparked your interest in hotel management and Java programming. We encourage you to take the knowledge gained here and embark on your own projects, customizing the system to meet the unique needs of your hotel or hospitality business. Happy coding, and I wish you all the best in your endeavours!

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

1 Response

  1. sultan says:

    it is not working.

Leave a Reply

Your email address will not be published. Required fields are marked *