Online Reservation System Using Java With Source Code
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 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 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
Complain Management using Python with a Graphical User Interface (GUI) Introduction: The Complain Management using Python program designed to manage complaints effectively …
COVID 19 Hospital Management Using Python [Django Framework] Introduction: The COVID-19 Hospital Management is a Python-based application that tracks web applications for Hospitals. …
Drawing Ganesha Using Python Turtle Graphics[Drawing Ganapati Using Python] Introduction In this blog post, we will learn how to draw Lord Ganesha …
Contact Management System in Python with a Graphical User Interface (GUI) Introduction: The Contact Management System is a Python-based application designed to …
KBC Game using Python with Source Code Introduction : Welcome to this blog post on building a “Kaun Banega Crorepati” (KBC) game …
Basic Logging System in C++ With Source Code Introduction : It is one of the most important practices in software development. Logging …