Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
2
Indexable
Never
import java.util.Scanner;

public class AirplaneSeating {
    private static final int NUM_ROWS = 13;
    private static final int SEATS_PER_ROW = 6;
    private static final char AVAILABLE_SEAT = '*';
    private static final char ASSIGNED_SEAT = 'X';

    private static char[][] seatingPlan = new char[NUM_ROWS][SEATS_PER_ROW];

    public static void main(String[] args) {
        initializeSeatingPlan();

        Scanner scanner = new Scanner(System.in);
        boolean quit = false;
        while (!quit) {
            System.out.println("Please select a ticket type:");
            System.out.println("1. First class");
            System.out.println("2. Business class");
            System.out.println("3. Economy class");
            System.out.println("4. Quit");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    assignSeat("First class", 1, 2);
                    break;
                case 2:
                    assignSeat("Business class", 3, 7);
                    break;
                case 3:
                    assignSeat("Economy class", 8, 13);
                    break;
                case 4:
                    quit = true;
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        }
    }

    private static void initializeSeatingPlan() {
        for (int i = 0; i < NUM_ROWS; i++) {
            for (int j = 0; j < SEATS_PER_ROW; j++) {
                seatingPlan[i][j] = AVAILABLE_SEAT;
            }
        }
    }

    private static void assignSeat(String ticketType, int startRow, int endRow) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("You have selected " + ticketType + ". Please enter the desired seat (e.g., 3A):");
        String seat = scanner.nextLine().toUpperCase();
        int row = Integer.parseInt(seat.substring(0, seat.length() - 1)) - 1;
        int col = seat.charAt(seat.length() - 1) - 'A';

        if (row >= startRow - 1 && row < endRow && col >= 0 && col < SEATS_PER_ROW) {
            if (seatingPlan[row][col] == AVAILABLE_SEAT) {
                seatingPlan[row][col] = ASSIGNED_SEAT;
                System.out.println("Seat " + seat + " has been assigned.");
                printSeatingPlan();
            } else {
                System.out.println("Seat " + seat + " is already occupied.");
            }
        } else {
            System.out.println("Invalid seat selection.");
        }
    }

    private static void printSeatingPlan() {
        System.out.println("  A B C D E F");
        for (int i = 0; i < NUM_ROWS; i++) {
            System.out.print((i + 1) + " ");
            for (int j = 0; j < SEATS_PER_ROW; j++) {
                System.out.print(seatingPlan[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
}