Question
Need help with this, please!
Write a java program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with 6 seats in each row. Rows 1 and 2 are first class, rows 3 to 7 are business class, and rows 8 to 13 are economy class. Your program prompts the user to enter the following information:
- Ticket type (first class or economy class)
- For economy class, the smoking or non-smoking section
- Desired seat
Output the seating plan in the following format:
A B C D E F
Row 1 * * X * X X
Row 2 * X * X * X
Row 3 * * X X * X
Row 4 X * X * X X
Row 5 * X * X * *
Row 6 * X * * * X
Row 7 X * * * X X
Row 8 * X * X X *
Row 9 * * X * X X
Row 10 * X * X X X
Row 11 * * X * X *
Row 12 * * X X * X
Row 13 * * * * X *
Note: * indicates that the seat is available; x indicates that the seat has been assigned. Make this a menu driven program; show the user's choices and allow the user to make the appropriate choices.
PS: if there is already an existing problem(exactly like this) which has been answered, please do redirect me. :) I'm aware this is a textbook problem, but we don't have a copy of the textbook.
Thank you so much for any help anyone would extend :)
Explanation / Answer
Dear, //import required files import java.io.*; import java.util.*; //AirPlane Seat class declaration public class AirPlaneSeat { public static void main(String[] args) throws IOException { //creating scanner object Scanner reader = new Scanner(System.in); int choice; //character array to hold airplane seat arrangement char [][] seats = new char[13][6]; String seatNo = ""; int row = 0; int column = 0; char ch; //nested for loops to initialize seats for (int i = 0; i < 13; i++) for (int j = 0; j < 6; j++) seats [i][j] = '*'; do { //calling function to display seat arrangement printSeats (seats); //calling function to print menu printMenu (); //read user choice choice = reader.nextInt(); //read the seat column of user choice System.out.println("Enter the alphabet of seat (Character corresponding to Column): "); seatNo = reader.next(); seatNo = seatNo.toLowerCase(); ch = seatNo.charAt(0); //switch to get the corresponding column number switch (ch) { case 'a': column = 0; break; case 'b': column = 1; break; case 'c': column = 2; break; case 'd': column = 3; break; case 'e': column = 4; break; case 'f': column = 5; break; default: System.out.println("You have selected invalid column. Column A is alloted"); column = 0; break; } //to allot seat to the user if available switch(choice) { case 1: System.out.println("Enter the row number you want (choose from 1 - 2):"); row = reader.nextInt(); row = row-1; First(seats, row, column); break; case 2: System.out.println("Enter the row number you want ( choose from 3 - 7):"); row = reader.nextInt(); row = row-1; EconomySmoking(seats, row, column); break; case 3: System.out.println("Enter the row number you want ( choose from 8 - 13):"); row = reader.nextInt(); row = row-1; EconomyNonSmoking(seats, row, column); break; case 4: System.out.println("Visit again - Thank you"); System.exit(0); } } while(choice != 4); } //declaration of printSeats function public static void printSeats(char [][] seatStatus) { System.out.println (" A B C D E F"); for (int i = 0; i < 13; i++) { System.out.print ("Row " + (i + 1)); if (i < 9) System.out.print (" "); for (int j = 0; j < 6; j++) System.out.print (" " + seatStatus [i] [j]); System.out.println (); } } //declaration of printMenu function public static void printMenu() { { System.out.print (" MENU ======================================="); System.out.println (" 1.First Class 2.Economy Class - Smoking"); System.out.print ("3.Economy Class - Non Smoking 4.Quit this program" ); System.out.println (" ======================================="); System.out.println (" Enter your choice (1,2,3, or 4): "); } } //declaration of First function public static void First(char [][] seatStatus, int row, int col) { if ((row >= 0) && (row < 2)) { if(seatStatus[row][col] == '*') { System.out.print("Your requested seat("); System.out.print(row+1); switch(col) { case 0: System.out.print ("A"); break; case 1: System.out.print ("B"); break; case 2: System.out.print ("C"); break; case 3: System.out.print ("D"); break; case 4: System.out.print ("E"); break; case 5: System.out.print ("F"); break; } System.out.print(") is alloated for you."); seatStatus[row][col]='x'; } else { System.out.println("Your requested seat has already assigned to another customer."); } } else { System.out.println("Your requested seat is not a first-class seat, try again."); } } //declaration of EconomySmoking function public static void EconomySmoking(char [][] seatStatus, int row, int col) { if ((row >= 2) && (row < 7)) { if(seatStatus[row][col] == '*') { System.out.print("Your requested seat("); System.out.print(row+1); switch(col) { case 0: System.out.print ("A"); break; case 1: System.out.print ("B"); break; case 2: System.out.print ("C"); break; case 3: System.out.print ("D"); break; case 4: System.out.print ("E"); break; case 5: System.out.print ("F"); break; } System.out.print(") is alloated for you."); seatStatus[row][col]='x'; } else { System.out.println("Your requested seat has already assigned to another customer."); } } else { System.out.println("Your requested seat is not an Economy Smoking seat, try again."); } } //declaration of EconomyNonSmoking function public static void EconomyNonSmoking(char [][] seatStatus, int row, int col) { if ((row >= 7) && (row < 14)) { if(seatStatus[row][col] == '*') { System.out.print("Your requested seat("); System.out.print(row+1); switch(col) { case 0: System.out.print ("A"); break; case 1: System.out.print ("B"); break; case 2: System.out.print ("C"); break; case 3: System.out.print ("D"); break; case 4: System.out.print ("E"); break; case 5: System.out.print ("F"); break; } System.out.print(") is alloated for you."); seatStatus[row][col]='x'; } else { System.out.println("Your requested seat has already assigned to another customer."); } } else { System.out.println("Your requested seat is not an Economy Non-Smoking seat, try again."); } } } ------------------------------------------------------------------------------------------------- Output: A B C D E F 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 13 * * * * * * MENU ======================================= 1.First Class 2.Economy Class - Smoking 3.Economy Class - Non Smoking 4.Quit this program ======================================= Enter your choice (1,2,3, or 4): 1 Enter the alphabet of seat (Character corresponding to Column): A Enter the row number you want (choose from 1 - 2): 3 Your requested seat is not a first-class seat, try again. A B C D E F 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 13 * * * * * * MENU ======================================= 1.First Class 2.Economy Class - Smoking 3.Economy Class - Non Smoking 4.Quit this program ======================================= Enter your choice (1,2,3, or 4): 2 Enter the alphabet of seat (Character corresponding to Column): T You have selected invalid column. Column A is alloted Enter the row number you want ( choose from 3 - 7): 3 Your requested seat(3A) is alloated for you. A B C D E F Row 1 * * * * * * Row 2 * * * * * * Row 3 x * * * * * Row 4 * * * * * * Row 5 * * * * * * Row 6 * * * * * * Row 7 * * * * * * Row 8 * * * * * * Row 9 * * * * * * Row 10 * * * * * * Row 11 * * * * * * Row 12 * * * * * * Row 13 * * * * * * MENU ======================================= 1.First Class 2.Economy Class - Smoking 3.Economy Class - Non Smoking 4.Quit this program ======================================= Enter your choice (1,2,3, or 4): 3 Enter the alphabet of seat (Character corresponding to Column): D Enter the row number you want ( choose from 8 - 13): 9 Your requested seat(9D) is alloated for you. A B C D E F Row 1 * * * * * * Row 2 * * * * * * Row 3 x * * * * * Row 4 * * * * * * Row 5 * * * * * * Row 6 * * * * * * Row 7 * * * * * * Row 8 * * * * * * Row 9 * * * x * * Row 10 * * * * * * Row 11 * * * * * * Row 12 * * * * * * Row 13 * * * * * * MENU ======================================= 1.First Class 2.Economy Class - Smoking 3.Economy Class - Non Smoking 4.Quit this program ======================================= Enter your choice (1,2,3, or 4): Hope this would help you.