In: Computer Science
(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You’ve been asked to develop the new system. You’re to write an application to assign seats on each flight of the airline’s only plane (capacity: 500 seats).Your application should display the following alternatives: Please type 1 for First Class and Please type 2 for Economy. If the user types 1, your application should assign a seat in the first class section (seats 1–250). If the user types 2, your application should assign a seat in the economy section (seats 250–500). Your application should then display a boarding pass indicating the person’s seat number and whether it’s in the first-class or economy section of the plane. Use a one-dimensional array of primitive type boolean to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all the seats are empty. As each seat is assigned, set the corresponding element of the array to true to indicate that the seat is no longer available. Your application should never assign a seat that has already been assigned. When the economy section is full, your application should ask the person if it’s acceptable to be placed in the first-class section (and vice versa). If yes, make the appropriate seat assignment. If no, display the message "Next flight leaves in 3 hours."
For Java
Java Program:
import java.util.*;
class AirplaneReservations
{
public static void main(String args[])
{
//Scanner object
Scanner reader = new
Scanner(System.in);
String loop = "yes";
boolean[] status = new
boolean[500];
int ch;
String option;
while(loop.equalsIgnoreCase("yes")
== true)
{
//Checking
whether seats are available or not
if(isEconomyClassFull(status) &&
isFirstClassFull(status))
{
System.out.println("\n All the seats reserved...
\n Next flight leaves in 3 hours. \n");
break;
}
//Prompting
user
System.out.print("\n\n Press type 1 for First Class or 2 for
Economy: ");
ch =
reader.nextInt();
//Checking type
of class
if(ch ==
1)
{
//Checking for full
if(!isFirstClassFull(status))
{
//Checking for empty
seats
for(int i=0; i<250;
i++)
{
if(status[i] == false)
{
status[i] = true;
System.out.println("\n ***** Boarding Pass *****
\n Seat Number: " + (i+1) + "\n Class: First Class\n");
break;
}
}
}
else
{
//Checking in Economy
if(!isEconomyClassFull(status))
{
reader.nextLine();
System.out.print("\n\n First Class is Full... Do you want to
reserve in Economy Class? (yes or no) ");
option =
reader.nextLine();
//Checking
option
if(option.equalsIgnoreCase("yes") == true)
{
//Checking for empty seats
for(int i=250; i<500; i++)
{
if(status[i] == false)
{
status[i]
= true;
System.out.println("\n ***** Boarding Pass ***** \n Seat Number: "
+ (i+1) + "\n Class: Economy Class\n");
break;
}
}
}
else
{
System.out.println("\n Next flight leaves in 3
hours. \n");
}
}
}
}
else if(ch ==
2)
{
//Checking for full
if(!isEconomyClassFull(status))
{
//Checking for empty
seats
for(int i=250; i<500;
i++)
{
if(status[i] == false)
{
status[i] = true;
System.out.println("\n ***** Boarding Pass *****
\n Seat Number: " + (i+1) + "\n Class: Economy Class\n");
break;
}
}
}
else
{
//Checking in First
Class
if(!isFirstClassFull(status))
{
reader.nextLine();
System.out.print("\n\n Economy Class is Full... Do you want to
reserve in First Class? (yes or no) ");
option =
reader.nextLine();
//Checking
option
if(option.equalsIgnoreCase("yes") == true)
{
//Checking for empty seats
for(int i=0; i<250; i++)
{
if(status[i] == false)
{
status[i]
= true;
System.out.println("\n ***** Boarding Pass ***** \n Seat Number: "
+ (i+1) + "\n Class: First Class\n");
break;
}
}
}
else
{
System.out.println("\n Next flight leaves in 3
hours. \n");
}
}
}
}
else
{
System.out.println("\n Invalid option....
\n");
}
reader.nextLine();
System.out.print("\n\n Do you want to reserve another ticket? (yes
or no): ");
loop =
reader.nextLine();
}
}
//Method that checks whether First Class is full
public static boolean isFirstClassFull(boolean[]
status)
{
//Iterating over array
for(int i=0; i<250; i++)
{
//Checking
status
if(status[i] ==
false)
return false;
}
return true;
}
//Method that checks whether Economy Class is
full
public static boolean isEconomyClassFull(boolean[]
status)
{
//Iterating over array
for(int i=250; i<500; i++)
{
//Checking
status
if(status[i] ==
false)
return false;
}
return true;
}
}
_________________________________________________________________________________________
Sample Run: