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

Hi I need help on a homework assignment the seating chart for the airline progra

ID: 3772525 • Letter: H

Question

Hi I need help on a homework assignment the seating chart for the airline program doesnt come up big and with A-f as columns and rows 1-13 for first class 1-2 business class 3-7 and economy class 8-13 it should have * as available seats and x as taken seats also it should print out the chart each time they want to reserve a seat not until they quit and they should go back to the main menu if they choose to go back to the previous menu here is my code.

import java.io.*;
import java.util.*;
public class AirReservations2 {
   static Scanner scan = new Scanner(System.in);

   public static void main(String[] args) {
   // TODO Auto-generated method stub
   {
   // creats new array, at this point all of the values are set to null
   char[][] seats = new char[13][6];
   choiceSelection(seats);
   // this method will populate the array with "*" instead of nulls,
   // denoting an empty seat
   pupulateGrid(seats);
   // prompt user, 0 to quit 1 to reserve
   choiceMenu(seats);
   printGrid(seats);
   }
   }

   private static void choiceSelection(char[][] seats) {
   int choice = menuSystemCall();
   System.out.println("Choice Choosen--->"+choice);
   switch (choice) {
   case 0:
   System.exit(0);

   break;
   case 1:
   seatingSystemCall(seats, choice);
   break;
   case 2:
   choice = menuSystemCall();
   seatingSystemCall(seats, choice);
   break;
   case 3:
   seatingSystemCall(seats, choice);
   break;
   default:
   choice = menuSystemCall();
   break;
   }
   }

   public static void choiceMenu(char[][] seats) {
   System.out.println("Enter 0 to quit or 1 to reserve a ticket");
   // choice is the result of the above prompt
   int choice = scan.nextInt();
   // if choice is 0 then the user does not run this loop and they are done
   // booking id choice is 1 the loop below will run.
   while (choice != 0) {
   // the rowMessage() asks the user for a row choice i.e. A, B, C, D,
   // E
   // converts it to an int. We do this so we can use the numerical
   // value to index the array.
   int row = rowMessage();
   // the colMessage() asks the user for a numerical row choice
   // and returns and stores it into column
   int col = colMessage();
   // HERE IS WHERE WE CHECK IF THE ROW AND COL ENTERD IS VALID
   if (row < 13 && col < 6) {
   // System.out.println("Seat Status--->"+seats[row][col]);
   // HERE IS WHERE WE CHECK IF THE seat is taken, if it is print
   // seat taken
   if (seats[row][col] == 'X')
   System.out.println("seat taken by others already");

   // if the seat is empty mark it occupied using 'x'
   if (seats[row][col] == '*')
   System.out.println("seat reserved for you");
   seats[row][col] = 'x';
   }// END IF FOR VALID CHECK HERE

   System.out.print("Enter 0 to quit or 1 to reserve a ticket");
   choice = scan.nextInt();
   }
   }

   private static void seatingSystemCall(char[][] seats, int ch) {
   SeatingSystem2 mySystem = new SeatingSystem2();
   if (ch == 3 || ch == 1)
   mySystem.displaySeatingchart(seats);
   mySystem.seatAvailable(seats, "seatCode");
   mySystem.seatAssign(seats, "seatCode");
   if (ch == 2)
   mySystem.seatUnAssign(seats, "seatCode");
   }

   private static int menuSystemCall() {
   MenuSystem2 myMenu = new MenuSystem2();
   int choice = myMenu.presentMenu("main");
   System.out.println("Choice menuSystemCall--->"+choice);
   if(choice == 0 || choice==2||choice==3){
   return choice;
   }
   myMenu.presentSeatingChoice("sc-menu", 0);
   myMenu.presentClassChoice("cc-menu", 0);
   myMenu.presentRowChoiceMenu("rc-menu", 0);
   myMenu.presentColumnChoiceMenu("col-menu", 0);
   return choice;
   }

   public static int rowMessage() {
   System.out.println(" Enter your row as letter ");
   String rowLetter = scan.next();
   if (rowLetter.equals("A"))
   return 0;
   if (rowLetter.equals("B"))
   return 1;
   if (rowLetter.equals("C"))
   return 2;
   if (rowLetter.equals("D"))
   return 3;
   if (rowLetter.equals("E"))
   return 4;
   if (rowLetter.equals("F"))
   return 5;
  

   return 6;
   }

   public static int colMessage() {
   System.out.println(" Enter your column number ");
   int col = scan.nextInt();
   return col;
   }

   public static void printGrid(char[][] multiPassed) {
   for (int i = 0; i < 13; i++) {
   for (int j = 0; j < 6; j++) {
   System.out.print(multiPassed[i][j]);
   }
   System.out.println("");
   }
   }

   public static void pupulateGrid(char[][] multiPassed) {
   for (int i = 0; i < 13; i++) {
   for (int j = 0; j < 6; j++) {
   multiPassed[i][j] = '*';
   }
   }
   }
   }

import java.util.Scanner;


public class MenuSystem2 {
   //This will print the menuId which what the user want to do for the first
   //MainMenu
   public static int presentMenu(String menuId)
   { Scanner scan = new Scanner(System.in);
   if(menuId.equals("main"))//This will equal the main menu of the Airline Reservation System
   {
   System.out.println("1. Book a reservation ");
   System.out.println("2. Cancel a reservation ");
   System.out.println("3. Print Current Seating Chart ");
   System.out.println("Press 0 to Exit System " );
   System.out.println("Please choose an option to proceed|| Pres '0' to exit to main menu ");
   menuId = scan.nextLine();
  
   }
   return Integer.parseInt(menuId);//This will return the main menu
   }
  
   //This will let the user to pick a choice either a ticket type or return
   //to the main menu
   public static int presentSeatingChoice(String menuId, int scChoicemenu)
   {
   Scanner scan = new Scanner(System.in);
   if (menuId.equals("sc-menu"))//This method will be be determined what the
   //user will want to do.
   {
   System.out.println("Seating Choice Menu");

   System.out.println("1) Choose Ticket Type (First Class, Business Class, Economy Class)");

   System.out.println("2) Exit to Main Menu");
   System.out.println("Pick a choice to proceed to the next section or type a to '2' to go back to the MainMenu");
   scChoicemenu = scan.nextInt();
   }
   int sChoice = scChoicemenu;
   return sChoice;//This will return the Seating Choice Menu

   }
  
   //This method will return the class choice menu
   public static int presentClassChoice(String menuId, int ccChoicemenu)
   {
   Scanner scan = new Scanner(System.in);
   if (menuId.equals("cc-menu"))// This will also equal to the menuId for
   // the class choice menu to appear
   {
   System.out.println("Class Choice Menu");
  
   System.out.println("1. Firstclass");
   System.out.println("2. Businessclass");
   System.out.println("3. Economyclass");
   System.out.println("0. Exit to the MainMenu");
   System.out.println("Please choose a Class choice or type '0'to return to return to the main menu");
   ccChoicemenu = scan.nextInt();
   }
   int cChoice = ccChoicemenu;

   return cChoice;// This will return the Class Choice Menu
   }
   //This method will let the user to choose what row they like to seat in
   public static int presentRowChoiceMenu(String menuId, int rwChoicemenu)   
   {
   Scanner scan = new Scanner(System.in);
   if(menuId.equals("rc-menu"));//Row Choice Menu inside a if statement that
   //Will equals to the choices below
   System.out.println("Row Choice Menu");
   System.out.println("1)(For first class type numbers type '1-2'");
   System.out.println("2)(For Business class type '3-7'");
   System.out.println("3)(For Economy class type '8-13' ");
   System.out.println("Choose a option to proceed to the column choice");
   rwChoicemenu= scan.nextInt();
  
   int rwChoice= rwChoicemenu;
   return rwChoice;
   }

   //This method is where the column choices will take place to let the user
   //Decide what column they like to place in
   public static int presentColumnChoiceMenu(String menuId, int cwChoice)
   {
   Scanner scan = new Scanner(System.in);
   System.out.println("Seat Choice Menu");
   if(menuId.equals("col-menu"));//Column Choice Menu will also be equal to the
   //choices below
   {
   System.out.println("Pick a letter which you want to reserve in 'A=1' , 'B=2', 'C=3', 'D=4', 'E=5', 'F=6' ");

   System.out.println("Choose a option to proceed || Pres '0' to exit to main menu ");
   cwChoice = scan.nextInt();
   }
   int cChoice=cwChoice;
   return cwChoice;
   }
   }

public class SeatingSystem2 {
public static final int ROWS = 13;
public static final int COLS = 6;

public static final void seat(char[][] seatingChart) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
seatingChart[i][j] = '*';
}
}
}// this is where the seatingSystem will determine if the seat is Available
// or not

public static boolean seatAvailable(char[][] seatingChart, String seatCode) {
int row = 1, index = 1, col = 1;
while (seatCode.charAt(index) >= '0' && seatCode.charAt(index) <= '9') {
row = row * 10 + (int) seatCode.charAt(index) - 48;
index++;
}
if(index>(int) 'A')
col = (int) index - (int) 'A';
System.out.println(row +"--->"+col);
if (seatingChart[row][col] == '*')
return true;
else
return false;
}

// This method will be an integer and a char statement with a nested
// if else statment to assign the seat
public static int seatAssign(char[][] seatingChart, String seatCode) {
int row = 0, index = 0, col = 0;
while (seatCode.charAt(index) >= '0' && seatCode.charAt(index) <= '9') {
row = row * 10 + (int) seatCode.charAt(index) - 48;
index++;
}
if(index>(int) 'A')
col = (int) index - (int) 'A';
System.out.println("roiw"+row+"----->"+col);
if (seatingChart[row][col] == '*') {
seatingChart[row][col] = 'X';
return 1;
}
return 0;
}

public static int seatUnAssign(char[][] seatingChart, String seatCode) {
int row = 0, index = 0, col = 0;
while (seatCode.charAt(index) >= '0' && seatCode.charAt(index) <= '9') {
row = row * 10 + (int) seatCode.charAt(index) - 48;
index++;
}
if(index>(int) 'A')
col = (int) index - (int) 'A';
if (seatingChart[row][col] == 'X') {
seatingChart[row][col] = '*';
return 1;
}
return 0;
}

// this method will display and print the seating chart
public static void displaySeatingchart(char[][] seatingChart) {
System.out.print(" ");

for (int j = 0; j < COLS; j++) {
System.out.print((char) ((int) 'A' + j) + " ");

}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.print("Row" + String.valueOf(i + 1) + " ");

for (int j = 0; j < COLS; j++)
System.out.print(seatingChart[i][j] + " ");
}
System.out.println();
}

  
  
   }

     

Explanation / Answer

Hi,

I tried to compile this code but getting lot of error.I tried to create new code..

import java.io.*;
import java.util.*;

public class Airline
{

    static final int MAX_SEATS = 8;

    public static void main(String[] args) {
        Airline airline = new Airline();
    }

    public Airline() {
        boolean done = false;

        String[] seats = new String[MAX_SEATS];
        initializeSeats(seats);

        do {
            printMainMenu();
            int choice = getMainMenuChoice();

            switch (choice) {
            case 1:
                makeReservation(seats);
                break;
            case 2:
                cancelReservation(seats);
                break;
            case 3:
                printSeatingChart(seats);
                break;
            case 4:
                done = true;
                break;
            }

        } while (!done);

    }

    void printMainMenu() {
        System.out.println(" Main Menu ");
        System.out.println("1. Make a reservation");
        System.out.println("2. Cancel a reservation");
        System.out.println("3. Print seating chart");
        System.out.println("4. Quit");
        System.out.println();
    }

    int getMainMenuChoice()
    {
        int choice; // choice entered
        boolean valid = false; // is choice valid?

        do {
            System.out.print("===> ");
            choice = Console.readInt();

            if (1 <= choice && choice <= 4) {
                valid = true;
            } else {
                System.out.println("Invalid choice.");
            }
        } while (!valid);

        return choice;
    }

    void initializeSeats(String[] seats) {
        for (int i = 0; i < seats.length; i++) {
            seats[i] = "";
        }
    }

    void makeReservation(String[] seats) {
        int seatIndex = findEmptySeat(seats); // index of first empty seat
        if (seatIndex == seats.length) {
            System.out.println("All seats are full. Sorry.");
        } else {
            String name = getPassengerName(); // passenger's name
            seats[seatIndex] = name;
            System.out.println(name + " has been assigned seat #"
                    + (seatIndex + 1));
        }
    }

    /**
     * Cancel a reservation
     */
    void cancelReservation(String[] seats) {
        int seatIndex = getSeatToCancel(); // index of seat to cancel
                                            // reservation for
        if (isEmpty(seats, seatIndex)) {
            System.out.println("Seat #" + (seatIndex + 1)
                    + " has not been reserved for anyone");
        } else {
            seats[seatIndex] = "";
            System.out
                    .println("Seat #" + (seatIndex + 1) + " is now available");
        }
    }

    /**
     * Print the seating chart
     */
    void printSeatingChart(String[] seats) {
        System.out.println(" Seating Chart ");
        for (int i = 0; i < seats.length; i++) {
            System.out.println((i + 1) + ". " + seats[i]);
        }
    }

    /**
     * Find the index of the first empty seat on the plane. If there are no
     * empty seats, return seats.length
     */
    int findEmptySeat(String[] seats) {
        for (int i = 0; i < seats.length; i++) {
            if (isEmpty(seats, i)) {
                return i;
            }
        }

        return seats.length;
    }

    /**
     * Yield whether seats[seatIndex] is an empty seat.
     */
    boolean isEmpty(String[] seats, int seatIndex) {
        return seats[seatIndex].equals("");
    }

    /**
     * Input a passenger's name
     */
    String getPassengerName() {
        System.out.print("Enter the passenger's name: ");
        return Console.readString();
    }

  
    int getSeatToCancel() {
        boolean valid = false; // is the seat number valid?
        int seat = 0; // seat number to cancel

        do {
            System.out.print("Enter the seat to cancel: ");
            // seat = Console.readInt();
            if (1 <= seat && seat <= MAX_SEATS) {
                valid = true;
            } else {
                System.out.println("Invalid seat number");
            }
        } while (!valid);

        return seat - 1;
    }

}