Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having difficulty with the last bit. I\'m trying to get not only for it to

ID: 3826819 • Letter: I

Question

I'm having difficulty with the last bit. I'm trying to get not only for it to show the seat ID and the layout, but also what seat it is.

Example: A9 is a Window seat in Economy Class

The seat ID and the Layout works great but that one I can't figure out

I have everbody in an Aisle seat in First class which I'm sure evrybody flying would enjoy it , but that is not how this program is supposed to work.

The seating plan should display:

//The lines are there just so you can see them

________A___B___C___D___E___F

Row 1___*___*____X___*____X__X

Row 2 __*___X____*___X____*__X

Row 3 __*___*____X___X____*__X

Etc.

The * indicates the seat is available. The X indicates the seat is taken.The chart will begin with all *'s as all seats are empty.

When choosing a seat, the user will enter the seat ID, for example C5, and the program will check to see if that seat is available. Is so, it will mark that seat as taken, display the seat ID with the class and seat type designation (for example, F4 is a Window seat in Business class). If not, it will display a 'seat taken' message and let the user choose another seat.

import java.util.*;

public class Airplane
{
static Scanner console = new Scanner(System.in);

public static void main(String[]args)
{
//Declare Variables
int choice = 0;
String seat, trash;
char colLetter = ' ';
int rowNum= 99;
  
//Declare 2D Arrays
int rows = 13;
int seatCol = 6;
char[][] seats = new char[rows][seatCol];
  
for(int row = 0; row < 13; row++)
{
for(int col = 0; col < 6; col++)
{
seats[row][col] = '*';
}
} //end of for loops that display the seating with all *



System.out.println(" The current seating chart ");
System.out.println("_____________________________________");
displaySeating(seats);
System.out.println();
  
menuDisplay();
System.out.println();
System.out.println("Please enter 1, 2, or 3");
choice = console.nextInt();
  
while(choice != 3)
{
//if choice is 1 then show the layout
if(choice == 1)
{
displaySeating(seats);
System.out.println();
}
//if choice is 2 then ask to select a seat
else if (choice == 2)
{
trash = console.nextLine();
seat = selectSeat();
//seatPlacement is the letter and typeClass is the number
System.out.println(seat + " is a " + seatPlacement(seats) + " in " + typeClass(seats));
System.out.println();
colLetter = seat.charAt(0);
rowNum = Integer.parseInt(seat.substring(1));

seatAvailability(colLetter, rowNum , seats);
}

menuDisplay();
System.out.println();
System.out.println("Please enter 1, 2, or 3");
choice = console.nextInt();
System.out.println();


}//end while(choice != 3)
  
  
}//end main

public static void displaySeating(char[][]seats)
{   
System.out.println(" A B C D E F");
  
for(int row = 0; row < 13; row++)
{
System.out.print("Row " + (row + 1));

if(row < 9)
{
System.out.print(" ");
}

for(int col = 0; col < 6; col++)
{

System.out.print(" " + seats[row][col]);
}
System.out.println();
}//end for loops
}//end displaySeating

public static void menuDisplay()
{
System.out.println("Main Menu");
System.out.println("1. Display Seating Plan");
System.out.println("2. Choose a seat");
System.out.println("3. Exit");
  
}//end menuDisplay

public static String selectSeat()
{
String seat;
  
System.out.println("Enter the seat ID: ");
seat = console.nextLine();

System.out.println();
  
return seat;

}//end selectSeat


public static void seatAvailability(char colLetter, int rowNum , char[][] seats)
{
  
int col = 6;
  
if(colLetter == 'A')
col = 0;

else if(colLetter == 'B')
col = 1;

else if(colLetter == 'C')
col = 2;

else if(colLetter == 'D')
col = 3;

else if(colLetter== 'E')
col = 4;

else
col = 5;


  
if(seats[rowNum - 1][col] != 'X')
{
seats[rowNum - 1][col] = 'X';
}
else
{
System.out.println("Seat already booked");
System.out.println();
}
}//end seatAvailability

public static String typeClass(char[][] seats)
{
String type;
int row = 0;
  
if (row <= 2)
type = "First Class";

else if (row <= 7)
type = "Business Class";

else
type = "Economy Class";

return type;
}//end typeClass

public static String seatPlacement(char[][] seats)
{
char letter = ' ';
String seatType;
  
if(letter == 'A' || letter == 'F')
seatType = "Window seat";

else if(letter == 'B' || letter == 'E')
seatType = "Center seat";

else
seatType = "Aisle seat";

return seatType;
  
}//end seatPlacement

}//end class

Explanation / Answer

You need to call the seatAvailability() method before the seatPlacement() functions because seatAvailability() determines whether seat is vacant or not . If it is vacant then only you need to show the placement. Also you need to pass the typeClass() method an extra argument specifying the row value. Please find the code below.

import java.util.*;

public class Airplane
{
static Scanner console = new Scanner(System.in);


public static void main(String[]args)
{
   int choice = 0;
   String seat, trash;
   char colLetter = ' ';
   int rowNum= 99;
   boolean ret = true;

  
   int rows = 13;
   int seatCol = 6;
   char[][] seats = new char[rows][seatCol];
  
   for(int row = 0; row < 13; row++)
   {
       for(int col = 0; col < 6; col++)
       {
           seats[row][col] = '*';
       }
   }

   System.out.println(" The current seating chart ");
   System.out.println("_____________________________________");
   displaySeating(seats);
   System.out.println();
  
   menuDisplay();

   System.out.println();
   System.out.println("Please enter 1, 2, or 3");
   choice = console.nextInt();
  
   while(choice != 3)
   {

       if(choice == 1)
       {
           displaySeating(seats);
           System.out.println();
       }
       else if (choice == 2)
       {
           trash = console.nextLine();
           seat = selectSeat();
           colLetter = seat.charAt(0);
           rowNum = Integer.parseInt(seat.substring(1));
           ret = seatAvailability(colLetter, rowNum , seats);
          
           if(ret){
               System.out.println(seat + " is a " + seatPlacement(seats) + " in " + typeClass(seats, rowNum));
           }
           System.out.println();
       }

       menuDisplay();
       System.out.println();
       System.out.println("Please enter 1, 2, or 3");
       choice = console.nextInt();
       System.out.println();

   }//end while(choice != 3)
  
  
}//end main


public static void displaySeating(char[][]seats)
{   
   System.out.println(" A B C D E F");
  
   for(int row = 0; row < 13; row++)
   {
       System.out.print("Row " + (row + 1));

       if(row < 9)
       {
           System.out.print(" ");
       }

       for(int col = 0; col < 6; col++)
       {

           System.out.print(" " + seats[row][col]);
       }
       System.out.println();
   }//end for loops
}//end displaySeating

public static void menuDisplay()
{
System.out.println("Main Menu");
System.out.println("1. Display Seating Plan");
System.out.println("2. Choose a seat");
System.out.println("3. Exit");
  
}//end menuDisplay

public static String selectSeat()
{
String seat;
  
System.out.println("Enter the seat ID: ");
seat = console.nextLine();
System.out.println();
  
return seat;

}//end selectSeat

public static boolean seatAvailability(char colLetter, int rowNum , char[][] seats)
{
  
int col = 6;
  
if(colLetter == 'A')
col = 0;

else if(colLetter == 'B')
col = 1;

else if(colLetter == 'C')
col = 2;

else if(colLetter == 'D')
col = 3;

else if(colLetter== 'E')
col = 4;

else
col = 5;


  
if(seats[rowNum - 1][col] != 'X')
{
seats[rowNum - 1][col] = 'X';
return true;
}
else
{
System.out.println("Seat already booked");
System.out.println();
return false;
}
}//end seatAvailability

public static String typeClass(char[][] seats, int row)
{
String type;
  
if (row <= 2)
type = "First Class";

else if (row <= 7)
type = "Business Class";

else
type = "Economy Class";

return type;
}//end typeClass

public static String seatPlacement(char[][] seats)
{
char letter = ' ';
String seatType;
  
if(letter == 'A' || letter == 'F')
seatType = "Window seat";

else if(letter == 'B' || letter == 'E')
seatType = "Center seat";

else
seatType = "Aisle seat";

return seatType;
  
}//end seatPlacement
}//end class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote