0

LOL
Well how bout this.......how bout I just change the parameters for each method?
Instead of lets say first class in the for loop where int i = 0 and then i > 3.....i make it i > 2?
And then do the same thing for all of the other methods?

0

Yes, you can say that first class is from 0-2 instead of 1-3 and so on, it's a good solution.

Edited by apines: n/a

0

Now for the thing where if all of the classes are full it tells the user to take another flight.....how would I do that? I mean it is all in different methods......I can't make a method that uses all of those methods right? OR perhaps a boolean method that uses all of those methods? Doesn't it become too complicated and confusing?

Edited by Tankadin: n/a

0

First try to implement something that works. Then we will see if indeed it is confusing and can be simplified.

0

Ok I added the necessary code and new boolean and options in my if else in each case.
What do you guys think?

import java.util.*;
public class airlineReservation
{
  public static final int MAX_COLUMN = 4;
  public static final int MAX_ROW = 20;

  public static void main(String[] args)
  {
    
    int[][] totalSeats = new int[MAX_ROW][MAX_COLUMN];
    
    Scanner input = new Scanner(System.in);
    
    displayMenu();
    int menuChoice;
    menuChoice = input.nextInt();
    while (menuChoice < 6)
    {
    switch(menuChoice)
    {
      case 1:
        System.out.println("You have Chosen First Class!");
        if (!isFirstClassFull(totalSeats))
        {
        makeFirstReservation(totalSeats,input);
        }
        else if(!isAllFull(totalSeats))
        {
          System.out.println("This Class is Full Please Choose Another Class");
        }
        else
        {
          System.out.println("Flight is Full.....Another Flight Will Be Leaving in 3 Hours!");
        }
        displayMenu();
        menuChoice = input.nextInt();
        break;
      case 2:
        System.out.println("You have Chosen Business Class!");
        if (!isBusinessClassFull(totalSeats))
        {
        makeBusinessReservation(totalSeats,input);
        }
        else if (!isAllFull(totalSeats))
        {
          System.out.println("Business Class is Full Please Choose Another Class");
        }
        else
        {
          System.out.println("Flight is Full.....Another Flight Will Be Leaving in 3 Hours!");
        }
        displayMenu();
        menuChoice = input.nextInt();
        break;
      case 3:
        System.out.println("You have Chosen Economy Class!");
        if (!isEconomyClassFull(totalSeats))
        {
        makeEconomyReservation(totalSeats,input);
        }
        else if (!isAllFull(totalSeats))
        {
          System.out.println("Economy Class is Full Please Choose Another Class");
        }
        else
        {
          System.out.println("Flight is Full.....Another Flight Will Be Leaving in 3 Hours!");
        }
        displayMenu();
        menuChoice = input.nextInt();
        break;
      case 4:
        System.out.println("You have Chosen to Display All Available Seating!");
        displayArray(totalSeats);
        displayMenu();
        menuChoice = input.nextInt();
        break;
      case 5:
        System.out.println("You have Chosen to Quit the Program!");
        break;
      }
    }
  }
  public static void makeFirstReservation(int[][] totalSeats, Scanner input)
  {
    for (int row = 0; row < 3; row++)
    {
      for (int col = 0; col < 4; col++)
    {
        if (totalSeats[row][col] == 0)
        {
          totalSeats[row][col] = 1;
          System.out.println("You have been assigned a First class seat " + col + " in row " + row);
          return;
        }      
      }
    }
  }
  public static void makeBusinessReservation(int[][] totalSeats, Scanner input)
  {
    for (int row = 3; row < 7; row++)
    {
      for (int col = 0; col < 4; col++)
      {
      if (totalSeats[row][col] == 0)
        {
          totalSeats[row][col] = 1;
          System.out.println("You have been assigned a Business class seat " + col + " in row " + row);
          return;
        }
      }
    }
  }
  public static void makeEconomyReservation(int[][] totalSeats, Scanner input)
  {
    for (int row = 7; row < 20; row++)
    {
      for (int col = 0; col < 4; col++)
      {
      if (totalSeats[row][col] == 0)
        {
          totalSeats[row][col] = 1;
          System.out.println("You have been assigned an Economy class seat " + col + " in row" + row);
          return;
         }
       }
      }
    }
  public static boolean isFirstClassFull(int[][] totalSeats)
  {
   for (int row = 0; row < 3; row++) 
     {
      for (int col = 0; col < 4; col++) 
        {
         if (totalSeats[row][col] == 0) 
            return false;
      }
    }
   return true;
  }
  public static boolean isBusinessClassFull(int[][] totalSeats)
  {
    for (int row = 4; row < 7; row++)
    {
      for (int col = 0; col < 4; col++)
      {
        if (totalSeats[row][col] == 0)
          return false;
      }
    }
    return true;
  }
  public static boolean isEconomyClassFull(int[][] totalSeats)
  {
    for (int row = 8; row < 20; row++)
    {
      for (int col = 0; col < 4; col++)
      {
        if (totalSeats[row][col] == 0)
              return false;
      }
    }
    return true;
  }
  public static boolean isAllFull(int[][] totalSeats)
  {
    for (int row = 0; row < 20; row++)
    {
      for (int col = 0; col < 4; col++)
      {
        if (totalSeats[row][col] == 0)
          return false;
      }
    }
    return true;
  }
  public static void displayArray(int[][] totalSeats)
  {
    for (int row = 0; row < totalSeats.length; row++)
    {
      for (int col = 0; col < totalSeats[row].length; col++)
      {
        System.out.print(totalSeats[row][col]);
      }
      System.out.println();
    }
  }
  public static void displayMenu()
  {
    System.out.println("Welcome to Never Crash Airlines");
    System.out.println("We almost hardly ever crash!");
    System.out.println("Below are our menu options for picking flights and checking which classes are open");
    System.out.println("Choose and Option 1-5!");
    System.out.println("1. First Class");
    System.out.println("2. Business Class");
    System.out.println("3. Economy Class");
    System.out.println("4. Display All Available Seats!");
    System.out.println("5. Quit!");
  }
}
0

If all the plane is full, perhaps you want to exit the program and not display the reservation menu again?

0

Oh yea good call.....how do I do that?
I tried System.exit(0); a while ago and it never seemed to work.

0

System.exit(n); should definitely work to exit the program. If it didn't work before, you might have put it in the wrong place.

By the way, your isAllFull method is fine the way it is, but you guessed correctly that you could have used your existing methods to see if the plane is full like this:

public static boolean isAllFull(int[][] totalSeats) {
   return isFirstClassFull(totalSeats) &&
          isBusinessClassFull(totalSeats) &&
          isEconomyClassFull(totalSeats);
}
0

don't you have to call those methods in the argument parenthesis for it to work that way?

0

The methods are visible throughout the class, the members have their scope meaning that their visibility is depending on where they were created.

0

Oh cool so it is just a positioning thing so as long as it at the end of the program.

0

In Java the order of the methods does not matter - inside the class all the methods are visible and outside of it it depends on the modifier (public, private etc')

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.