/**
* Exercise 7.14
* Airline reservation system - assign seats on each flight of the airline's only plane (capacity: 10 seats)
* 1. Please type 1 for First Class & Please type 2 for Economy
* 2. If the user types 1 - assign a seat in the first class section (1-5).
* 3. If the user types 2 - assign a seat in the economy section (6-10).
* 4. Display a boarding pass - person's seat number & first-class/economy section of the plane.
* 5. One-dimensional array of primitive type boolean to represent the seating chart of the plane.
* 6. Initialize all the elements of the array to false for all empty seats.
* 7. As each seat is assigned, set the corresponding elements of the array to true to indicate that the seat is no longer
* available.
* 8. Never assign a seat that has already been assigned.
* 9. When the economy section is full, ask the person if it is acceptable to be placed in the first-class section (& vice-versa).
* 10. If yes, make the appropriate seat assignment. If no, display the message "Next flight leaves in 3 hours".
*/
package com.chapter7.arrays;
import java.io.BufferedReader;
public class Exercise714 {
private boolean seatsBooked;
private boolean arrSeats[];
private int CAPACITY = 10;
private int availSeats = CAPACITY;
private Scanner input;
private int userChoice;
private int current;
// constructor
public Exercise714() {
this.arrSeats = new boolean[CAPACITY];
initSeats();
this.seatsBooked = true;
}
// Initialize all the elements of the array to false for all empty seats.
private void initSeats() {
for (int i = 0; i < arrSeats.length; i++) {
arrSeats[i] = false;
}
}
// Display the menu for user choice
public void displayMenu() {
do {
// if no more seats available in the flight, display the message
if (availSeats == 0) {
System.out.println();
System.out.println("No more available seats in this flight.");
System.out.println("Thank you for using our system");
break;
}
System.out.println("***** Airline Reservation System *****");
System.out.println(" Menu Options ");
System.out.println(" 1 - First Class ");
System.out.println(" 2 - Economy Class ");
System.out.println(" 3 - Exit ");
System.out.println("**************************************");
System.out.println("Please choose your option: ");
input = new Scanner(System.in);
// try to get the user choice
try {
userChoice = input.nextInt();
while (userChoice != 1 && userChoice != 2 && userChoice != 3) {
System.out.println("Please choose either '1', '2' or '3'");
displayMenu();
}
} catch (InputMismatchException ex) {
System.err.println("Not a valid number: " + userChoice);
}
seatsBooked = assignSeats(userChoice);
} while (seatsBooked);
}
// Assign the seats based from the user's choice for the class
public boolean assignSeats(int userChoice) {
switch (userChoice) {
// First class section
case 1:
for (current = 0; current < 5; current++) {
if (arrSeats[current] == false) {
arrSeats[current] = true;
printBoardingPass(current + 1);
availSeats--;
seatsBooked = true;
break;
}
}
// if first class section is full, prompt user to choose other class
if (current == 5) {
chooseOtherClass();
}
break;
// Economy class section
case 2:
for (current = 5; current < 10; current++) {
if (arrSeats[current] == false) {
arrSeats[current] = true;
printBoardingPass(current + 1);
availSeats--;
seatsBooked = true;
break;
}
}
// if economy class section is full, prompt user to choose other
// class
if (current == 10) {
chooseOtherClass();
}
break;
case 3:
System.out.println("Thank you for using our system. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid input. Please type again");
seatsBooked = true;
}
return seatsBooked;
}
// If the chosen class is full, ask the user whether want to choose other
// class section
private boolean chooseOtherClass() {
do {
System.out
.println("There are no more seats available for the chosen class.\n");
System.out
.println("Do you want to choose another class? Type 'Y' for Yes or 'N' for No");
String ans = input.next();
if (ans.equalsIgnoreCase("Y")) {
seatsBooked = true;
} else if (ans.equalsIgnoreCase("N")) {
System.out.println("Next flight leaves in 3 hours.");
System.out.println();
break;
} else {
System.out.println("\nInvalid input.");
}
} while (!seatsBooked);
return seatsBooked;
}
// Print the boarding pass with the seat number and class section
public void printBoardingPass(int seatNum) {
System.out.println("***** Boarding Pass *****");
System.out.println();
System.out.println("Seat number: " + (seatNum));
if (seatNum <= 5) {
System.out.println("You've been assigned to first class.");
} else {
System.out.println("You've been assigned to economy class.");
}
System.out.println("Thank you for using our system. Have a safe flight!");
System.out.println();
System.out.println("*************************");
System.out.println();
}
}
/** Output
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
***** Boarding Pass *****
Seat number: 1
You've been assigned to first class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
***** Boarding Pass *****
Seat number: 2
You've been assigned to first class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
2
***** Boarding Pass *****
Seat number: 6
You've been assigned to economy class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
2
***** Boarding Pass *****
Seat number: 7
You've been assigned to economy class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
***** Boarding Pass *****
Seat number: 3
You've been assigned to first class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
***** Boarding Pass *****
Seat number: 4
You've been assigned to first class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
2
***** Boarding Pass *****
Seat number: 8
You've been assigned to economy class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
***** Boarding Pass *****
Seat number: 5
You've been assigned to first class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
There are no more seats available for the chosen class.
Do you want to choose another class? Type 'Y' for Yes or 'N' for No
Y
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
2
***** Boarding Pass *****
Seat number: 9
You've been assigned to economy class.
Thank you for using our system. Have a safe flight!
*************************
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
1
There are no more seats available for the chosen class.
Do you want to choose another class? Type 'Y' for Yes or 'N' for No
N
Next flight leaves in 3 hours.
***** Airline Reservation System *****
Menu Options
1 - First Class
2 - Economy Class
3 - Exit
**************************************
Please choose your option:
2
***** Boarding Pass *****
Seat number: 10
You've been assigned to economy class.
Thank you for using our system. Have a safe flight!
*************************
No more available seats in this flight.
Thank you for using our system
**/
package com.chapter7.arrays;
public class Exercise714Test {
public static void main(String[] args) {
Exercise714 airlineSystem=new Exercise714();
airlineSystem.displayMenu();
}
}

