Exercise 2.20. Airline seating. Design and implement 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 180 seats in economy class (30 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. Follow the design
process that was described in this chapter.
Everything else I get, except the main problem, which is assigning seats. I cannot figure out a way to add for example, only 2 seats to the array.
If someone could help steer me in the right direction, that would be great. Thanks
airplane. Assume the airplane has 20 seats in first class (5 rows of 4 seats each, separated
by an aisle) and 180 seats in economy class (30 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. Follow the design
process that was described in this chapter.
Everything else I get, except the main problem, which is assigning seats. I cannot figure out a way to add for example, only 2 seats to the array.
If someone could help steer me in the right direction, that would be great. Thanks
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(""); } } }