Hotel Booking System in Java with Source Code

Hotel Booking System in Java with Source Code

Hotel Booking System in Java

Managing hotel reservations can be a complex and time-consuming process, but with a Hotel Booking System built in Java, the process becomes simpler, more efficient, and organized. This Java-based application is designed to handle room reservations, guest details, and manage bookings seamlessly, making it ideal for small inns, guesthouses, and boutique hotels.


Key Features 🌟

  1. Reserve a Room
    The system allows the user to easily reserve rooms by entering guest details, selecting a room number, and providing contact information. It tracks the reservation details and stores them securely in a database.
  2. View Reservations
    This feature provides a clear overview of all current bookings. Users can see guest names, room numbers, contact details, and reservation dates. It’s an efficient way to keep track of who’s staying and for how long.
  3. Edit Reservation Details
    The system is flexible, allowing users to update existing reservation details. Whether it’s a name change or a room switch, editing reservations is hassle-free.
  4. Delete Reservations
    Old or canceled bookings can easily be deleted from the system, ensuring that the reservation list stays up-to-date and clutter-free.

PHP PROJECT:- CLICK HERE


Getting Started 🚀

Here’s a simple guide to setting up the Hotel Booking System on your local machine.

Prerequisites

  • Java Development Kit (JDK): Ensure you have JDK installed to compile and run the Java application.
  • MySQL Database: The system requires a MySQL database to store and manage reservation data.
  • MySQL Connector/J: This is necessary for Java to communicate with the MySQL database.
See also  Create a Snake Game in Java

Step-by-Step Setup

If you’d like a Hotel Reservation System in Java without using a database, we can store the data in memory using Java’s ArrayList or other collection classes. This approach simplifies the code and allows you to manage hotel reservations without the complexity of setting up and managing a database.

Here’s a simplified version of the Hotel Booking System that uses an ArrayList to store reservations:

Code

import java.util.ArrayList;
import java.util.Scanner;

class Reservation {
    private int id;
    private int roomNumber;
    private String guestName;
    private String contactInfo;
    private String checkInDate;
    private String checkOutDate;

    public Reservation(int id, int roomNumber, String guestName, String contactInfo, String checkInDate, String checkOutDate) {
        this.id = id;
        this.roomNumber = roomNumber;
        this.guestName = guestName;
        this.contactInfo = contactInfo;
        this.checkInDate = checkInDate;
        this.checkOutDate = checkOutDate;
    }

    // Getters and setters
    public int getId() { return id; }
    public int getRoomNumber() { return roomNumber; }
    public String getGuestName() { return guestName; }
    public void setGuestName(String guestName) { this.guestName = guestName; }
    public String getContactInfo() { return contactInfo; }
    public void setContactInfo(String contactInfo) { this.contactInfo = contactInfo; }
    public String getCheckInDate() { return checkInDate; }
    public String getCheckOutDate() { return checkOutDate; }

    @Override
    public String toString() {
        return "Reservation ID: " + id + 
               ", Room Number: " + roomNumber + 
               ", Guest: " + guestName + 
               ", Contact: " + contactInfo + 
               ", Check-in: " + checkInDate + 
               ", Check-out: " + checkOutDate;
    }
}

public class HotelReservationSystem {
    private ArrayList<Reservation> reservations;
    private int nextReservationId;

    public HotelReservationSystem() {
        reservations = new ArrayList<>();
        nextReservationId = 1;
    }

    // Method to add a new reservation
    public void addReservation(int roomNumber, String guestName, String contactInfo, String checkInDate, String checkOutDate) {
        Reservation reservation = new Reservation(nextReservationId, roomNumber, guestName, contactInfo, checkInDate, checkOutDate);
        reservations.add(reservation);
        nextReservationId++;
        System.out.println("Reservation successfully added!");
    }

    // Method to view all reservations
    public void viewReservations() {
        if (reservations.isEmpty()) {
            System.out.println("No reservations found.");
        } else {
            for (Reservation reservation : reservations) {
                System.out.println(reservation);
            }
        }
    }

    // Method to update an existing reservation
    public void updateReservation(int id, String guestName, String contactInfo) {
        for (Reservation reservation : reservations) {
            if (reservation.getId() == id) {
                reservation.setGuestName(guestName);
                reservation.setContactInfo(contactInfo);
                System.out.println("Reservation successfully updated!");
                return;
            }
        }
        System.out.println("Reservation not found.");
    }

    // Method to delete a reservation
    public void deleteReservation(int id) {
        for (Reservation reservation : reservations) {
            if (reservation.getId() == id) {
                reservations.remove(reservation);
                System.out.println("Reservation successfully deleted!");
                return;
            }
        }
        System.out.println("Reservation not found.");
    }

    // Main menu for interacting with the system
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HotelReservationSystem system = new HotelReservationSystem();

        while (true) {
            System.out.println("\nHotel Reservation System Menu:");
            System.out.println("1. Add a Reservation");
            System.out.println("2. View All Reservations");
            System.out.println("3. Update a Reservation");
            System.out.println("4. Delete a Reservation");
            System.out.println("5. Exit");

            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (choice) {
                case 1:
                    // Add a reservation
                    System.out.print("Enter Room Number: ");
                    int roomNumber = scanner.nextInt();
                    scanner.nextLine();
                    System.out.print("Enter Guest Name: ");
                    String guestName = scanner.nextLine();
                    System.out.print("Enter Contact Information: ");
                    String contactInfo = scanner.nextLine();
                    System.out.print("Enter Check-in Date (YYYY-MM-DD): ");
                    String checkInDate = scanner.nextLine();
                    System.out.print("Enter Check-out Date (YYYY-MM-DD): ");
                    String checkOutDate = scanner.nextLine();
                    system.addReservation(roomNumber, guestName, contactInfo, checkInDate, checkOutDate);
                    break;
                case 2:
                    // View all reservations
                    system.viewReservations();
                    break;
                case 3:
                    // Update a reservation
                    System.out.print("Enter Reservation ID to update: ");
                    int idToUpdate = scanner.nextInt();
                    scanner.nextLine();
                    System.out.print("Enter New Guest Name: ");
                    String newGuestName = scanner.nextLine();
                    System.out.print("Enter New Contact Information: ");
                    String newContactInfo = scanner.nextLine();
                    system.updateReservation(idToUpdate, newGuestName, newContactInfo);
                    break;
                case 4:
                    // Delete a reservation
                    System.out.print("Enter Reservation ID to delete: ");
                    int idToDelete = scanner.nextInt();
                    system.deleteReservation(idToDelete);
                    break;
                case 5:
                    // Exit
                    System.out.println("Exiting...");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }
}
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java
Hotel Booking System in Java

New Project :-https://www.youtube.com/@Decodeit2

See also  Insurance Management System In Python Free Source Code

Explanation of the System

  • Reservation Class: Holds information about a single reservation, such as the room number, guest details, and dates.
  • HotelReservationSystem Class: Contains an ArrayList to store the reservations and provides methods to add, view, update, and delete reservations.
  • Main Menu: Allows users to interact with the system using a menu-driven interface to add new reservations, view all reservations, update, or delete them.

Running the Application

  1. Copy and paste the code into a Java file (HotelReservationSystem.java).
  2. Compile and run the program:
   javac HotelReservationSystem.java
   java HotelReservationSystem

Once the program runs, you’ll be presented with a menu where you can choose to add a reservation, view all reservations, update a reservation, or delete a reservation.

Feel free to modify and expand the project as per your requirements!


Usage 📋

After running the application, the system will prompt you with a menu to choose the operation you want to perform. The options are:

  • Make a new reservation
  • View all reservations
  • Edit existing reservations
  • Delete reservations
  • Exit the system

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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