Flight Registration System
Flight Registration System
T.B.Harsha Vardhan-2021BCSE07AED197
Tharun Chakali -2021BCSE07AED201
CODE:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Flight {
private int flightNumber;
private String airline;
private String source;
private String destination;
private int availableSeats;
private double price;
public Flight(int flightNumber, String airline, String source, String destination, int
availableSeats, double price) {
this.flightNumber = flightNumber;
this.airline = airline;
this.source = source;
this.destination = destination;
this.availableSeats = availableSeats;
this.price = price;
}
class Reservation {
private int reservationId;
private String passengerName;
private int flightNumber;
public AirlinesReservationSystem() {
flights = new ArrayList<>();
reservations = new ArrayList<>();
nextReservationId = 1;
}
if (selectedFlight != null) {
selectedFlight.bookSeat();
Reservation reservation = new Reservation(nextReservationId++, passengerName,
flightNumber);
reservations.add(reservation);
System.out.println("Seat booked successfully. Reservation ID: " +
reservation.getReservationId());
} else {
System.out.println("Invalid flight number.");
}
}
}
while (true) {
System.out.println("1. Add a flight");
System.out.println("2. Display available flights");
System.out.println("3. Book a flight");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter flight number: ");
int flightNumber = scanner.nextInt();
System.out.print("Enter airline: ");
scanner.nextLine(); // Consume the newline character
String airline = scanner.nextLine();
System.out.print("Enter source: ");
String source = scanner.nextLine();
System.out.print("Enter destination: ");
String destination = scanner.nextLine();
System.out.print("Enter available seats: ");
int availableSeats = scanner.nextInt();
System.out.print("Enter price: ");
double price = scanner.nextDouble();
case 2:
reservationSystem.displayFlights();
break;
case 3:
System.out.print("Enter flight number: ");
int bookFlightNumber = scanner.nextInt();
System.out.print("Enter passenger name: ");
scanner.nextLine(); // Consume the newline character
String passengerName = scanner.nextLine();
reservationSystem.bookFlight(bookFlightNumber, passengerName);
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
OUTPUT:
REPORT:
1. Overview: The code implements an airline reservation system in Java. It allows
users to add flights, display available flights, and book flights through a
command-line interface. The system maintains information about flights,
reservations, and passenger details.
2. Class Structure: The code consists of three classes:
Flight : Represents a flight with properties such as flight number, airline, source,
destination, available seats, and price. It includes methods to retrieve flight
information and book a seat.
Reservation : Represents a reservation made by a passenger for a specific flight.
It includes properties like reservation ID, passenger name, and flight number.
AirlinesReservationSystem : Acts as the main controller for the airline reservation
system. It maintains lists of flights and reservations, and provides methods to
add flights, display available flights, and book flights.
3. Flight Management: The AirlinesReservationSystem class provides functionality to
add flights and display available flights.
The addFlight method allows users to add a flight by providing flight details
such as flight number, airline, source, destination, available seats, and price.
The displayFlights method retrieves flight information from the list of flights
and displays it, including flight number, airline, source, destination, available
seats, and price.
4. Booking a Flight: The system allows users to book a flight using the bookFlight
method.
Users are prompted to enter the flight number and passenger name.
The system checks if the flight exists and if there are available seats.
If a seat is available, it decrements the available seats count, creates a
reservation with a unique ID, and adds it to the list of reservations.
The system provides feedback on the successful booking, including the
reservation ID.
If the flight doesn't exist or no seats are available, appropriate error messages
are displayed.
5. User Interaction: The AirlinesReservationSystemDemo class contains the main
method, serving as the entry point for the program.
It creates an instance of AirlinesReservationSystem and a Scanner object to handle
user input.
A menu is displayed to the user, allowing them to choose options like adding
a flight, displaying available flights, booking a flight, or exiting the program.
A while loop keeps the program running until the user chooses to exit.
6. Future Enhancements: The code provides a basic implementation of an airline
reservation system. Here are some possible enhancements:
Implement additional error handling and validation for user inputs.
Include more features like canceling reservations, searching for flights by
criteria (e.g., source, destination), and managing passenger information.
Store data persistently using a database instead of in-memory lists.
Implement a graphical user interface (GUI) for a more user-friendly
experience.
Overall, the provided code serves as a foundation for building an airline reservation
system. It can be extended and enhanced based on specific requirements and
business needs.