Online Guest Reservation System using Java

Online Reservation System using Java

Introduction

Welcome to Online Reservation System using Java. This system allows you to make reservations for guests in online mode. Online reservation systems have become increasingly popular in recent years, as more and more businesses look to streamline their booking process and provide a more convenient experience for their customers. In this blog, we will discuss how to build an online reservation system using Java. The online reservation system we will build will be a simple console-based application that allows users to make, view, and cancel reservations. For businesses, online reservation systems offer a number of benefits, including increased efficiency, reduced workload, improved customer experience, and the ability to manage and monitor reservations in real-time. Overall, online reservation systems have become a vital tool for businesses in today’s digital age, providing a streamlined and convenient way for customers to make reservations while helping businesses to operate more efficiently and effectively.

Explanation

The online reservation system we will build will be a simple console-based application that allows users to make, view, and cancel reservations. We will use the following components to build the system: Reservation Class, Reservation System Class and User Interface. We will discuss about each class and its functionality. Reservation class will represent a reservation and will have properties like name, date, and number of guests. This class consists of methods for name, ID, date etc. ‘this’ keyword is used to get reference of running class. Reservation System class will manage the reservations and will have methods for making a reservation, getting all reservations, getting a reservation by ID, and cancelling a reservation. Array list  has been used in this class to make things simple for adding reservation using ‘add’ function and deleting reservations with ‘remove’ function. User Interface will be a simple console-based interface that allows users to interact with the reservation system. For taking the inputs from user, we have used the scanner class. In while loop all the options have been mentioned as – Make a reservation, view all reservations, Cancel a reservation and Exit. Reservations can simply be made by giving information of user and calling the make reservation method. It will return output as reservation ID. Cancelling of reservation is take place by ID. It will delete reservation of given ID. Summing up all the things, OOPS and some basic java concepts are the core of this system. Happy Coding!

Source Code

				
					import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Reservation {
    private int id;
    private String name;
    private String date;
    private int numberOfGuests;

    public Reservation(int id, String name, String date, int numberOfGuests) {
        this.id = id;
        this.name = name;
        this.date = date;
        this.numberOfGuests = numberOfGuests;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDate() {
        return date;
    }

    public int getNumberOfGuests() {
        return numberOfGuests;
    }
}


 class ReservationSystem {
    private List<Reservation> reservations = new ArrayList<>();
    private int nextId = 1;

    public Reservation makeReservation(String name, String date, int numberOfGuests) {
        Reservation reservation = new Reservation(nextId++, name, date, numberOfGuests);
        reservations.add(reservation);
        return reservation;
    }

    public List<Reservation> getReservations() {
        return reservations;
    }

    public Reservation getReservationById(int id) {
        for (Reservation reservation : reservations) {
            if (reservation.getId() == id) {
                return reservation;
            }
        }
        return null;
    }

    public boolean cancelReservation(int id) {
        Reservation reservation = getReservationById(id);
        if (reservation != null) {
            reservations.remove(reservation);
            return true;
        }
        return false;
    }
}


 class ReservationSystemUI {
    private ReservationSystem reservationSystem = new ReservationSystem();

    public void start() {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. Make a reservation");
            System.out.println("2. View all reservations");
            System.out.println("3. Cancel a reservation");
            System.out.println("4. Exit");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("Name: ");
                    String name = scanner.nextLine();
                    System.out.print("Date: ");
                    String date = scanner.nextLine();
                    System.out.print("Number of guests: ");
                    int numberOfGuests = scanner.nextInt();
                    scanner.nextLine();

                    Reservation reservation = reservationSystem.makeReservation(name, date, numberOfGuests);
                    System.out.println("Reservation made with ID " + reservation.getId());
                    break;
                case 2:
                    System.out.println("Reservations:");
                    for (Reservation r : reservationSystem.getReservations()) {
                        System.out.println(r.getId() + " - " + r.getName() + " - " + r.getDate() + " - " + r.getNumberOfGuests());
                    }
                    break;
                case 3:
                    System.out.print("Reservation ID to cancel: ");
                    int id = scanner.nextInt();
                    scanner.nextLine();

                    if (reservationSystem.cancelReservation(id)) {
                        System.out.println("Reservation canceled");
                    } else {
                        System.out.println("Reservation not found");
                    }
                    break;
                case 4:
                    return;
                default:
                    System.out.println("Invalid choice");
            }

            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        ReservationSystemUI obj = new ReservationSystemUI();
        obj.start();
    }
}
				
			

Output

Find More Projects

Hostel Management System Using Python With Source Code by using Python Graphical User Interface GUI Introduction: Managing a hostel efficiently can be …

Contra Game Using Python with source code using Pygame Introduction: Remember the super fun Contra game where you fight aliens and jump …

Restaurant Billing Management System Using Python with Tkinter (Graphical User Interface) Introduction: In the bustling world of restaurants, efficiency is paramount, especially …

Jungle Dash Game Using Python with source code with pygame module Introduction: Dive into the excitement of jungle adventures with our new …

Building a Tetris Game with Python with source code Introduction: Welcome to the world of Tetris, where falling blocks meet strategic maneuvers …

Super Mario Game Using Python with source code Introduction: Remember playing Super Mario as a kid? It’s that classic game where you …

All Coding Handwritten Notes

Browse Handwritten Notes