Java Ca2 Sample

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

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

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


String ticketClass, int quantity) {
this.passengerName = passengerName;
this.date = date;
this.source = source;
this.destination = destination;
this.ticketClass = ticketClass;
this.quantity = quantity;

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

void bookTicket(Ticket ticket) {


tickets.add(ticket);
}

double cancelTicket(Ticket ticket) {


if (tickets.remove(ticket)) {
return ticket.price * 0.95;
} else {
return 0;
}
}
void displayAllTickets() {
for (Ticket ticket : tickets) {
ticket.display();
System.out.println();
}
}

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("Ticket Class (Economy, Premium Economy,


Business, First Class): ");
String ticketClass = scanner.nextLine();

System.out.print("Quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine();

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


for (int i = 1; i <= quantity; i++) {
System.out.print("Passenger Name " + i + ": ");
String passengerName = scanner.nextLine();
Ticket ticket = new Ticket(passengerName, date, source,
destination, ticketClass, 1);
ticketsToBook.add(ticket);
}

double totalCost = 0.0;


System.out.println("---- Tickets to Book ----");
for (Ticket ticket : ticketsToBook) {
totalCost += ticket.price;
ticket.display();
System.out.println();
}
System.out.println("Total Cost (for all passengers): $" + totalCost);

System.out.print("Do you want to proceed with the booking? (Y/N):


");
String confirmation = scanner.nextLine();
if (confirmation.equalsIgnoreCase("Y")) {
for (Ticket ticket : ticketsToBook) {
airlineTicketBooking.bookTicket(ticket);
}
System.out.println("Ticket(s) booked successfully!");
} else {
System.out.println("Booking canceled.");
}
} else if (choice == 2) {
System.out.print("Select the passenger to cancel their ticket: ");
List<String> passengerNames =
airlineTicketBooking.getAllPassengerNames();
for (String name : passengerNames) {
System.out.print("[" + name + "] ");
}
System.out.println();

String passengerToCancel = scanner.nextLine();


boolean canceled = false;

for (Ticket ticket : airlineTicketBooking.tickets) {


if (ticket.passengerName.equals(passengerToCancel)) {
double refundAmount =
airlineTicketBooking.cancelTicket(ticket);
System.out.println("Ticket for passenger " + passengerToCancel
+ " cancelled successfully! Refund Amount: $" + refundAmount);
canceled = true;
break;
}
}

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.

You might also like