This is what I have so far import java.util.*; public class AssignSeats { static
ID: 3815385 • Letter: T
Question
This is what I have so far
import java.util.*;
public class AssignSeats
{
static Scanner console = new Scanner(System.in);
public static void main(String[]args)
{
//Declare variables
int choice = 0;
//Declare 2D Arrays
int rows = 13;
int seatCol = 6;
char[][] seats = new char[rows][seatCol];
menuDisplay();
choice = console.nextInt();
while(choice != 3)
{
switch(choice)
{
//if choice is 1 then show layout
case 1:
displaySeating(seats);
break;
//if choice is 2 then ask to select a seat
case 2:
selectSeat();
break;
}//end switch
menuDisplay();
choice = console.nextInt();
}//end while(choice != 3)
}//end main
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 void displaySeating(char[][] seats)
{
System.out.println(" A " + " B " + " C " + " D " + " F");
for (int row = 0; row < 13; row++)
{
for(int col = 0; col < 6; col++)
System.out.println("Row " + (row + 1));
}
}//end planeLayout
public static void selectSeat()
{
String seat, trash;
System.out.println("Enter the seat ID: ");
seat = console.nextLine();
trash = console.nextLine();
}//end selectSeat
}//end class
Java programming 5th edition chapter 9 two dimensional arrays
Nothing is private in this chapter and there is only one class
Write a 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 through 7 are business class, rows 8 through 13 are economy class.
The program should be menu driven (with a loop) as follows:
Main Menu
1.Display Seating Plan
2. Choose a seat
3. Exit
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.
Test the program:
Display the seating plan (it should have all*)
Choose seats A9, B4, E1, D13, C7, F4, D13, D4, B9, E4, A12, B4
Display the seating plan again (make sure it is correct)
Explanation / Answer
package seatplan;
import java.util.*;
public class AssignSeats {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Declare variables
int choice = 0;
// Declare 2D Arrays
int rows = 9;
int seatCol = 6;
char[][] seats = new char[rows][seatCol];
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 5; col++) {
seats[row][col]='*';
}
}
menuDisplay();
choice = console.nextInt();
while (choice != 3) {
switch (choice) {
// if choice is 1 then show layout
case 1:
displaySeating(seats);
break;
// if choice is 2 then ask to select a seat
case 2:
String seat=selectSeat();
verifySeatAvailability(seat, seats);
break;
}// end switch
menuDisplay();
choice = console.nextInt();
} // end while(choice != 3)
}// end main
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 void displaySeating(char[][] seats) {
System.out.println(" A " + " B " + " C " + " D " + " E");
for (int row = 0; row < 9; row++) {
System.out.print("Row" + (row + 1) + " ");
for (int col = 0; col < 5; col++) {
System.out.print(" " + seats[row][col]+ " ");
}
System.out.println();
}
}// end planeLayout
public static String selectSeat() {
String seat, trash;
System.out.println("Enter the seat ID Example C5: ");
console.nextLine();
seat = console.nextLine();
return seat;
}// end selectSeat
private static void verifySeatAvailability(String seat, char[][] seats) {
int row = 0;
int col = 0;
if (seat.charAt(0) == 'A')
col = 0;
if (seat.charAt(0) == 'B')
col = 1;
if (seat.charAt(0) == 'C')
col = 2;
if (seat.charAt(0) == 'D')
col = 3;
if (seat.charAt(0) == 'E')
col = 4;
row = Integer.parseInt(seat.substring(1));
if (seats[row-1][col] != 'X') {
seats[row-1][col] = 'X';
} else {
System.out.println("Seat already booked");
}
}
}// end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.