Flight Ticket Booking System zoho round 3

Building a Flight Ticket Booking Application — Zoho Round 3 Solution

In this article, we’ll explore a practical solution to a problem statement posed in Zoho Round 3, where we need to implement a Flight Ticket Booking Application. This involves designing a reservation system with specific functionalities, which include booking tickets, applying booking conditions, handling cancellations, and printing flight details.

Let’s break down the problem and provide a detailed implementation.

Problem Statement

We need to implement the following modules and functionalities:

  1. Booking Ticket
  2. Booking Conditions:
  • Each flight has 50 seats.
  • Get passenger details, including flight name and seats required.
  • Ticket starting price is ₹5000.
  • Ticket price increases by ₹200 for every successful booking.

3. Cancel Ticket

4. Cancel Conditions:

  • Issue a refund for the canceled ticket.
  • Ticket price reduces by ₹200 per canceled seat.

5. Print Flight Details with Passengers

Solution Code

The solution involves four classes: PassengerFlightFlightReservationSystem, and Main. Below is the full implementation.

1. Passenger Class

This class represents a passenger’s booking information.

class Passenger {
    private String bookingId;
    private String name;
    private int age;
    private int seatsBooked;

    public Passenger(String bookingId, String name, int age, int seatsBooked) {
        this.bookingId = bookingId;
        this.name = name;
        this.age = age;
        this.seatsBooked = seatsBooked;
    }
    public String getBookingId() {
        return bookingId;
    }
    public int getSeatsBooked() {
        return seatsBooked;
    }
    @Override
    public String toString() {
        return "Passenger{bookingId='" + bookingId + "', name='" + name + "', age=" + age + ", seatsBooked=" + seatsBooked + '}';
    }
}

2. Flight Class

This class handles operations related to a specific flight, such as booking tickets, cancellations, and managing passenger data.

import java.util.*;

class Flight {
    private String flightName;
    private int availableSeats = 50; // Each flight starts with 50 seats
    private int ticketPrice = 5000; // Initial price of ₹5000
    private Map<String, Passenger> bookings = new HashMap<>();
    private int bookingCounter = 0;
    public Flight(String flightName) {
        this.flightName = flightName;
    }
    public String bookTickets(String passengerName, int age, int seats) {
        if (seats <= availableSeats) {
            bookingCounter++;
            String bookingId = "T" + bookingCounter;
            Passenger passenger = new Passenger(bookingId, passengerName, age, seats);
            bookings.put(bookingId, passenger);
            availableSeats -= seats;
            ticketPrice += 200 * seats; // Increase price for each seat booked
            return bookingId;
        } else {
            System.out.println("Booking failed: Not enough seats available.");
            return null;
        }
    }
    public boolean cancelBooking(String bookingId) {
        Passenger passenger = bookings.get(bookingId);
        if (passenger != null) {
            int seats = passenger.getSeatsBooked();
            availableSeats += seats;
            ticketPrice -= 200 * seats; // Decrease price for canceled seats
            bookings.remove(bookingId);
            System.out.println("Booking canceled successfully. Refund issued for " + seats + " seats.");
            return true;
        } else {
            System.out.println("Cancellation failed: Booking ID not found.");
            return false;
        }
    }
    public void displayDetails() {
        System.out.println("Flight: " + flightName);
        System.out.println("Available Seats: " + availableSeats);
        System.out.println("Current Ticket Price: " + ticketPrice);
    }
    public void printDetails() {
        System.out.println("Flight: " + flightName);
        System.out.println("Available Seats: " + availableSeats);
        System.out.println("Current Ticket Price: " + ticketPrice);
        System.out.println("Passengers:");
        for (Passenger passenger : bookings.values()) {
            System.out.println(passenger);
        }
    }
}

3. FlightReservationSystem Class

This class manages multiple flights and delegates operations to the relevant Flight objects.

import java.util.*;

class FlightReservationSystem {
    Map<String, Flight> flights = new HashMap<>();
    public FlightReservationSystem() {
        flights.put("Indigo", new Flight("Indigo"));
        flights.put("SpiceJet", new Flight("SpiceJet"));
    }
    public String bookTicket(String flightName, String passengerName, int age, int seats) {
        Flight flight = flights.get(flightName);
        if (flight != null) {
            return flight.bookTickets(passengerName, age, seats);
        } else {
            System.out.println("Booking failed: Flight not found.");
            return null;
        }
    }
    public boolean cancelTicket(String flightName, String bookingId) {
        Flight flight = flights.get(flightName);
        if (flight != null) {
            return flight.cancelBooking(bookingId);
        } else {
            System.out.println("Cancellation failed: Flight not found.");
            return false;
        }
    }
    public void displayFlightDetails(String flightName) {
        Flight flight = flights.get(flightName);
        if (flight != null) {
            flight.displayDetails();
        } else {
            System.out.println("Flight not found.");
        }
    }
    public void printFlightDetails(String flightName) {
        Flight flight = flights.get(flightName);
        if (flight != null) {
            flight.printDetails();
        } else {
            System.out.println("Flight not found.");
        }
    }
}

4. Main Class

The entry point for the application, where users can interact with the system.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        FlightReservationSystem system = new FlightReservationSystem();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n--- Flight Reservation System ---");
            System.out.println("1. Book Ticket");
            System.out.println("2. Cancel Ticket");
            System.out.println("3. Print Flight Details");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    System.out.print("Enter flight name (Indigo/SpiceJet): ");
                    String flightName = scanner.nextLine();
                    System.out.print("Enter passenger name: ");
                    String passengerName = scanner.nextLine();
                    System.out.print("Enter passenger age: ");
                    int age = scanner.nextInt();
                    System.out.print("Enter number of seats to book: ");
                    int seats = scanner.nextInt();
                    String bookingId = system.bookTicket(flightName, passengerName, age, seats);
                    if (bookingId != null) {
                        System.out.println("Booking successful! Your booking ID is: " + bookingId);
                    }
                    break;
                case 2:
                    System.out.print("Enter flight name (Indigo/SpiceJet): ");
                    flightName = scanner.nextLine();
                    System.out.print("Enter booking ID: ");
                    bookingId = scanner.nextLine();
                    system.cancelTicket(flightName, bookingId);
                    break;
                case 3:
                    System.out.print("Enter flight name (Indigo/SpiceJet): ");
                    flightName = scanner.nextLine();
                    system.printFlightDetails(flightName);
                    break;
                case 4:
                    System.out.println("Exiting system. Thank you!");
                    scanner.close();
                    return;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }
}

Conclusion

This solution captures the essence of real-world flight booking systems with essential functionalities. By modularizing the application into distinct classes, we achieve clear separation of concerns, making the code easier to maintain and extend.

Picture of aravind16101800@gmail.com

aravind16101800@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *