0% found this document useful (0 votes)
43 views11 pages

Java Airline Ticket Booking System

The document describes an airline ticket booking application written in Java. The application allows users to book and cancel tickets, view booked tickets, and manage passenger data. It is structured using classes like Ticket, AirlineTicketBooking, and AirlineTicketBookingApp. The Ticket class stores ticket details, AirlineTicketBooking handles booking and cancellation functions, and AirlineTicketBookingApp runs the command-line interface. The program employs object-oriented design and comprehensive exception handling.

Uploaded by

Klaus mikaelson
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)
43 views11 pages

Java Airline Ticket Booking System

The document describes an airline ticket booking application written in Java. The application allows users to book and cancel tickets, view booked tickets, and manage passenger data. It is structured using classes like Ticket, AirlineTicketBooking, and AirlineTicketBookingApp. The Ticket class stores ticket details, AirlineTicketBooking handles booking and cancellation functions, and AirlineTicketBookingApp runs the command-line interface. The program employs object-oriented design and comprehensive exception handling.

Uploaded by

Klaus mikaelson
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

Name:Nikhil Danbahadur Saroj

Class: SYCS-A
Rollno:5859
Subject: Core Java

CA2
Topic: Airline Ticket Booking Application

Abstract
The Airline Ticket Booking Application is a command-line Java program
designed to facilitate the booking, cancellation, and management of airline
tickets. The application employs object-oriented principles to organize ticket
data and provides users with a menu-driven interface to interact with various
booking functions. The code includes comprehensive exception handling to
ensure robustness and graceful handling of runtime errors.
The Airline Ticket Booking Application is a Java program that offers users the
ability to book tickets, cancel bookings, view booked tickets, and manage
passenger data. The program is structured using several classes, each
responsible for specific functionalities. The core classes are Ticket,
AirlineTicketBooking, and AirlineTicketBookingApp.

Code:
import [Link];
import [Link];
import [Link];
import [Link];

class Ticket {
String passengerName;
String date;
String source;
String destination;
String ticketClass;
int quantity;
double price;

Ticket(String passengerName, String date, String source, String destination,


String ticketClass, int quantity) {
[Link] = passengerName;
[Link] = date;
[Link] = source;
[Link] = destination;
[Link] = ticketClass;
[Link] = quantity;

if ([Link]("Economy")) {
[Link] = 1000 * quantity;
} else if ([Link]("Premium Economy")) {
[Link] = 2000 * quantity;
} else if ([Link]("Business")) {
[Link] = 3000 * quantity;
} else if ([Link]("First Class")) {
[Link] = 5000 * quantity;
}
}

void display() {
[Link]("Passenger Name: " + passengerName);
[Link]("Date: " + date);
[Link]("Source: " + source);
[Link]("Destination: " + destination);
[Link]("Ticket Class: " + ticketClass);
[Link]("Quantity: " + quantity);
[Link]("Price: " + price);
}
}

class AirlineTicketBooking {
List<Ticket> tickets;

AirlineTicketBooking() {
tickets = new ArrayList<>();
}

void bookTicket(Ticket ticket) {


[Link](ticket);
}

double cancelTicket(Ticket ticket) {


if ([Link](ticket)) {
return [Link] * 0.95;
} else {
return 0;
}
}
void displayAllTickets() {
for (Ticket ticket : tickets) {
[Link]();
[Link]();
}
}

List<String> getAllPassengerNames() {
List<String> passengerNames = new ArrayList<>();
for (Ticket ticket : tickets) {
[Link]([Link]);
}
return passengerNames;
}
}

class AirlineTicketBookingApp {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
AirlineTicketBooking airlineTicketBooking = new AirlineTicketBooking();

while (true) {
try{
[Link]("Welcome to the Airline Ticket Booking!");
[Link]("1. Book Ticket");
[Link]("2. Cancel Ticket");
[Link]("3. View All Tickets");
[Link]("4. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
[Link]();

if (choice == 1) {
[Link]("Date (dd MMMM): ");
String date = [Link]();

[Link]("Source: ");
String source = [Link]();

[Link]("Destination: ");
String destination = [Link]();

[Link]("Ticket Class (Economy, Premium Economy,


Business, First Class): ");
String ticketClass = [Link]();

[Link]("Quantity: ");
int quantity = [Link]();
[Link]();

List<Ticket> ticketsToBook = new ArrayList<>();


for (int i = 1; i <= quantity; i++) {
[Link]("Passenger Name " + i + ": ");
String passengerName = [Link]();
Ticket ticket = new Ticket(passengerName, date, source,
destination, ticketClass, 1);
[Link](ticket);
}

double totalCost = 0.0;


[Link]("---- Tickets to Book ----");
for (Ticket ticket : ticketsToBook) {
totalCost += [Link];
[Link]();
[Link]();
}
[Link]("Total Cost (for all passengers): $" + totalCost);

[Link]("Do you want to proceed with the booking? (Y/N):


");
String confirmation = [Link]();
if ([Link]("Y")) {
for (Ticket ticket : ticketsToBook) {
[Link](ticket);
}
[Link]("Ticket(s) booked successfully!");
} else {
[Link]("Booking canceled.");
}
} else if (choice == 2) {
[Link]("Select the passenger to cancel their ticket: ");
List<String> passengerNames =
[Link]();
for (String name : passengerNames) {
[Link]("[" + name + "] ");
}
[Link]();

String passengerToCancel = [Link]();


boolean canceled = false;

for (Ticket ticket : [Link]) {


if ([Link](passengerToCancel)) {
double refundAmount =
[Link](ticket);
[Link]("Ticket for passenger " + passengerToCancel
+ " cancelled successfully! Refund Amount: $" + refundAmount);
canceled = true;
break;
}
}

if (!canceled) {
[Link]("No booking found for the given passenger
name.");
}
} else if (choice == 3) {
[Link]("---- All Tickets ----");
[Link]();
double totalCost = 0.0;
for (Ticket ticket : [Link]) {
totalCost += [Link];
}
[Link]("Total Cost of all Tickets: $" + totalCost);
} else if (choice == 4) {
[Link]("Exiting the program.");
break;
} else {
[Link]("Invalid choice! Please try again.");
}
} catch (InputMismatchException e) {
[Link]("Invalid input. Please enter a valid choice.");
[Link](); // Clear the input buffer
} catch (ArithmeticException e) {
[Link]("Arithmetic error occurred: " + [Link]());
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of bounds: " + [Link]());
} catch (NullPointerException e) {
[Link]("NullPointerException occurred: " +
[Link]());
} catch (NumberFormatException e) {
[Link]("Invalid number format: " + [Link]());

} catch (StringIndexOutOfBoundsException e) {
[Link]("String index out of bounds: " + [Link]());
}
}
}
}

Output:
Conclusion
The Airline Ticket Booking Application showcases the effective use of object-
oriented programming principles and exception handling in Java. It provides a
practical example of how to structure a program with multiple classes to
manage a specific domain, in this case, airline ticket booking. By incorporating
robust exception handling, the program maintains stability and user-friendliness
in the face of unexpected events.

Common questions

Powered by AI

The Airline Ticket Booking Application uses object-oriented principles such as encapsulation, inheritance, and polymorphism. Encapsulation is implemented by defining classes like Ticket and AirlineTicketBooking, which contain methods and attributes necessary for their specific functionalities, thus hiding the internal details from the user. The application does not explicitly illustrate inheritance and polymorphism but applies polymorphic principles during exception handling, where different types of exceptions are caught using a hierarchy, improving code robustness and adaptability to different errors .

A command-line interface provides simplicity and low resource demand, allowing quick interaction for users familiar with text interfaces. It is easy to develop and debug, which is suitable for an educational context. However, the drawbacks include a steep learning curve for users unfamiliar with command-line interactions, limited visual appeal, and lack of intuitive navigation compared to graphical interfaces. Complex tasks can lead to user errors, highlighting the importance of clear instructions and output messages .

The application ensures data encapsulation by defining classes that contain both data members and methods for operating on this data. For instance, the Ticket class encapsulates passenger details and ticket information, with methods to calculate price and display ticket information. This promotes modularity, as it prevents direct access to data variables from outside the class, thus maintaining integrity and preventing accidental interference with object state .

The Ticket class serves as the foundational data structure for representing individual booking details, including passenger name, flight date, route, and ticket class. It supports architecture by encapsulating ticket-related operations like display and price calculation. This class-centric approach centralizes data management and operation logic, facilitating ease of maintenance and extensibility as the application grows, promoting single responsibility principle and enhancing the robustness of the architecture .

Implementing a discount feature involves modifying the price calculation logic to account for user categories (e.g., students, seniors). This can be achieved by introducing a UserCategory class with discount attributes, interfaced with the Ticket or AirlineTicketBooking class to adjust prices based on user details stored in a separate data repository. Architecturally, this requires a new integration layer ensuring that discount logic is decoupled from core business functions, maintaining current modularity and facilitating future feature expansion .

User interaction is managed through a menu-driven interface within the command-line environment, offering options for booking, canceling, and viewing tickets. The program uses a Scanner object to capture input, processing it through control structures. This interaction model is simple but lacks intuitive feedback mechanisms beyond text prompts, and it does not account for complex scenarios like user errors in a rich manner, which could be improved by implementing logical checks or validation feedback loops .

The application uses ArrayList to manage tickets, offering dynamic sizing and straightforward element access. While sufficient for moderate data volumes, challenges arise in scaling due to linear operations for searches, deletions, and additions, impacting performance with large datasets. Potential solutions include transitioning to more efficient data structures like hash tables or trees, which provide better scalability characteristics. Such changes necessitate revising logic for ticket management and entail significant design considerations .

Price calculation is managed through conditionals within the Ticket class constructor, setting prices based on the ticket class (Economy, Premium Economy, Business, First Class). Each ticket class has a predefined multiplier, thus making price calculation straightforward but rigid. While this allows for quick implementation, it limits flexibility, as changes require code modifications. For greater flexibility, an external configuration or method-based approach could be used, enabling dynamic adjustments without altering the core logic .

The application uses try-catch blocks to handle exceptions such as InputMismatchException, ArithmeticException, NullPointerException, and others. This approach ensures that the program can catch unexpected runtime errors gracefully, providing specific error messages to the user while avoiding abrupt program termination. This enhances user experience by ensuring the application remains stable and can recover from erroneous inputs or situations .

The application can be extended by incorporating functionalities such as user authentication, flight scheduling, and seat selection. This requires designing additional classes and interfaces, adhering to object-oriented principles to ensure maintainability. For example, a User class could manage authentication, while FlightSchedule and Seat classes handle respective functionalities. Extending the system mandates careful architecture planning to manage dependencies and ensure scalability while maintaining coherence and reducing code duplication .

You might also like