
Introduction:
In this project, we’ll show you how to create an Online Guest reservation system using java. This is a command-line user interface-based Guest reservation system that allows you to check room availability and reserve a room if it is available. The system is designed with a switch case that gives you three options: make reservation view reservation, and cancel reservation. So, get a cup of coffee and use the Guest reservation system.
Explanation:
This Guest Reservation System provides a simple and easy way for reserving the room if available, view the reservations and cancel the reservation. The system includes a simple command line interface with a switch case for reserving room, viewing reservations and cancelling the reservation.
Switch Case, while loop, ArrayList class and Scanner class are used to build this system. The system will first present the user with menu including make reservation, view reservation, cancel reservation and exit. The user must choose a required choice. The operation will be carried out in according to the input selection. The reservations are stored in ArrayList class. The makeReservation() method first asks your name and room be to reserved, if the room is available the reservation will be confirm else it will display “Sorry, this room is already reserved.”
Source Code:
import java.util.*;
class Reservation {
private String guestName;
private int roomNumber;
private boolean isReserved;
public Reservation(String guestName, int roomNumber) {
this.guestName = guestName;
this.roomNumber = roomNumber;
this.isReserved = true;
}
public String getGuestName() {
return guestName;
}
public int getRoomNumber() {
return roomNumber;
}
public boolean isReserved() {
return isReserved;
}
public void cancelReservation() {
isReserved = false;
}
}
public class Main {
private static List<Reservation> reservations = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Make Reservation");
System.out.println("2. View Reservation");
System.out.println("3. Cancel Reservation");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
makeReservation(scanner);
break;
case 2:
viewReservation();
break;
case 3:
cancelReservation(scanner);
break;
case 4:
System.out.println("Exiting... Thank you!");
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void makeReservation(Scanner scanner) {
System.out.print("Enter your name: ");
String guestName = scanner.nextLine();
System.out.print("Enter room number: ");
int roomNumber = scanner.nextInt();
for (Reservation reservation : reservations) {
if (reservation.getRoomNumber() == roomNumber && reservation.isReserved()) {
System.out.println("Sorry, this room is already reserved.");
return;
}
}
Reservation reservation = new Reservation(guestName, roomNumber);
reservations.add(reservation);
System.out.println("Reservation successful!");
}
private static void viewReservation() {
if (reservations.isEmpty()) {
System.out.println("No reservations found.");
} else {
System.out.println("Reservations:");
for (Reservation reservation : reservations) {
System.out.println("Room: " + reservation.getRoomNumber() + ", Guest: " + reservation.getGuestName());
}
}
}
private static void cancelReservation(Scanner scanner) {
System.out.print("Enter room number to cancel reservation: ");
int roomNumber = scanner.nextInt();
for (Reservation reservation : reservations) {
if (reservation.getRoomNumber() == roomNumber && reservation.isReserved()) {
reservation.cancelReservation();
System.out.println("Reservation for room " + roomNumber + " cancelled.");
return;
}
}
System.out.println("No reservation found for room " + roomNumber);
}
}
Output:


