0% found this document useful (0 votes)
81 views

Flight Registration System

The document describes a Java program that implements an airline reservation system with three classes: Flight to represent flights, Reservation for reservations, and AirlinesReservationSystem as the controller. The system allows users to add flights, display flights, and book flights through a command-line interface. It maintains lists of flights and reservations in memory. Future enhancements proposed include additional validation, more features, using a database, and a GUI.

Uploaded by

Harsha Vardhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Flight Registration System

The document describes a Java program that implements an airline reservation system with three classes: Flight to represent flights, Reservation for reservations, and AirlinesReservationSystem as the controller. The system allows users to add flights, display flights, and book flights through a command-line interface. It maintains lists of flights and reservations in memory. Future enhancements proposed include additional validation, more features, using a database, and a GUI.

Uploaded by

Harsha Vardhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA PROGRAMMING-CS403

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;
}

public int getFlightNumber() {


return flightNumber;
}

public String getAirline() {


return airline;
}

public String getSource() {


return source;
}

public String getDestination() {


return destination;
}

public int getAvailableSeats() {


return availableSeats;
}

public double getPrice() {


return price;
}

public void bookSeat() {


if (availableSeats > 0) {
availableSeats--;
} else {
System.out.println("No available seats on this flight.");
}
}
}

class Reservation {
private int reservationId;
private String passengerName;
private int flightNumber;

public Reservation(int reservationId, String passengerName, int flightNumber) {


this.reservationId = reservationId;
this.passengerName = passengerName;
this.flightNumber = flightNumber;
}

public int getReservationId() {


return reservationId;
}

public String getPassengerName() {


return passengerName;
}

public int getFlightNumber() {


return flightNumber;
}
}
class AirlinesReservationSystem {
private List<Flight> flights;
private List<Reservation> reservations;
private int nextReservationId;

public AirlinesReservationSystem() {
flights = new ArrayList<>();
reservations = new ArrayList<>();
nextReservationId = 1;
}

public void addFlight(Flight flight) {


flights.add(flight);
}

public void displayFlights() {


for (Flight flight : flights) {
System.out.println("Flight Number: " + flight.getFlightNumber());
System.out.println("Airline: " + flight.getAirline());
System.out.println("Source: " + flight.getSource());
System.out.println("Destination: " + flight.getDestination());
System.out.println("Available Seats: " + flight.getAvailableSeats());
System.out.println("Price: " + flight.getPrice());
System.out.println("-----------------------");
}
}

public void bookFlight(int flightNumber, String passengerName) {


Flight selectedFlight = null;

for (Flight flight : flights) {


if (flight.getFlightNumber() == flightNumber) {
selectedFlight = flight;
break;
}
}

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.");
}
}
}

public class AirlinesReservationSystemDemo {


public static void main(String[] args) {
AirlinesReservationSystem reservationSystem = new AirlinesReservationSystem();
Scanner scanner = new Scanner(System.in);

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();

Flight flight = new Flight(flightNumber, airline, source, destination, availableSeats,


price);
reservationSystem.addFlight(flight);
System.out.println("Flight added successfully.");
break;

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.

You might also like