OneCompiler

Java

1988

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
class CarrierDoesNotExistException extends Exception {
    public CarrierDoesNotExistException(String message) {
        super(message);
    }
}
class SeatingCapacityDiscrepancyException extends Exception {
    public SeatingCapacityDiscrepancyException(String message) {
        super(message);
    }
}
class Flight {
    private int flightID;
    private int carrierID;
    private String origin;
    private String destination;
    private int airfare;
    private int businessCapacity;
    private int economyCapacity;
    private int executiveCapacity;
    public Flight(int flightID, int carrierID, String origin, String destination, int airfare, int businessCapacity, int economyCapacity, int executiveCapacity) {
        this.flightID = flightID;
        this.carrierID = carrierID;
        this.origin = origin;
        this.destination = destination;
        this.airfare = airfare;
        this.businessCapacity = businessCapacity;
        this.economyCapacity = economyCapacity;
        this.executiveCapacity = executiveCapacity;
    }
    public int getFlightID() { return flightID; }
    public int getCarrierID() { return carrierID; }
    public String getOrigin() { return origin; }
    public String getDestination() { return destination; }
    public int getAirfare() { return airfare; }
    public int getBusinessCapacity() { return businessCapacity; }
    public int getEconomyCapacity() { return economyCapacity; }
    public int getExecutiveCapacity() { return executiveCapacity; }
}
class User {
    private int userID;
    private String name;

    public User(int userID, String name) {
        this.userID = userID;
        this.name = name;
    }
    public int getUserID() { return userID; }
    public String getName() { return name; }

    public void setName(String name) { this.name = name; }
}
public class AMSApplication {
    private static ArrayList<Flight> flightList = new ArrayList<>();
    private static ArrayList<User> userList = new ArrayList<>();
    private static AtomicInteger flightIDGenerator = new AtomicInteger(1);
    private static AtomicInteger userIDGenerator = new AtomicInteger(1);
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int choice;
        do {
            System.out.println("AMS Console - Choose an option:");
            System.out.println("1. Admin");
            System.out.println("2. Customer");
            System.out.println("3. Exit");
            choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    adminMenu();
                    break;
                case 2:
                    customerMenu();
                    break;
                case 3:
                    System.out.println("Exiting...");
                    break;
                default:
                    System.out.println("Invalid choice, please try again.");
            }
        } while (choice != 3);
    }
    private static void adminMenu() {
        int choice;
        do {
            System.out.println("Admin Section - Choose an option:");
            System.out.println("1. Add a Flight");
            System.out.println("2. Retrieve All Flights");
            System.out.println("3. View Cities and Flight Counts");
            System.out.println("4. Manage Users (CRUD)");
            System.out.println("5. Go back to Main Menu");
            choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addFlight();
                    break;
                case 2:
                    displayFlights();
                    break;
                case 3:
                    viewCityFlightCounts();
                    break;
                case 4:
                    manageUsers();
                    break;
                case 5:
                    System.out.println("Returning to Main Menu...");
                    break;
                default:
                    System.out.println("Invalid choice, please try again.");
            }
        } while (choice != 5);
    }
    private static void addFlight() {
        try {
            System.out.println("Enter Carrier ID: ");
            int carrierID = scanner.nextInt();
            System.out.println("Enter Origin (City Name): ");
            scanner.nextLine(); 
            String origin = scanner.nextLine();
            System.out.println("Enter Destination (City Name): ");
            String destination = scanner.nextLine();
            System.out.println("Enter Airfare: ");
            int airfare = scanner.nextInt();
            System.out.println("Enter Seat Capacity for Business Class: ");
            int businessCapacity = scanner.nextInt();
            System.out.println("Enter Seat Capacity for Economy Class: ");
            int economyCapacity = scanner.nextInt();
            System.out.println("Enter Seat Capacity for Executive Class: ");
            int executiveCapacity = scanner.nextInt();
            if (businessCapacity + economyCapacity + executiveCapacity < 50) {
                throw new SeatingCapacityDiscrepancyException("Seating capacity cannot be less than the minimum required.");
            }
            Flight flight = new Flight(
                    flightIDGenerator.getAndIncrement(),
                    carrierID,
                    origin,
                    destination,
                    airfare,
                    businessCapacity,
                    economyCapacity,
                    executiveCapacity
            );
            flightList.add(flight);
            System.out.println("Flight Information Saved Successfully in the System");
        } catch (InputMismatchException e) {
            System.out.println("Encountered issue while saving Flight Information. Please check the data and try again.");
            scanner.next();
        } catch (SeatingCapacityDiscrepancyException e) {
            System.out.println(e.getMessage());
        }
    }
    private static void displayFlights() {
        if (flightList.isEmpty()) {
            System.out.println("No flights available.");
            return;
        }
        System.out.printf("%-10s %-10s %-15s %-15s %-10s %-10s %-10s %-10s\n",
                "FlightID", "CarrierID", "Origin", "Destination", "Airfare", "Business", "Economy", "Executive");
        for (Flight flight : flightList) {
            System.out.printf("%-10d %-10d %-15s %-15s %-10d %-10d %-10d %-10d\n",
                    flight.getFlightID(),
                    flight.getCarrierID(),
                    flight.getOrigin(),
                    flight.getDestination(),
                    flight.getAirfare(),
                    flight.getBusinessCapacity(),
                    flight.getEconomyCapacity(),
                    flight.getExecutiveCapacity());
        }
    }
    private static void viewCityFlightCounts() {
        Map<String, Integer> cityFlightCounts = new TreeMap<>();
        for (Flight flight : flightList) {
            cityFlightCounts.put(flight.getOrigin(), cityFlightCounts.getOrDefault(flight.getOrigin(), 0) + 1);
            cityFlightCounts.put(flight.getDestination(), cityFlightCounts.getOrDefault(flight.getDestination(), 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : cityFlightCounts.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue() + " flights");
        }
    }
    private static void manageUsers() {
        int choice;
        do {
            System.out.println("User Management - Choose an option:");
            System.out.println("1. Add User");
            System.out.println("2. Update User");
            System.out.println("3. Delete User");
            System.out.println("4. View All Users");
            System.out.println("5. Go back to Admin Menu");
            choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addUser();
                    break;
                case 2:
                    updateUser();
                    break;
                case 3:
                    deleteUser();
                    break;
                case 4:
                    viewAllUsers();
                    break;
                case 5:
                    System.out.println("Returning to Admin Menu...");
                    break;
                default:
                    System.out.println("Invalid choice, please try again.");
            }
        } while (choice != 5);
    }
    private static void addUser() {
        System.out.println("Enter User Name: ");
        scanner.nextLine();
        String name = scanner.nextLine();
        User user = new User(userIDGenerator.getAndIncrement(), name);
        userList.add(user);
        System.out.println("User added successfully.");
    }
    private static void updateUser() {
        System.out.println("Enter User ID to update: ");
        int userID = scanner.nextInt();
        Optional<User> userOptional = userList.stream().filter(u -> u.getUserID() == userID).findFirst();
        if (userOptional.isPresent()) {
            System.out.println("Enter new User Name: ");
            scanner.nextLine();
            String newName = scanner.nextLine();
            userOptional.get().setName(newName);
            System.out.println("User updated successfully.");
        } else {
            System.out.println("User not found.");
        }
    }
    private static void deleteUser() {
        System.out.println("Enter User ID to delete: ");
        int userID = scanner.nextInt();
        userList.removeIf(user -> user.getUserID() == userID);
        System.out.println("User deleted successfully.");
    }
    private static void viewAllUsers() {
        if (userList.isEmpty()) {
            System.out.println("No users available.");
            return;
        }
        userList.forEach(user -> {
            System.out.println("UserID: " + user.getUserID() + ", Name: " + user.getName());
        });
    }
    private static void customerMenu() {
        System.out.println("Enter Origin (City Name): ");
        scanner.nextLine();
        String origin = scanner.nextLine();
        System.out.println("Enter Destination (City Name): ");
        String destination = scanner.nextLine();
        Flight cheapestFlight = flightList.stream()
                .filter(flight -> flight.getOrigin().equalsIgnoreCase(origin) && flight.getDestination().equalsIgnoreCase(destination))
                .min(Comparator.comparingInt(Flight::getAirfare))
                .orElse(null);
        if (cheapestFlight != null) {
            System.out.println("Cheapest Flight Found:");
            System.out.printf("%-10s %-10s %-15s %-15s %-10s %-10s %-10s %-10s\n",
                    "FlightID", "CarrierID", "Origin", "Destination", "Airfare", "Business", "Economy", "Executive");
            System.out.printf("%-10d %-10d %-15s %-15s %-10d %-10d %-10d %-10d\n",
                    cheapestFlight.getFlightID(),
                    cheapestFlight.getCarrierID(),
                    cheapestFlight.getOrigin(),
                    cheapestFlight.getDestination(),
                    cheapestFlight.getAirfare(),
                    cheapestFlight.getBusinessCapacity(),
                    cheapestFlight.getEconomyCapacity(),
                    cheapestFlight.getExecutiveCapacity());
        } else {
            System.out.println("No flights found for the selected route.");
        }
    }
}