Java Ca2 Sample
Java Ca2 Sample
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 java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
class Ticket {
String passengerName;
String date;
String source;
String destination;
String ticketClass;
int quantity;
double price;
if (ticketClass.equals("Economy")) {
this.price = 1000 * quantity;
} else if (ticketClass.equals("Premium Economy")) {
this.price = 2000 * quantity;
} else if (ticketClass.equals("Business")) {
this.price = 3000 * quantity;
} else if (ticketClass.equals("First Class")) {
this.price = 5000 * quantity;
}
}
void display() {
System.out.println("Passenger Name: " + passengerName);
System.out.println("Date: " + date);
System.out.println("Source: " + source);
System.out.println("Destination: " + destination);
System.out.println("Ticket Class: " + ticketClass);
System.out.println("Quantity: " + quantity);
System.out.println("Price: " + price);
}
}
class AirlineTicketBooking {
List<Ticket> tickets;
AirlineTicketBooking() {
tickets = new ArrayList<>();
}
List<String> getAllPassengerNames() {
List<String> passengerNames = new ArrayList<>();
for (Ticket ticket : tickets) {
passengerNames.add(ticket.passengerName);
}
return passengerNames;
}
}
class AirlineTicketBookingApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
AirlineTicketBooking airlineTicketBooking = new AirlineTicketBooking();
while (true) {
try{
System.out.println("Welcome to the Airline Ticket Booking!");
System.out.println("1. Book Ticket");
System.out.println("2. Cancel Ticket");
System.out.println("3. View All Tickets");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.print("Date (dd MMMM): ");
String date = scanner.nextLine();
System.out.print("Source: ");
String source = scanner.nextLine();
System.out.print("Destination: ");
String destination = scanner.nextLine();
System.out.print("Quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine();
if (!canceled) {
System.out.println("No booking found for the given passenger
name.");
}
} else if (choice == 3) {
System.out.println("---- All Tickets ----");
airlineTicketBooking.displayAllTickets();
double totalCost = 0.0;
for (Ticket ticket : airlineTicketBooking.tickets) {
totalCost += ticket.price;
}
System.out.println("Total Cost of all Tickets: $" + totalCost);
} else if (choice == 4) {
System.out.println("Exiting the program.");
break;
} else {
System.out.println("Invalid choice! Please try again.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid choice.");
scanner.nextLine(); // Clear the input buffer
} catch (ArithmeticException e) {
System.out.println("Arithmetic error occurred: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds: " + e.getMessage());
} catch (NullPointerException e) {
System.out.println("NullPointerException occurred: " +
e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Invalid number format: " + e.getMessage());
} catch (StringIndexOutOfBoundsException e) {
System.out.println("String index out of bounds: " + e.getMessage());
}
}
}
}
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.