OneCompiler

RDX_JAMS

1640
import java.util.HashMap;
import java.util.Scanner;

public class AirlineManagementSystem {

    // Default Admin credentials
    private static final String ADMIN_USER = "admin";
    private static final String ADMIN_PASS = "password";

    // HashMaps to store customer and carrier details (simulating a database)
    private static HashMap<String, Customer> customers = new HashMap<>();
    private static HashMap<String, Carrier> carriers = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Welcome message
        System.out.println("Welcome to Airline Management System - AMS");
        System.out.println("1. Admin Sign-in");
        System.out.println("2. Customer Sign-in");
        System.out.print("Enter choice (1 or 2): ");

        int choice = scanner.nextInt();
        scanner.nextLine();  // Consume newline

        switch (choice) {
            case 1:
                adminLogin(scanner);
                break;
            case 2:
                customerLogin(scanner);
                break;
            default:
                System.out.println("Invalid choice! Please enter 1 or 2.");
        }

        scanner.close();
    }

    // Admin login method
    private static void adminLogin(Scanner scanner) {
        System.out.print("Enter Admin UserId: ");
        String userId = scanner.nextLine();

        System.out.print("Enter Admin Password: ");
        String password = scanner.nextLine();

        if (userId.equals(ADMIN_USER) && password.equals(ADMIN_PASS)) {
            System.out.println("Admin logged in successfully!");
            adminMenu(scanner);
        } else {
            System.out.println("Invalid Admin credentials!");
        }
    }

    // Customer login method
    private static void customerLogin(Scanner scanner) {
        System.out.print("Enter Customer UserId: ");
        String userId = scanner.nextLine();

        System.out.print("Enter Customer Password: ");
        String password = scanner.nextLine();

        if (customers.containsKey(userId) && customers.get(userId).getPassword().equals(password)) {
            System.out.println("Customer logged in successfully!");
            customerMenu(scanner, userId);
        } else {
            System.out.println("Invalid Customer credentials or user does not exist!");
        }
    }

    // Admin menu for managing carriers
    private static void adminMenu(Scanner scanner) {
        while (true) {
            System.out.println("\nAdmin Menu:");
            System.out.println("1. Add Carrier");
            System.out.println("2. Edit Carrier Details by CarrierId");
            System.out.println("3. Remove Carrier by Id");
            System.out.println("4. Flight Cancellation - Refund Price Calculation");
            System.out.println("5. Exit AMS");
            System.out.print("Enter choice: ");

            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (choice) {
                case 1:
                    addCarrier(scanner);
                    break;
                case 2:
                    editCarrier(scanner);
                    break;
                case 3:
                    removeCarrier(scanner);
                    break;
                case 4:
                    flightCancellation(scanner);
                    break;
                case 5:
                    System.out.println("Exiting AMS...");
                    return;
                default:
                    System.out.println("Invalid choice! Please try again.");
            }
        }
    }

    // Customer menu for managing profile and booking
    private static void customerMenu(Scanner scanner, String userId) {
        while (true) {
            System.out.println("\nCustomer Menu:");
            System.out.println("1. Customer Registration");
            System.out.println("2. Edit Customer Profile");
            System.out.println("3. Ticket Booking - Price Calculation");
            System.out.println("4. Ticket Cancellation - Refund Price Calculation");
            System.out.println("5. Exit AMS");
            System.out.print("Enter choice: ");

            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (choice) {
                case 1:
                    customerRegistration(scanner);
                    break;
                case 2:
                    editCustomerProfile(scanner, userId);
                    break;
                case 3:
                    ticketBooking(scanner, userId);
                    break;
                case 4:
                    ticketCancellation(scanner, userId);
                    break;
                case 5:
                    System.out.println("Exiting AMS...");
                    return;
                default:
                    System.out.println("Invalid choice! Please try again.");
            }
        }
    }

    // Add carrier (flight details) method
    private static void addCarrier(Scanner scanner) {
        System.out.print("Enter Carrier Id: ");
        String carrierId = scanner.nextLine();

        System.out.print("Enter Carrier Name: ");
        String name = scanner.nextLine();

        System.out.print("Enter Flight Capacity: ");
        int capacity = scanner.nextInt();

        System.out.print("Enter Ticket Price: ");
        double ticketPrice = scanner.nextDouble();
        scanner.nextLine();  // Consume newline

        Carrier carrier = new Carrier(carrierId, name, capacity, ticketPrice);
        carriers.put(carrierId, carrier);
        System.out.println("Carrier added successfully!");
    }

    // Edit carrier details by CarrierId
    private static void editCarrier(Scanner scanner) {
        System.out.print("Enter Carrier Id to edit: ");
        String carrierId = scanner.nextLine();

        if (carriers.containsKey(carrierId)) {
            Carrier carrier = carriers.get(carrierId);

            System.out.println("Editing carrier: " + carrier.getName());
            System.out.print("Enter new Carrier Name: ");
            String newName = scanner.nextLine();
            carrier.setName(newName);

            System.out.print("Enter new Flight Capacity: ");
            int newCapacity = scanner.nextInt();

            System.out.print("Enter new Ticket Price: ");
            double newTicketPrice = scanner.nextDouble();
            scanner.nextLine();  // Consume newline

            carrier.setCapacity(newCapacity);
            carrier.setTicketPrice(newTicketPrice);

            System.out.println("Carrier details updated successfully!");
        } else {
            System.out.println("Carrier with Id " + carrierId + " not found!");
        }
    }

    // Remove carrier by CarrierId
    private static void removeCarrier(Scanner scanner) {
        System.out.print("Enter Carrier Id to remove: ");
        String carrierId = scanner.nextLine();

        if (carriers.remove(carrierId) != null) {
            System.out.println("Carrier removed successfully!");
        } else {
            System.out.println("Carrier with Id " + carrierId + " not found!");
        }
    }

    // Flight cancellation and refund calculation
    private static void flightCancellation(Scanner scanner) {
        System.out.print("Enter Carrier Id for flight cancellation: ");
        String carrierId = scanner.nextLine();

        if (carriers.containsKey(carrierId)) {
            Carrier carrier = carriers.get(carrierId);
            double refundAmount = carrier.getTicketPrice() * carrier.getCapacity() * 0.75; // 75% refund to customers
            double lossAmount = carrier.getTicketPrice() * carrier.getCapacity() * 0.25;  // 25% loss to carrier

            System.out.println("Refund amount to customers: $" + refundAmount);
            System.out.println("Loss to the carrier: $" + lossAmount);
        } else {
            System.out.println("Carrier with Id " + carrierId + " not found!");
        }
    }

    // Customer registration
    private static void customerRegistration(Scanner scanner) {
        System.out.print("Enter Customer UserId: ");
        String userId = scanner.nextLine();

        System.out.print("Enter Customer Password: ");
        String password = scanner.nextLine();

        if (!customers.containsKey(userId)) {
            customers.put(userId, new Customer(userId, password));
            System.out.println("Customer registered successfully!");
        } else {
            System.out.println("User already exists!");
        }
    }

    // Edit customer profile
    private static void editCustomerProfile(Scanner scanner, String userId) {
        Customer customer = customers.get(userId);

        if (customer != null) {
            System.out.print("Enter new Password: ");
            String newPassword = scanner.nextLine();
            customer.setPassword(newPassword);
            System.out.println("Customer profile updated successfully!");
        } else {
            System.out.println("Customer not found!");
        }
    }

    // Ticket booking and price calculation
    private static void ticketBooking(Scanner scanner, String userId) {
        System.out.print("Enter Carrier Id: ");
        String carrierId = scanner.nextLine();

        if (carriers.containsKey(carrierId)) {
            Carrier carrier = carriers.get(carrierId);
            System.out.print("Enter number of tickets: ");
            int numOfTickets = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            if (numOfTickets <= carrier.getCapacity()) {
                double totalPrice = numOfTickets * carrier.getTicketPrice();
                carrier.setCapacity(carrier.getCapacity() - numOfTickets);
                System.out.println("Booking successful! Total cost: $" + totalPrice);
            } else {
                System.out.println("Not enough seats available!");
            }
        } else {
            System.out.println("Carrier not found!");
        }
    }

    // Ticket cancellation and refund calculation
    private static void ticketCancellation(Scanner scanner, String userId) {
        System.out.print("Enter Carrier Id for ticket cancellation: ");
        String carrierId = scanner.nextLine();

        if (carriers.containsKey(carrierId)) {
            Carrier carrier = carriers.get(carrierId);
            System.out.print("Enter number of tickets to cancel: ");
            int numOfTickets = scanner.nextInt();
            scanner.nextLine