App Java
App Java
Scanner;
2 public class App {
3
4 // Global variables
5 private static int[][] planeSeats = null;
6 private static int[] pricePerRow = null;
7
8 public static void main(String[] args) {
9 System.out.println("Welcome to Flying Java!");
10 initialiseRows();
11 runMenu();
12 }
13
14 public static void initialiseRows() {
15 planeSeats = new int[4][]; // total rows - multidimensional array
16 planeSeats[0] = new int[18]; // row 1 - initialised at 0 all available
17 planeSeats[1] = new int[20]; // row 2 - initialised at 0 all available
18 planeSeats[2] = new int[20]; // row 2 - initialised at 0 all available
19 planeSeats[3] = new int[18]; // row 2 - initialised at 0 all available
20 pricePerRow = new int[4];
21 pricePerRow[0] = 50;
22 pricePerRow[1] = 80;
23 pricePerRow[2] = 80;
24 pricePerRow[3] = 50;
25 }
26
27 public static void runMenu() {
28 int option;
29 boolean cont = true;
30
Page 1 of 5
31 while (cont) {
32 option = getOption();
33 switch (option) {
34 case 0:
35 cont = false;
36 break;
37 case 1:
38 buyTicket();
39 break;
40 case 2:
41 showSeatingArea();
42 break;
43 default:
44 System.out.println("Option not available. Please select a valid opt
45 }
46 }
47 }
48
49 private static int getOption() {
50
51 Scanner input = new Scanner(System.in);
52 boolean valid = false;
53 int option = -1;
54 do {
55 System.out.println();
56 System.out.println("+---------------------------------------------+");
57 System.out.println("| MAIN MENU |");
58 System.out.println("+---------------------------------------------+");
59 System.out.println("| 1) Buy a plane ticket |");
60 System.out.println("| 2) Show seating area and available seats |");
Page 2 of 5
61 System.out.println("| 0) Quit |");
62 System.out.println("+---------------------------------------------+");
63 System.out.print("Please select an option: ");
64 try {
65 option = input.nextInt();
66 valid = true;
67 } catch (Exception e) {
68 System.out.println("This option not valid.");
69 }
70 } while (!valid);
71 return option;
72
73 }
74
75 private static void buyTicket() {
76 Scanner input = new Scanner(System.in);
77 System.out.print("Enter row number: ");
78 int row = input.nextInt() - 1;
79 System.out.print("Enter seat number: ");
80 int seat = input.nextInt() - 1;
81
82 // Check if the seat is available or not
83 if (planeSeats[row][seat] == 0) {
84 planeSeats[row][seat] = 1;
85 System.out.println("Purchase successful.");
86 showSeatingArea();
87 } else {
88 System.out.println("This seat is already taken.");
89 }
90
Page 3 of 5
91 }
92
93 private static void showSeatingArea() {
94
95 int rows = planeSeats.length;
96 char aisle = '|';
97
98 System.out.println("=".repeat(76));
99 System.out.println(" PLANE SEATING MAP ");
100 System.out.println("=".repeat(76));
101
102 for (int row = 0; row < rows; row++) {
103 System.out.print("Row " + (row+1) + "(£" + pricePerRow[row]+ ") ");
104 int seatsPerRow = planeSeats[row].length;
105 for (int seat = 0; seat < seatsPerRow; seat++) {
106 if (seat == 9) { // Create aisles
107 System.out.print(" " + aisle + " ");
108 }
109 if (planeSeats[row][seat] == 0) { //available
110 System.out.print("[O]");
111 } else { // not available
112 System.out.print("[X]");
113 }
114 }
115 System.out.println();
116 }
117 System.out.println("=".repeat(76));
118 System.out.println("LEGEND: [O] = Seat available, [X] = Seat not available, |
119 System.out.println("=".repeat(76));
120 System.out.println();
Page 4 of 5
121
122 }
123 }
124
Page 5 of 5