Java language Business P12.6 Airline seating. Write a program that assigns seats
ID: 3714715 • Letter: J
Question
Java language
Business P12.6 Airline seating. Write a program that assigns seats on an airplane. Assume the airplane has 20 seats in first class (5 rows of 4 seats each, separated by an aisle) and 90 seats in economy class (15 rows of 6 seats each, separated by an aisle). Your program should take three commands: add passengers, show seating, and quit. When passengers are added, ask for the class (first or economy), the number of passengers traveling together (1 or 2 in first class; 1 to 3 in economy), and the seating preference (aisle or window in first class; aisle, center, or window in economy). Then try to find a match and assign the seats. If no match exists, print a message. Your solution should include a class Airplane that is not coupled with the Scanner or PrintStream classes. Follow the design process that was described in this chapter.
Explanation / Answer
Hi Dear, Please find implementation.
// this is Class to run
public class AirplaneSeatingSystem {
static AirplaneMenu menu = new AirplaneMenu();
public static void main(String[] args) {
while(true){
menu.stage1();
}//close while
}//close main
}
AirplaneMenu.java
import java.util.Scanner;
public class AirplaneMenu {
Scanner reader = new Scanner(System.in);
Airplane plane = new Airplane();
public void stage1(){
System.out.println("A)dd S)how Q)uit");
String input = reader.nextLine();
if(input.equalsIgnoreCase("a")){
stage2();
}else if(input.equalsIgnoreCase("s")){
plane.printSeats();
}else if(input.equalsIgnoreCase("q")){
System.exit(0);
}else{
System.out.println("Invalid Input");
}
}//close printMenu
public void stage2(){
while(true){
System.out.println("F)irst E)conomy");
String input = reader.nextLine();
if(input.equalsIgnoreCase("f")){
stage3(2);
break;
}else if(input.equalsIgnoreCase("e")){
stage3(3);
break;
}//close else
System.out.println("Invalid input, try again.");
}
}//close stage2
public void stage3(int cls){
while(true){
System.out.println("Passengers? (1-" + cls + ")");
int input = reader.nextInt();
reader.nextLine();//consume
if(input == cls){
plane.findSeats(input,cls,1);
break;
}else if(input > 0 && input < cls){
stage4(cls, input);
break;
}else{
System.out.println("Invalid input, try again");
}//close else
}//close while
}//close stage3
public void stage4(int cls, int pass){
while(true){
if(cls==2)
System.out.println("A)isle W)indow");
else
System.out.println("A)isle C)enter W)indow");
String input = reader.nextLine();
if(input.equalsIgnoreCase("a")){
if(cls == 2){
plane.findSeats(pass,cls,2);
break;
}
plane.findSeats(pass,cls,3);
break;
}else if(input.equalsIgnoreCase("c")){
plane.findSeats(pass,cls, 2);
break;
}else if(input.equalsIgnoreCase("w")){
plane.findSeats(pass,cls,1);
break;
}else{
System.out.println("Invalid input, try again");
}//close else
}//close while
}//close stage4
}
Airplane.java
import java.util.ArrayList;
public class Airplane {
private SeatRow[] rows = new SeatRow[20];
public Airplane(){
for(int i=0; i<5; i++)
rows[i] = new SeatRow("E");
for(int i=5; i<20; i++)
rows[i] = new SeatRow("F");
}
public void printSeats()
{
for(int i=0; i<20; i++){
int j = i+1;
System.out.print(j);
System.out.print(":");
System.out.println(rows[i]);
}
}
public void findSeats(int quantity, int cls, int preference)
{
boolean seatsTaken = false;
int firstRow, lastRow, seatCount;
if (cls==2){
firstRow = 0;
lastRow = 4;
}
else{
firstRow = 5;
lastRow = 19;
}
for(int i=firstRow; i<lastRow && !seatsTaken; i++){
if(!rows[i].seats[preference-1]){
boolean seatsAvailable = true;
for(int j = preference; j<preference + quantity; j++)
{
if(rows[i].seats[j-1])
seatsAvailable = false;
}
if(seatsAvailable){
for(int j = preference; j<preference + quantity; j++)
{
rows[i].seats[j-1] = true;
}
seatsTaken = true;
}
}
else if(!rows[i].seats[preference-1+cls]){
boolean seatsAvailable = true;
for(int j = preference + cls; j<preference + cls + quantity; j++)
{
if(rows[i].seats[j-1])
seatsAvailable = false;
}
if(seatsAvailable){
for(int j = preference +cls; j<preference + cls + quantity; j++)
{
rows[i].seats[j-1] = true;
}
seatsTaken = true;
}
}
}
}
}
SeatRow.java
public class SeatRow {
public boolean[] seats;
public SeatRow(String cls){
if(cls.equals("E"))
{
seats = new boolean[] {false, false, false, false};
}
else
{
seats = new boolean[] {false, false, false, false, false, false};
}
}
public String toString(){
String output = "";
if(seats.length == 4)
{
Character[] output2 = new Character[] {1,2,3,4};
for(int i=0; i<seats.length; i++)
{
if (seats[i])
output2[i] = '*';
else
output2[i] = '.';
}
output = output2[0] + " " + output2[1] + " " + output2[2] + " " + output2[3];
}
else if(seats.length == 6)
{
Character[] output2 = new Character[] {1,2,3,4,5,6};
for(int i=0; i<seats.length; i++)
{
if (seats[i])
output2[i] = '*';
else
output2[i] = '.';
}
output = output2[0].toString() + output2[1] + output2[2] + " " + output2[3] + output2[4] + output2[5];
}
return output;
}
public void fillSeats(int startIndex, int quantity)
{
for(int i=startIndex; i < startIndex + quantity; i++)
seats[i] = true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.