Structures Task : Create a seating reservation program for Podunk Airlines. The
ID: 3824352 • Letter: S
Question
Structures
Task: Create a seating reservation program for Podunk Airlines. The air fleet consists of a single plane with a seating capacity of 12. The plane makes one flight daily. Your program will maintain the reservation information for a single day.
Processing: Use a 12-element array of structures to store information about the 12-seat plane. The structures will include this information:
seat identification number (an integer from 1 to 12)
a Boolean variable that indicates whether or not the seat is currently assigned
the first name of the seat holder
the last name of the seat holder
The program should be user-friendly, providing a menu of these functions:
show the count of empty seats
show a list of all seats in numeric order, including assigned passenger names
show an alphabetical list of passengers and their seat numbers; don’t include empty seats
assign a passenger to a seat
cancel a seat reservation
quit
After the completion of a menu task other than f, the program should clear the screen and redisplay the menu for the user. When searching for a name or when sorting names, note that passengers may have the same last name; however, we will assume for our program that two passengers don’t have identical first and last names.
Input: Choice d in the menu will require the user to input the name of a passenger and a desired seat number. Choice e should permit the user to provide a passenger’s name or a seat number.
Output: All output may be directed to the screen.
Explanation / Answer
import java.util.Scanner;
public class Airline
{
boolean[] seating = new boolean[11]; /* create 10 seat numbers (array[0] will not be used). Empty seat indicated by false*/
Scanner input = new Scanner(System.in);
public void start()
{
while ( true )
{
makeReservation();
}
}
public void makeReservation()
{
System.out.println("Please type 1 for First Class or 2 for Economy: ");
int section = input.nextInt();
if ( section == 1 )
{
firstClassSeat();
}
else
{
economySeat();
}
}
public void firstClassSeat() // assign a first class seat
{
for ( int count = 1; count <= 5; count++ )
{
if ( seating[count] == false )
{
seating[count] = true;
System.out.printf("First Class. Seat# %d ", count);
break;
}
else if ( seating[5] == true )
{
if ( seating[10] == true)
{
System.out.println("Sorry, flight fully booked. Next flight is in 3 hours.");
}
else // ask passenger if they would like an economy ticket instead
{
System.out.println("First Class is fully booked. Would you like Economy? 1 for Yes 2 for No");
int choice = input.nextInt();
if ( choice == 1 )
{
economySeat();
start();
}
else
{
System.out.println("Next flight is in 3 hours.");
System.exit(0);
}
}
}
}
}
public void economySeat() // assign an economy seat
{
for ( int count = 6; count <= 10; count++ )
{
if ( seating[count] == false )
{
seating[count] = true;
System.out.printf("Economy. Seat# %d ", count);
break;
}
else if ( seating[10] == true )
{
if ( seating[5] == true)
{
System.out.println("Sorry, flight fully booked. Next flight is in 3 hours.");
System.exit(0);
}
else
{
System.out.println("Economy is fully booked. Would you like First Class? 1 for Yes 2 for No");
int choice = input.nextInt();
if ( choice == 1 )
{
firstClassSeat();
start();
}
else
{
System.out.println("Next flight is in 3 hours");
System.exit(0);
}
}
}
}
}
}