OneCompiler

Ghhh

1660

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class AMSApplication {

private static ArrayList<Flight> flightList = new ArrayList<>();

public static void main(String[] args) {
    // Sample flight data
    flightList.add(new Flight(101, "Air India", "New York", "London", new Date(2024, 9, 10), 500));
    flightList.add(new Flight(102, "Delta", "Los Angeles", "Tokyo", new Date(2024, 9, 15), 800));
    flightList.add(new Flight(103, "Emirates", "Dubai", "Paris", new Date(2024, 9, 20), 600));

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter Origin:");
    String origin = scanner.nextLine();

    System.out.println("Enter Destination:");
    String destination = scanner.nextLine();

    System.out.println("Enter Travel Date (dd-MM-yyyy):");
    String travelDateStr = scanner.nextLine();

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date travelDate = sdf.parse(travelDateStr);

        searchFlightDetails(origin, destination, travelDate);

    } catch (Exception e) {
        System.out.println("Invalid date format. Please enter the date in dd-MM-yyyy format.");
    }

    scanner.close();
}

public static void searchFlightDetails(String origin, String destination, Date travelDate) {
    boolean flightFound = false;

    for (Flight flight : flightList) {
        if (flight.getOrigin().equalsIgnoreCase(origin) &&
            flight.getDestination().equalsIgnoreCase(destination) &&
            flight.getDateOfJourney().equals(travelDate)) {

            System.out.println("Flight ID: " + flight.getFlightID());
            System.out.println("Carrier Name: " + flight.getCarrierName());
            System.out.println("Origin: " + flight.getOrigin());
            System.out.println("Destination: " + flight.getDestination());
            System.out.println("Date of Journey: " + new SimpleDateFormat("dd-MM-yyyy").format(flight.getDateOfJourney()));
            System.out.println("AirFare: $" + flight.getAirFare());
            flightFound = true;
            break;
        }
    }

    if (!flightFound) {
        System.out.println("No flights available for the given search criteria.");
    }
}

}

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class AMSApplication {

private static ArrayList<Flight> flightList = new ArrayList<>();

public static void main(String[] args) {
    // Sample flight data
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        flightList.add(new Flight(101, "Air India", "New York", "London", sdf.parse("10-09-2024"), 500, 10, 20, 5));
        flightList.add(new Flight(102, "Delta", "Los Angeles", "Tokyo", sdf.parse("15-09-2024"), 800, 15, 25, 10));
        flightList.add(new Flight(103, "Emirates", "Dubai", "Paris", sdf.parse("20-09-2024"), 600, 8, 30, 7));
    } catch (Exception e) {
        System.out.println("Error parsing date.");
    }

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter Origin:");
    String origin = scanner.nextLine();

    System.out.println("Enter Destination:");
    String destination = scanner.nextLine();

    System.out.println("Enter Travel Date (dd-MM-yyyy):");
    String dateOfTravelStr = scanner.nextLine();

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date dateOfTravel = sdf.parse(dateOfTravelStr);

        searchFlights(origin, destination, dateOfTravel);

    } catch (Exception e) {
        System.out.println("Invalid date format. Please enter the date in dd-MM-yyyy format.");
    }

    scanner.close();
}

public static void searchFlights(String origin, String destination, Date dateOfTravel) {
    boolean flightFound = false;

    System.out.printf("%-10s %-12s %-15s %-15s %-15s %-10s%n",
            "Flight ID", "CarrierName", "Origin", "Destination", "Date of Journey", "AirFare");

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

    for (Flight flight : flightList) {
        if (flight.getOrigin().equalsIgnoreCase(origin) &&
            flight.getDestination().equalsIgnoreCase(destination) &&
            flight.getDateOfJourney().equals(dateOfTravel)) {

            System.out.printf("%-10d %-12s %-15s %-15s %-15s %-10d%n",
                    flight.getFlightID(),
                    flight.getCarrierName(),
                    flight.getOrigin(),
                    flight.getDestination(),
                    sdf.format(flight.getDateOfJourney()),
                    flight.getAirFare());

            flightFound = true;
        }
    }

    if (!flightFound) {
        System.out.println("No flights found for the given criteria.");
    }
}

}

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class AMSApplication {

private static ArrayList<Flight> flightList = new ArrayList<>();
private static ArrayList<Booking> bookingList = new ArrayList<>();
private static int loggedInUserID = 123; // Example user ID for the logged-in user
private static String loggedInUserName = "John Doe"; // Example user name for the logged-in user

public static void main(String[] args) {
    // Assuming some sample bookings are already made
    bookingList.add(new Booking(1, loggedInUserID, 101, 2, "Business", new Date(2024, 9, 10), "Booked", 1000));
    bookingList.add(new Booking(2, loggedInUserID, 102, 1, "Economy", new Date(2024, 9, 15), "Booked", 800));

    // Function call to view all bookings
    viewAllBookings();
}

public static void viewAllBookings() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

    System.out.println("-------------------------------------------------------------");
    System.out.println("Booking ID | Flight Name | User Name | No Of Seats | Seat Category | Date Of Travel | Booking Status | Booking Amount");
    System.out.println("-------------------------------------------------------------");

    for (Booking booking : bookingList) {
        if (booking.getUserID() == loggedInUserID) {
            String flightName = getFlightNameById(booking.getFlightID());
            System.out.printf("%10d | %-11s | %-9s | %-12d | %-13s | %-13s | %-14s | $%-13d%n",
                    booking.getBookingID(),
                    flightName,
                    loggedInUserName,
                    booking.getNoOfSeats(),
                    booking.getSeatCategory(),
                    sdf.format(booking.getDateOfTravel()),
                    booking.getBookingStatus(),
                    booking.getBookingAmount());
        }
    }
    System.out.println("-------------------------------------------------------------");
}

private static String getFlightNameById(int flightID) {
    for (Flight flight : flightList) {
        if (flight.getFlightID() == flightID) {
            return flight.getCarrierName();
        }
    }
    return "Unknown";
}

}

public class Booking {
private int bookingID;
private int userID;
private int flightID;
private int noOfSeats;
private String seatCategory;
private Date dateOfTravel;
private String bookingStatus;
private int bookingAmount;

// Constructor
public Booking(int bookingID, int userID, int flightID, int noOfSeats, String seatCategory, Date dateOfTravel, String bookingStatus, int bookingAmount) {
    this.bookingID = bookingID;
    this.userID = userID;
    this.flightID = flightID;
    this.noOfSeats = noOfSeats;
    this.seatCategory = seatCategory;
    this.dateOfTravel = dateOfTravel;
    this.bookingStatus = bookingStatus;
    this.bookingAmount = bookingAmount;
}

// Getters
public int getBookingID() { return bookingID; }
public int getUserID() { return userID; }
public int getFlightID() { return flightID; }
public int getNoOfSeats() { return noOfSeats; }
public String getSeatCategory() { return seatCategory; }
public Date getDateOfTravel() { return dateOfTravel; }
public String getBookingStatus() { return bookingStatus; }
public int getBookingAmount() { return bookingAmount; }

}

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class AMSApplication {

private static ArrayList<Flight> flightList = new ArrayList<>();
private static ArrayList<Booking> bookingList = new ArrayList<>();
private static int nextBookingID = 1;
private static int loggedInUserID = 123; // Example user ID for the logged-in user

public static void main(String[] args) {
    // Sample flight data
    flightList.add(new Flight(101, "Air India", "New York", "London", new Date(2024, 9, 10), 500, 10, 20, 5));
    flightList.add(new Flight(102, "Delta", "Los Angeles", "Tokyo", new Date(2024, 9, 15), 800, 15, 25, 10));
    flightList.add(new Flight(103, "Emirates", "Dubai", "Paris", new Date(2024, 9, 20), 600, 8, 30, 7));

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter FlightID:");
    int flightID = scanner.nextInt();

    System.out.println("Enter Number of Seats:");
    int noOfSeats = scanner.nextInt();

    System.out.println("Enter Seat Category (Business/Economy/Executive):");
    String seatCategory = scanner.next();

    System.out.println("Enter Date of Travel (dd-MM-yyyy):");
    String dateOfTravelStr = scanner.next();

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date dateOfTravel = sdf.parse(dateOfTravelStr);

        bookFlight(flightID, noOfSeats, seatCategory, dateOfTravel);

    } catch (Exception e) {
        System.out.println("Invalid date format. Please enter the date in dd-MM-yyyy format.");
    }

    scanner.close();
}

public static void bookFlight(int flightID, int noOfSeats, String seatCategory, Date dateOfTravel) {
    for (Flight flight : flightList) {
        if (flight.getFlightID() == flightID && flight.getDateOfJourney().equals(dateOfTravel)) {
            int availableSeats = 0;
            int seatPriceMultiplier = 1;

            switch (seatCategory.toLowerCase()) {
                case "business":
                    availableSeats = flight.getSeatCapacityBusinessClass();
                    seatPriceMultiplier = 3;
                    break;
                case "economy":
                    availableSeats = flight.getSeatCapacityEconomyClass();
                    seatPriceMultiplier = 1;
                    break;
                case "executive":
                    availableSeats = flight.getSeatCapacityExecutiveClass();
                    seatPriceMultiplier = 2;
                    break;
                default:
                    System.out.println("Invalid seat category. Please choose Business, Economy, or Executive.");
                    return;
            }

            if (noOfSeats > availableSeats) {
                System.out.println("No seats available under \"" + seatCategory + "\" Category for Booking. Please try searching in other seat category or search for another flight available on this route.");
            } else {
                // Calculate booking amount
                int bookingAmount = noOfSeats * flight.getAirFare() * seatPriceMultiplier;

                // Reduce the available seats
                switch (seatCategory.toLowerCase()) {
                    case "business":
                        flight.setSeatCapacityBusinessClass(availableSeats - noOfSeats);
                        break;
                    case "economy":
                        flight.setSeatCapacityEconomyClass(availableSeats - noOfSeats);
                        break;
                    case "executive":
                        flight.setSeatCapacityExecutiveClass(availableSeats - noOfSeats);
                        break;
                }

                // Create and add the booking
                Booking booking = new Booking(nextBookingID++, loggedInUserID, flightID, noOfSeats, seatCategory, dateOfTravel, "Booked", bookingAmount);
                bookingList.add(booking);

                System.out.println("Booking Successful! Your Booking ID is " + booking.getBookingID());
                System.out.println("Total Booking Amount: $" + booking.getBookingAmount());
            }
            return;
        }
    }
    System.out.println("No flight found with the given Flight ID and Date of Travel.");
}

}