The seating plan should display: //The lines are there just so you can see them
ID: 3820995 • Letter: T
Question
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.
I keep having a problem with my program Please help
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];
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();
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)
{
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(" A B C D E F");
for(int row = 0; row < 13; row++)
{
System.out.print("Row " + (row + 1));
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 = 0;
if(colLetter == 'A')
col = 0;
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");
}
}//end seatAvailability
}//end class
Explanation / Answer
I went through the complete code. There are not major changes, 3 things are there that I think you should change to make this work perfectly. I am first writing the code and then explaining the things...
Source Code:
import java.util.*;
public class Airplane
{
static Scanner console = new Scanner(System.in);
static char[][] seats;
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;
seats = new char[rows][seatCol];
System.out.println(" The current seating chart ");
System.out.println("_____________________________________");
displaySeating();
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();
System.out.println();
}
//if choice is 2 then ask to select a seat
else if (choice == 2)
{
trash = console.nextLine();
seat = selectSeat();
colLetter = seat.charAt(0);
rowNum = Integer.parseInt(seat.substring(1));
seatAvailability(colLetter, rowNum);
}
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()
{
for(int row = 0; row < 13; row++)
{
for(int col = 0; col < 6; col++)
{
if(seats[row][col] == '')
seats[row][col] = '*';
}
}//end of for loops that display the seating with all *
System.out.println(" A B C D E F"); // added 1 space before A to make it in proper alignment
for(int row = 0; row < 13; row++)
{
System.out.printf("Row %2d",(row + 1));
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)
{
int col = 0;
switch (colLetter) {
case 'A':
case 'a':
col = 0;
break;
case 'B':
case 'b':
col = 1;
break;
case 'C':
case 'c':
col = 2;
break;
case 'D':
case 'd':
col = 3;
break;
case 'E':
case 'e':
col = 4;
break;
default:
col = 5;
break;
}
if(seats[rowNum - 1][col] != 'X')
{
seats[rowNum - 1][col] = 'X';
}
else
{
System.out.println("Seat already booked");
}
}//end seatAvailability
}//end class
Description:
The main problem you were getting was because of you had created the array seats [ ] [ ] in main(). And after that, you were passing the array to display function. Ok, that can be done as display() does not modify the content. But, in availability checking, you are changing the array values right.. but that change is not returned and written back in the original array. Plus, your given display() method makes ' * ' value for every entries at every time. So, that also need to be changed.
So, at first, I declared the char seats [ ] [ ] outside the main() method. As it will be accessed in main() as well, and as main() is static, you need to define this array as static as well then only main() can access it as per the rules.
Now, as we have this array outside of main(), we do not require to pass 'seats' as an argument to the functions. So, from both the functions displaySeating() and seatAvailability() definition and calling inside main(), I removed seats from there.
Next is, every time it was displaying * in every entries. So, this needs to be done whenever the displaySeating() is called for the first time. So, at the first time, as the seats[ ] [ ] is just defined, it will have the values null character ''. So, I checked for that and if the value is null, then only it is assigned to * otherwise this loop does not get executed.
After that, while running the program, I entered a2 but it did not work. The reason was the lowercase letters were not allowed. But, it is possible that user will also enter lower case letters. So, I changed those conditions for both uppercase and lowercase letters. And, the number of comparisons and if - else statements were many so I converted them in switch statement. Working is same, but this helps in writing code easily.
There is also 1 change in displaySeating(). I used printf() instead of print() so that the alignment can be proper. It was displaying till line 9 same, then from row 10, everything shifted right by 1 place. So, I printed the row number as a 2 digit number having 2 spaces to be written on the output so that everything came aligned.
I executed this code and checked for new booking, seat already booked,... different test cases and it is working fine.
The lines I added or modified are underlined in the above code.
Please comment if there is any query. Thank you. :)