Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PlaneSeating {
- private final Scanner input = new Scanner(System.in);
- private final char[][] rows = {{'A', 'B', 'C'}, {'A', 'B', 'C'}, {'A', 'B', 'C'}};
- public void run() {
- while (true) {
- System.out.print("\nMenu\n1. Select Seat\n2. Print Seating Chart\n3. Exit\n");
- System.out.print("Choice: ");
- switch (readInput(1, 3)) {
- case 1:
- selectSeat();
- break;
- case 2:
- printSeating();
- break;
- case 3:
- return;
- }
- }
- }
- private int readInput(int from, int to) {
- while (true) {
- String choice = input.nextLine();
- if(choice.matches("[A-Za-z]")) {
- int i = Character.toLowerCase(choice.charAt(0)) - 'a';
- if (i >= from && i <= to) {
- return i;
- }
- } else if (choice.matches("[0-9]+")) {
- int i = Integer.parseInt(choice);
- if (i >= from && i <= to) {
- return i;
- }
- }
- System.out.print("Sorry, invalid option, try again\n");
- }
- }
- public void selectSeat() {
- System.out.print("Select Row(1, 2, 3): ");
- int row = readInput(1, rows.length) - 1;
- System.out.print("Select Seat(A, B, C): ");
- int seat = readInput(0, 2);
- rows[row][seat] = 'X';
- }
- public void printSeating() {
- for (int r = 0; r < rows.length; r++) {
- for (int s = 0; s < rows[r].length; s++) {
- System.out.print(rows[r][s] + " ");
- }
- System.out.println();
- }
- }
- public static void main(String[] args) {
- new PlaneSeating().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement