0% found this document useful (0 votes)
9 views5 pages

Java Plane Ticket Booking System

The document is a Java program for a ticket booking system called 'Flying Java'. It initializes a seating arrangement for a plane with different row prices and provides a menu for users to buy tickets or view seating availability. The program includes methods for handling user input, checking seat availability, and displaying the seating map.

Uploaded by

saddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Java Plane Ticket Booking System

The document is a Java program for a ticket booking system called 'Flying Java'. It initializes a seating arrangement for a plane with different row prices and provides a menu for users to buy tickets or view seating availability. The program includes methods for handling user input, checking seat availability, and displaying the seating map.

Uploaded by

saddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 import [Link].

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 [Link]("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 [Link]("Option not available. Please select a valid opt
45 }
46 }
47 }
48
49 private static int getOption() {
50
51 Scanner input = new Scanner([Link]);
52 boolean valid = false;
53 int option = -1;
54 do {
55 [Link]();
56 [Link]("+---------------------------------------------+");
57 [Link]("| MAIN MENU |");
58 [Link]("+---------------------------------------------+");
59 [Link]("| 1) Buy a plane ticket |");
60 [Link]("| 2) Show seating area and available seats |");
Page 2 of 5
61 [Link]("| 0) Quit |");
62 [Link]("+---------------------------------------------+");
63 [Link]("Please select an option: ");
64 try {
65 option = [Link]();
66 valid = true;
67 } catch (Exception e) {
68 [Link]("This option not valid.");
69 }
70 } while (!valid);
71 return option;
72
73 }
74
75 private static void buyTicket() {
76 Scanner input = new Scanner([Link]);
77 [Link]("Enter row number: ");
78 int row = [Link]() - 1;
79 [Link]("Enter seat number: ");
80 int seat = [Link]() - 1;
81
82 // Check if the seat is available or not
83 if (planeSeats[row][seat] == 0) {
84 planeSeats[row][seat] = 1;
85 [Link]("Purchase successful.");
86 showSeatingArea();
87 } else {
88 [Link]("This seat is already taken.");
89 }
90
Page 3 of 5
91 }
92
93 private static void showSeatingArea() {
94
95 int rows = [Link];
96 char aisle = '|';
97
98 [Link]("=".repeat(76));
99 [Link](" PLANE SEATING MAP ");
100 [Link]("=".repeat(76));
101
102 for (int row = 0; row < rows; row++) {
103 [Link]("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 [Link](" " + aisle + " ");
108 }
109 if (planeSeats[row][seat] == 0) { //available
110 [Link]("[O]");
111 } else { // not available
112 [Link]("[X]");
113 }
114 }
115 [Link]();
116 }
117 [Link]("=".repeat(76));
118 [Link]("LEGEND: [O] = Seat available, [X] = Seat not available, |
119 [Link]("=".repeat(76));
120 [Link]();
Page 4 of 5
121
122 }
123 }
124

Page 5 of 5

Common questions

Powered by AI

The program manages the display of available and occupied seats using a symbolic representation where '[O]' denotes available seats and '[X]' indicates occupied seats. This visual mapping is executed within a loop that iterates over each seat in the 'planeSeats' array. It's important for user interaction as it provides a clear and immediate view of which seats can be selected, enhancing the user's decision-making process and satisfaction by allowing easy navigation and quicker purchase decisions .

The program initializes the seating arrangement by creating an array of arrays, where each inner array represents a row with all seats initially set to zero, indicating they are available. While this approach is efficient in setting up the initial state with a flat array representation and consistent seat pricing band associated with each row, it lacks dynamic allocation if the seat configuration needs to be altered after initialization. Any changes would require re-initializing the entire seating configuration, which is not dynamically adaptable without program restarts .

Using Scanner for input handling allows the program to collect user input interactively and handle user-driven actions, providing a simple interface to receive integers and detect invalid formats. However, it bears potential downsides, such as not gracefully handling end-of-file or stream exceptions and requires additional code to correctly manage input cleaning and conversion from strings. Furthermore, Scanner can introduce blocking I/O operations, which may slow down interactions if input is not well-managed, thus affecting user experience in a real-time application setting .

Taking user input for seat reservations poses security implications such as potential injection attacks, incorrect data handling, and invalid input leading to application logic errors. These risks can be mitigated by implementing robust input validation to ensure inputs fall within expected numeric ranges, sanitizing inputs against injection threats, and applying exception handling to gracefully manage invalid submissions. Additionally, using input filtering and encoding practices before data processing or storage can further protect against malicious activity and enhance the system's resilience to external manipulation .

Using a multidimensional array to represent plane seating allows for a structured and straightforward representation of seats across multiple rows, where each row can have a different number of seats. This flexibility is beneficial for modeling real-world seating plans. However, maintaining this structure may become complex if the seating configuration changes often or if additional attributes are required for seats, as it would necessitate changes across the entire array structure and potentially the logic that manipulates it .

The method used to show the seating area involves iteratively printing each seat's status using a symbolic representation to distinguish between available and occupied seats. This provides immediate visual feedback upon user interaction, which is enhanced by clear labeling. On purchase attempts, the feedback mechanism informs users of the success or failure of their transaction, which is essential for user transparency and experience. However, this could be improved with more detailed explanations or options to address errors, such as suggesting alternative seat choices if the selected one is unavailable. This comprehensive feedback enhances usability and user satisfaction .

Improvements to the menu system could include adding more detailed error handling to guide users when incorrect input is entered, implementing a more dynamic and easily extendable menu structure to simplify adding new options, and incorporating input verification directly within menu options to minimize errors. Additionally, enhancing the user interface with graphical elements or using a library to improve the text-based menu's aesthetics and accessibility could vastly improve user experience and make the application more intuitive and engaging .

The program uses different pricing for different rows to reflect potentially varying seat value or demand, such as better views or more legroom in certain rows. This is implemented through the 'pricePerRow' array, where each index corresponds to a specific row, and the values represent the ticket price for each row. During seating map display, the program prints the price for each row by accessing this array. This separation allows for easy price customization per row without changing the core seat allocation logic .

The 'runMenu()' method serves as the core interaction loop for the program, continuously displaying menu options to the user and handling corresponding actions based on user input until the program is terminated. Its design is critical because it controls user experience by synchronizing user input with program functionality, ensuring seamless operation and a logical flow of command choices without interruption. It effectively manages the transition between different states within the application, such as buying tickets or displaying the seating map, thereby facilitating efficient program execution .

The Java program checks the availability of a seat by examining the value in the 'planeSeats' array at the specified row and seat indices. It uses the condition 'if (planeSeats[row][seat] == 0)' to determine whether the seat is unoccupied (available). If the seat is available, indicated by the value 0, the program updates the array to 1 (indicating that the seat is now occupied) and confirms the purchase as successful .

You might also like