Answer:
Here is the simple JAVA program.
import java.util.Scanner; // to take input from user
public class AirlineReservationSystem { //airline reservation class
public static void main(String[] args) { //start of main() function body
Scanner input=new Scanner(System.in); //Scanner class instance
int first_class=0; //stores the first class seat numbers
int eco_class=5; //holds the economy class seat numbers
int quit; //used to quit the reservation system
System.out.println("Welcome to the AIRLINE RESERVATION SYSTEM!"); // displays welcome message
do{ //start of do while loop
//displays following messages for user to make choice between 2 sections
System.out.println("Please type 1 for First Class ");
System.out.println("Please type 2 for Economy ");
int choice=input.nextInt(); //takes the choice from user
switch(choice){ // takes choice of user from first class or economy section
case 1: //when user enters first class as choice
first_class++;
//increments count of first class by 1 every time user reserves a seat in first //class
if (first_class<=5){ // the seats in first class section should be between 1-5
//displays message of seat reserved in first class
System.out.println("Your seat is assigned in First Class.");
System.out.println("Your seat number is "+first_class);
//assigns seat number in first class }
else{
// displays following message when number of seats in first class exceed 5
System.out.println("No seat is available in First Class");
//asks user if he wants to placed in economy class when there is no seat //available in first class
System.out.println("Do you want to be placed at the Economy section?(1.yes\t2.no)");
int full=input.nextInt(); //reads yes or no (1 or 2) from user
if (full==1){ //if user entered 1
System.out.println("Enter 1 to return to main menu to reserve a seat in Economy class.");
} // asks user to press 1 to go to main menu
else{ //displays following message if user enters 2
System.out.println("Next flight leaves in 3 hours.");} }
break;
case 2: //when the user choose to reserve seat in economy class
eco_class++;
//increments count of first class by 1 every time user reserves a seat in //economy class
//the seats of economy class ranges from 6 to 10
if ((eco_class>5)&&(eco_class<=10)){
//display message about economy class seat reservation
System.out.println("Your seat is assigned in Economy Class.");
//assigns seat number in economy class
System.out.println("Your seat number is "+eco_class); }
else{
// displays following message when number of seats in economy class //exceed 10
System.out.println("No seat is available in Economy Class");
//asks user if he wants to placed in first class when there is no seat //available in economy class
System.out.println("Do you want to be placed at the First class section?(1.yes\t2.no)");
int full=input.nextInt(); //reads yes or no (1 or 2) from user
if (full==1){ //if user entered 1
System.out.println("Enter 1 to return to main menu to reserve a seat in business class.");
// asks user to press 1 to go to main menu }
else{ //displays following message if user enters 2
System.out.println("Next flight leaves in 3 hours."); }} }
System.out.println("1.main menu\t2.quit");
quit=input.nextInt();
//if user presses 2 it reads that value from user and store it in quit variable
}while (quit==1); //the loop stops when user chooses quit
System.out.println("Bye! Have a nice day!"); } }
//displays message when user quits the reservation system
Explanation:
The program is well explained in the comments mentioned with each line of the program. The program uses switch statement for the user to make choice between first and economy class. If the user selects first class, then his seat is reserved in first class and a seat number is generated to user. The range of first class seats is from 1 to 5. When this range is exceeded the person is asked if he wants to be placed in economy class. If the user presses one then the person is asked to go back to main menu and reserve seat in economy class. If user enters 2 then the system exits. Same flow goes with the economy class seat reservation. The do while loop keeps executing until the user quits the system. If the user presses 2 to quit, the system exits after displaying bye message.