RDX AMS2


 import java.util.ArrayList;
import java.util.Scanner;

// Main Application
public class AirlineManagementSystem {

    static Scanner scanner = new Scanner(System.in);
    static ArrayList<Flight> flights = new ArrayList<>();
    static ArrayList<Customer> customers = new ArrayList<>();
    static Admin admin = new Admin("admin", "admin123");

    public static void main(String[] args) {
        while (true) {
            System.out.println("Welcome to Airline Management System!");
            System.out.println("1. Admin Login");
            System.out.println("2. Customer Login");
            System.out.println("3. Exit");
            System.out.print("Select your role: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    adminLogin();
                    break;
                case 2:
                    customerMenu();
                    break;
                case 3:
                    System.out.println("Exiting system...");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }

    // Admin login
    private static void adminLogin() {
        System.out.print("Enter Admin ID: ");
        String id = scanner.next();
        System.out.print("Enter Password: ");
        String password = scanner.next();

        if (admin.authenticate(id, password)) {
            adminMenu();
        } else {
            System.out.println("Invalid Admin Credentials!");
        }
    }

    // Admin menu
    private static void adminMenu() {
        while (true) {
            System.out.println("\n--- Admin Menu ---");
            System.out.println("1. Add Flight");
            System.out.println("2. Edit Flight");
            System.out.println("3. Remove Flight");
            System.out.println("4. Exit Admin Menu");
            System.out.print("Enter choice: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    admin.addFlight(flights);
                    break;
                case 2:
                    admin.editFlight(flights);
                    break;
                case 3:
                    admin.removeFlight(flights);
                    break;
                case 4:
                    return;
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }

    // Customer menu
    private static void customerMenu() {
        System.out.println("\n--- Customer Registration ---");
        System.out.print("Enter Customer ID: ");
        String customerId = scanner.next();
        System.out.print("Enter Customer Name: ");
        String name = scanner.next();

        Customer customer = new Customer(customerId, name);
        customers.add(customer);
        
        while (true) {
            System.out.println("\n--- Customer Menu ---");
            System.out.println("1. View Flights");
            System.out.println("2. Book a Flight");
            System.out.println("3. Exit");
            System.out.print("Enter choice: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    customer.viewFlights(flights);
                    break;
                case 2:
                    customer.bookFlight(flights);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }
}

// Flight Class
class Flight {
    String flightId;
    String destination;
    int capacity;
    int bookedSeats = 0;

    Flight(String flightId, String destination, int capacity) {
        this.flightId = flightId;
        this.destination = destination;
        this.capacity = capacity;
    }

    boolean bookSeat() {
        if (bookedSeats < capacity) {
            bookedSeats++;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return "Flight ID: " + flightId + ", Destination: " + destination + ", Capacity: " + capacity + ", Booked: " + bookedSeats;
    }
}

// Admin Class
class Admin {
    private String adminId;
    private String password;

    Admin(String adminId, String password) {
        this.adminId = adminId;
        this.password = password;
    }

    boolean authenticate(String adminId, String password) {
        return this.adminId.equals(adminId) && this.password.equals(password);
    }

    void addFlight(ArrayList<Flight> flights) {
        System.out.println("--- Add Flight ---");
        System.out.print("Enter Flight ID: ");
        String flightId = AirlineManagementSystem.scanner.next();
        System.out.print("Enter Destination: ");
        String destination = AirlineManagementSystem.scanner.next();
        System.out.print("Enter Capacity: ");
        int capacity = AirlineManagementSystem.scanner.nextInt();

        Flight flight = new Flight(flightId, destination, capacity);
        flights.add(flight);
        System.out.println("Flight added successfully!");
    }

    void editFlight(ArrayList<Flight> flights) {
        System.out.println("--- Edit Flight ---");
        System.out.print("Enter Flight ID to edit: ");
        String flightId = AirlineManagementSystem.scanner.next();
        Flight flight = findFlight(flightId, flights);

        if (flight != null) {
            System.out.print("Enter new Destination: ");
            flight.destination = AirlineManagementSystem.scanner.next();
            System.out.print("Enter new Capacity: ");
            flight.capacity = AirlineManagementSystem.scanner.nextInt();
            System.out.println("Flight details updated!");
        } else {
            System.out.println("Flight not found!");
        }
    }

    void removeFlight(ArrayList<Flight> flights) {
        System.out.println("--- Remove Flight ---");
        System.out.print("Enter Flight ID to remove: ");
        String flightId = AirlineManagementSystem.scanner.next();
        Flight flight = findFlight(flightId, flights);

        if (flight != null) {
            flights.remove(flight);
            System.out.println("Flight removed successfully!");
        } else {
            System.out.println("Flight not found!");
        }
    }

    Flight findFlight(String flightId, ArrayList<Flight> flights) {
        for (Flight flight : flights) {
            if (flight.flightId.equals(flightId)) {
                return flight;
            }
        }
        return null;
    }
}

// Customer Class
class Customer {
    private String customerId;
    private String name;

    Customer(String customerId, String name) {
        this.customerId = customerId;
        this.name = name;
    }

    void viewFlights(ArrayList<Flight> flights) {
        System.out.println("\n--- Available Flights ---");
        for (Flight flight : flights) {
            System.out.println(flight);
        }
    }

    void bookFlight(ArrayList<Flight> flights) {
        System.out.print("Enter Flight ID to book: ");
        String flightId = AirlineManagementSystem.scanner.next();
        Flight flight = findFlight(flightId, flights);

        if (flight != null) {
            if (flight.bookSeat()) {
                System.out.println("Flight booked successfully!");
            } else {
                System.out.println("No seats available on this flight.");
            }
        } else {
            System.out.println("Flight not found!");
        }
    }

    Flight findFlight(String flightId, ArrayList<Flight> flights) {
        for (Flight flight : flights) {
            if (flight.flightId.equals(flightId)) {
                return flight;
            }
        }
        return null;
    }
}