Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PlaneSeating {
  4.     private final Scanner input = new Scanner(System.in);
  5.     private final char[][] rows = {{'A', 'B', 'C'}, {'A', 'B', 'C'}, {'A', 'B', 'C'}};
  6.  
  7.     public void run() {
  8.         while (true) {
  9.             System.out.print("\nMenu\n1. Select Seat\n2. Print Seating Chart\n3. Exit\n");
  10.             System.out.print("Choice: ");
  11.  
  12.             switch (readInput(1, 3)) {
  13.                 case 1:
  14.                     selectSeat();
  15.                     break;
  16.                 case 2:
  17.                     printSeating();
  18.                     break;
  19.                 case 3:
  20.                     return;
  21.             }
  22.         }
  23.     }
  24.  
  25.     private int readInput(int from, int to) {
  26.         while (true) {
  27.             String choice = input.nextLine();
  28.  
  29.             if(choice.matches("[A-Za-z]")) {
  30.                 int i = Character.toLowerCase(choice.charAt(0)) - 'a';
  31.                 if (i >= from && i <= to) {
  32.                     return i;
  33.                 }
  34.             } else if (choice.matches("[0-9]+")) {
  35.                 int i = Integer.parseInt(choice);
  36.                 if (i >= from && i <= to) {
  37.                     return i;
  38.                 }
  39.             }
  40.  
  41.             System.out.print("Sorry, invalid option, try again\n");
  42.         }
  43.     }
  44.  
  45.     public void selectSeat() {
  46.         System.out.print("Select Row(1, 2, 3): ");
  47.         int row = readInput(1, rows.length) - 1;
  48.         System.out.print("Select Seat(A, B, C): ");
  49.         int seat = readInput(0, 2);
  50.  
  51.         rows[row][seat] = 'X';
  52.     }
  53.  
  54.     public void printSeating() {
  55.         for (int r = 0; r < rows.length; r++) {
  56.             for (int s = 0; s < rows[r].length; s++) {
  57.                 System.out.print(rows[r][s] + " ");
  58.             }
  59.             System.out.println();
  60.         }
  61.     }
  62.  
  63.     public static void main(String[] args) {
  64.         new PlaneSeating().run();
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement