C++ Airplane Seating Code Design and implement a program that assigns seats on a
ID: 3672436 • Letter: C
Question
C++ Airplane Seating Code
Design and implement a program that assigns seats on an airplane. Assume the airplane has 20 seats in the first class (5 rows of 4 seats each, separated by an isle) and 180 seats in the economy class (30 rows of 6 seats each, separated by an isle). Your program should take three commands: add passengers, show seating, and quit. When passengers are added, ask for the class (first or the economy), the number of passengers traveling together (1 to 2 in the first class, 1 to 3 in the economy), and the seating preference (aisle or a window in the first class; aisle, center or window in economy). Then try to find a match and assign the seats. If no match exits, print a message. Follow the object printed design process.
Explanation / Answer
import java.util.Scanner;
public class Menu {
/**
* @param args
*/
public static void main(String[] args) {
int choice = 0;
Scanner kb = new Scanner(System.in);
//Create a new airplane - this probably has to go inside the while loop
First_Class airplane = new First_Class();
Economy_Class airplane1 = new Economy_Class();
while(choice != 3){
System.out.println("1: Add passengers");
System.out.println("2: Show seating");
System.out.println("3: Quit");
choice = kb.nextInt(); //Selection choice
//Add Passengers
if(choice == 1){
System.out.println("1:First or 2:Economy class?"); //Asks for class
int seatingClass = kb.nextInt(); //Stores class choice
System.out.println("How many passengers?");//Asks for # of passengers
int numPassengers = kb.nextInt();//Stores # of passengers
//If first class and 3 or more passengers, deny access.
if(seatingClass == 1){
if(numPassengers <= 2){
System.out.println("1:Aisle or 2:Window");//Seating preference
int preference = kb.nextInt();
if(preference == 1){
airplane.addAisle(numPassengers); //Add to aisle
}
else{
airplane.addWindow(numPassengers); //Add to window
}
}
else{
System.out.println("You want to add " + numPassengers + " passengers."); //2 or less
System.out.println("Sorry, but you can only add up to 2 passengers at a time into First class, please try again.");
}
}
if(seatingClass == 2){
if(numPassengers <= 3){
System.out.println("1:Aisle 2:Center 3:Window");
int preference = kb.nextInt();//Seating preference
if(preference == 1){//Aisle
System.out.println("1:Aisle");
}
if(preference == 2){//Center
System.out.println("2:Center");
}
if(preference == 3){//Window
System.out.println("3:Window");
}
}
else{
System.out.println("You want to add " + numPassengers + " passengers."); //3 or less
System.out.println("Sorry, but you can only add up to 3 passengers at a time into Economy class, please try again.");
}
}
}
if(choice == 2){
System.out.println("First Class");
airplane.display();
System.out.println("Economy");
airplane1.display();
}
}
}
}
*******************************
public class First_Class {
int ROW = 5; //Five rows
int COLUMN = 4; //Five columns
private int[][] array = new int[ROW][COLUMN];
int numPassnegers;
public First_Class(){
}
public void addAisle(int numPassengers){
this.numPassnegers = numPassengers;
for(int i =0; i<ROW; i++){
if(array[i][1] == 0){
array[i][1] = 1;
}
}
}
public void addWindow(int numPassengers){
}
public void display(){
for(int i=0; i< ROW; i++){
for(int j=0; j< COLUMN; j++){
System.out.print(array[i][j] + " ");
}
System.out.println("");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.