w3resource

Java: Search, book, cancel hotel and flight reservations

Java OOP: Exercise-19 with Solution

Write a Java program to create a class with methods to search for flights and hotels, and to book and cancel reservations.

Sample Solution:

Java Code:

//TravelApp.java
import java.util.ArrayList;
import java.util.Random;

public class TravelApp {
  private ArrayList < Flight > flights;
  private ArrayList < Hotel > hotels;

  public TravelApp() {
    this.flights = new ArrayList < Flight > ();
    this.hotels = new ArrayList < Hotel > ();
  }

  public void searchFlights(String origin, String destination, String date, int numPassengers) {
    System.out.println("Searching for flights from " + origin + " to " + destination + " on " + date + " for " + numPassengers + " passengers.");
  }

  public void searchHotels(String location, String checkIn, String checkOut, int numGuests) {
    System.out.println("Searching for hotels in " + location + " from " + checkIn + " to " + checkOut + " for " + numGuests + " guests.");
  }

  public void bookFlight(int flightNumber, String passengerName, String origin, String destination, String date, int numPassengers, double price) {
    Flight flight = new Flight(flightNumber, passengerName, origin, destination, date, numPassengers, price);
    int confirmationNumber = generateConfirmationNumber();
    flight.setConfirmationNumber(confirmationNumber);
    this.flights.add(flight);
    System.out.println("Flight booked! Confirmation number: " + confirmationNumber);
  }

  public void bookHotel(int hotelId, String guestName, String location, String checkIn, String checkOut, int numGuests, double price) {
    Hotel hotel = new Hotel(hotelId, guestName, location, checkIn, checkOut, numGuests, price);
    int confirmationNumber = generateConfirmationNumber();
    hotel.setConfirmationNumber(confirmationNumber);
    this.hotels.add(hotel);
    System.out.println("Hotel booked! Confirmation number: " + confirmationNumber);
  }

  public void cancelReservation(int confirmationNumber) {
    // Cancel a flight or hotel reservation based on confirmation number
    for (Flight flight: this.flights) {
      if (flight.getConfirmationNumber() == confirmationNumber) {
        this.flights.remove(flight);
        System.out.println("Flight reservation with confirmation number " + confirmationNumber + " cancelled.");
        return;
      }
    }
    for (Hotel hotel: this.hotels) {
      if (hotel.getConfirmationNumber() == confirmationNumber) {
        this.hotels.remove(hotel);
        System.out.println("Hotel reservation with confirmation number " + confirmationNumber + " cancelled.");
        return;
      }
    }
    System.out.println("No reservation found with confirmation number " + confirmationNumber + ".");
  }

  private int generateConfirmationNumber() {
    // Generate a random 6-digit confirmation number
    Random rand = new Random();
    return rand.nextInt(900000) + 100000;
  }
}

The above Java class is used for searching and booking flights and hotels, as well as cancelling reservations. It contains methods to search for flights and hotels based on specific criteria, book flights and hotels by creating new Flight and Hotel objects, cancel reservations by confirmation number, and generate a random confirmation number using the Random class in Java. It also contains private instance variables to store ArrayLists of Flight and Hotel objects that represent the current reservations.

//Flight.java
public class Flight {
    private int flightNumber;
	private String passengerName;
    private String origin;
    private String destination;
    private String date;
    private int numPassengers;
	private double price;
    private int confirmationNumber;

public Flight(int flightNumber, String passengerName, String origin, String destination, String date, int numPassengers, double price) {
    this.flightNumber = flightNumber;
    this.passengerName = passengerName;
    this.origin = origin;
    this.destination = destination;
    this.date = date;
    this.numPassengers = numPassengers;
    this.price = price;
}

    public int getFlightNumber() {
        return flightNumber;
    }
	
    public String getPassengerName() {
        return passengerName;
    }

    public String getOrigin() {
        return origin;
    }

    public String getDestination() {
        return destination;
    }

    public String getDate() {
        return date;
    }

    public int getNumPassengers() {
        return numPassengers;
    }
  
     public double getPrice() {
        return price;
    }
    public int getConfirmationNumber() {
        return confirmationNumber;
    }

    public void setConfirmationNumber(int confirmationNumber) {
        this.confirmationNumber = confirmationNumber;
    }
}

The above “Flight” class represents a flight. It has flight number, passenger name, origin, destination, date, number of passengers, price, and confirmation number. It has a constructor to create a Flight object and getters and setters to access and modify the object's properties. The confirmation number is set after a flight is booked to identify a reservation.

//Hotel.java
public class Hotel {
  private int hotelId;
  private String name;
  private String location;
  private String checkIn;
  private String checkOut;
  private int numGuests;
  private double price;
  private int confirmationNumber;

  public Hotel(int hotelId, String name, String location, String checkIn, String checkOut, int numGuests, double price) {
    this.hotelId = hotelId;
    this.name = name;
    this.location = location;
    this.checkIn = checkIn;
    this.checkOut = checkOut;
    this.numGuests = numGuests;
    this.price = price;
  }
  public int getHotelId() {
    return hotelId;
  }

  public String getName() {
    return name;
  }

  public String getLocation() {
    return location;
  }

  public String getCheckIn() {
    return checkIn;
  }

  public String getCheckOut() {
    return checkOut;
  }

  public double getPrice() {
    return price;
  }

  public int getNumGuests() {
    return numGuests;
  }

  public int getConfirmationNumber() {
    return confirmationNumber;
  }

  public void setConfirmationNumber(int confirmationNumber) {
    this.confirmationNumber = confirmationNumber;
  }
}

The above “Hotel” class represents a hotel, with an ID, a name, a location, a check-in date, a check-out date, a number of guests, a price, and a confirmation number. It contains a constructor that initializes these properties, as well as getter and setter methods for each property. The confirmation number is randomly generated and set through the setter method.

//Main.java
public class Main {
  public static void main(String[] args) {
    TravelApp app = new TravelApp();
    app.searchFlights("New York", "London", "2022-09-01", 1);
    app.searchHotels("London", "2022-08-01", "2022-09-05", 2);
    app.bookFlight(12345670, "Martin Nadine", "New York", "London", "2022-08-01", 1, 700.00);
    app.bookFlight(67843513, "Jennifer Ulrike", "New York", "London", "2022-08-01", 1, 655.00);
    app.bookHotel(98765432, "Martin Nadine", "London", "2022-09-01", "2022-09-05", 1, 100.00);
    // Cancel a flight or hotel reservation based on reservation number
    app.cancelReservation(12345670);
  }
}

In the main() method of the above class, an instance of the “TravelApp” class is created. Several methods of the “TravelApp” class are called to perform various tasks related to travel. These tasks include searching for flights and hotels, booking flights and hotels, and cancelling reservations.

In particular, the “searchFlights()” method is called with the arguments "New York", "London", "2022-09-01", and 1, to search for flights from New York to London on September 1, 2022 for one passenger. Similarly, the “searchHotels()” method is called with the arguments "London", "2022-08-01", "2022-09-05", and 2, to search for hotels in London from August 1, 2022 to September 5, 2022 for two guests.

Then, two flights and one hotel are booked using the “bookFlight()” and “bookHotel()” methods respectively, with different arguments. Finally, a reservation for one of the flights is cancelled using the “cancelReservation()” method with the argument 12345670.

Sample Output:

Searching for flights from New York to London on 2022-09-01 for 1 passengers.
Searching for hotels in London from 2022-08-01 to 2022-09-05 for 2 guests.
Flight booked! Confirmation number: 528140
Flight booked! Confirmation number: 664315
Hotel booked! Confirmation number: 392396
No reservation found with confirmation number 12345670.

Flowchart:

Flowchart: Java  OOP Exercises: Search, book, cancel hotel and flight reservations.
Flowchart: Java  OOP Exercises: Search, book, cancel hotel and flight reservations.
Flowchart: Java  OOP Exercises: Search, book, cancel hotel and flight reservations.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Restaurant menu, average rating.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.