Homework -3 (Airplane Seating Assignment) Write a program that can be used to as
ID: 3699800 • Letter: H
Question
Homework -3 (Airplane Seating Assignment) Write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with six seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business class, and rows 8 through 13 are economy class. Your program must prompt the user to enter the following information: a. Ticket type (first class, business class, or economy class) b. Desired seat Output the seating plan in the following form: Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10 Row 11 Row 12 Row 13Explanation / Answer
import java.util.*;
class Airplane
{
public static void display(boolean[][] seat)
{
System.out.println(" A B C D E F");
int i, j;
for( i = 1 ; i < seat.length ; i++ )
{
System.out.print("Row " + i + " ");
for( j = 1; j < seat[i].length ; j++ )
{
// if current seat is booked
if( seat[i][j] == true )
System.out.print("X ");
// if current seat is available
else
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// true means the seat is already alloted to someone
// false means the seat is available
boolean[][] seat = new boolean[14][7];
int type, row, column;
display(seat);
while( true )
{
while(true)
{
System.out.println("Enter the Ticket Type ...");
System.out.println("1. First Class");
System.out.println("2. Business Class");
System.out.println("3. Economy Class");
type = sc.nextInt();
// if entered ticket type is valid
if( type > 0 && type < 4 )
break;
System.out.println("Invalid Ticket Type");
}
System.out.println();
System.out.println("Enter the desired Seat ...");
// for first class
if( type == 1 )
{
System.out.print("Enter row ( 1 - 2 ) : ");
row = sc.nextInt();
}
// for business class
else if( type == 2 )
{
System.out.print("Enter row ( 3 - 7 ) : ");
row = sc.nextInt();
}
// for economy class
else
{
System.out.print("Enter row ( 8 - 13 ) : ");
row = sc.nextInt();
}
System.out.print("Enter column ( 1 - 6 ) : ");
column = sc.nextInt();
int i, j;
if( seat[row][column] == true )
System.out.println("Sorry! The seat is already booked to someone else.");
else
{
// book the current seat
seat[row][column] = true;
System.out.println("Seat Booked Successfully!");
}
System.out.print(" Continue [ y / n ] : ");
char ch = sc.next().charAt(0);
if( ch != 'y' && ch != 'Y' )
break;
}
System.out.println();
display(seat);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.