hello everyone need help with my program this is a airplane seating program for reservations it's a 2d array of 000 .. and when i take a seat it changs to 1 we have first class supposed to be first 2 rows and rows 3 to 7 are business class, and rows 8 to 12 are economy class ..
what i am reallly having problem with is when i enter number of seats i want to change values of the array form 0 to 1 depending on number of seats currently he changes all values to 1 :(
i need to finish it by tommorow plz help ..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package airplane.seating;
import java.util.Scanner;
import sun.security.util.Length;
/**
 *
 * @author Home
 */
public class AirplaneSeating {

public static double firstClass(int [][] airplane, int seats, double price) 
{ 
    boolean emptySit = false;
   for(int i =0;i<3;i++)
        {
         for(int j = 0;j<7;j++)
         {

              if (airplane[i][j] == 0)
          {
              emptySit = true;

          }
              if(emptySit)
              {
                  airplane[i][j] = 1;

              }
              else
              {
                   airplane[i][j] = 0;
              }
             System.out.print(airplane[i][j] + "   ");
         }
         System.out.println();
        }

         return price;

}

public static void displaySeat(int [][] airplane)  
{
     System.out.println("-------------------------------------------");
        for(int i =0;i<airplane.length;i++)
        {
         for(int j = 0;j<airplane[i].length;j++)
         {
             System.out.print(airplane[i][j] + "   ");
         }
              System.out.println();
        }     
        System.out.println("-------------------------------------------\n");
}
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);
        int [][] matrix = new int[12][7];
        System.out.println("Welcome To The Airplane Managment System");
        System.out.println("-------------------------------------------\n" +
                           "1. Book First Class Seats\n" +
                           "2. Book Buisness Class Seats\n" +
                           "3. Book Economy Class Seats\n" +
                           "4. View air Plane Seating assignment\n" +
                           "5. Know Fair\n" +
                           "6. System Reset\n" +
                           "7. Exit From The Booking System\n" +
                           "Enter Your Choice :" );

        int choice = input.nextInt();

        switch(choice)
        {
            case 1: 
            System.out.println("Enter How Many seats you want to book in First Class ?");
            int seat = input.nextInt();  
            double price=seat * 1050;
             for(int i =0;i<3;i++)
        {
         for(int j = 0;j<7;j++)
         {
               if (seat > 0) {
                  matrix[i][j]++;

              } }
        }
            System.out.println("Total Fair of the booking is " + price);
            firstClass(matrix,seat,price);

            break;

        //Displaying Matrix   
        case 4:     
        displaySeat(matrix);
            break;
        }

    }
}

Line 29 sets the array element to a 1. How often should that line be executed? What code controls how often that line is exceuted?

You should not hardcode numbers to control for loops. See the 3 in line 17 and the 7 in line 19.
These values should either be the size of the array from the .length property or a variable passed to the method.

There are couple mistakes in your code...

1) When you said first 2 rows, in array data type, it means 0 & 1 indices because array uses 0 index (value from 0 up to array_length-1). In your code, line 17 for example, is going through 0, 1, and 2 which means the first 3 rows, not 2.
2) If you are going to assign 1 to each seat, you should use matrix[i][j]=1; instead of matrix[i][j]++;.
3) The reason it assigns all to 1 because of lines 80~88. You did not decrease the number of seat after you assign a seat to your matrix. As a result, every seat is assigned because the number of seat will always be greater than 0.
4) I am not sure what the purpose of firstClass() method...

Now question... Do you automatically assign seat for users? The reason I ask is because your current program attempts to do so without checking whether the seat is already assigned.

thx all for replying
this is the purpose of the methods copide fro the assignment ..

  1. Book First Class Seats: This option is used when a customer want to reserve First Class Seats in Air Plane, Rows 1 and 2 are reserved for First Class, you have to make sure that if the seat is available, you will allow customer to reserve seats in First Class, Customer can reserve more than one seats (User will enter seats in all the cases).
    To solve this option you have to use a method and signature of the method is given underneath

public static double firstClass(int [][] airplane, int seats, double price) where airplane is a 2-D Array and seats is the number of seats customer want to reserve and price is the price of one seat. This method will reserve seats in first class only, if requested seats are available and return total amount customer has to pay for his seats. This method returns -1 in case seat is not available.

  1. Book Business Class Seats : This option is used when a customer want to reserve Business Class Seats in Air Plane, Rows 3 and 7 are reserved for Business Class, you have to make sure that if the seat is available, you will allow customer to reserve seats in Business Class, Customer can reserve more than one seats.
    To solve this option you have to use a method and signature of the method is given underneath

public static double businessClass (int [][] airplane, int seats, double price) where airplane is a 2-D Array and seats is the number of seats customer want to reserve and price is the price of one seat. This method will reserve seats in Business class only, if requested seats are available and return total amount customer has to pay for his seats. This method returns -1 in case seat is not available.

  1. Book Economy Class Seats : This option is used when a customer want to reserve Economy Class Seats in Air Plane, Rows 8 and 12 are reserved for Economy Class, you have to make sure that if the seat is available, you will allow customer to reserve seats in Economy Class, Customer can reserve more than one seats.
    To solve this option you have to use a method and signature of the method is given underneath

public static double economyClass (int [][] airplane, int seats, double price) where airplane is a 2-D Array and seats is the number of seats customer want to reserve and price is the price of one seat. This method will reserve seats in Economy class only, if requested seats are available and return total amount customer has to pay for his seats. This method returns -1 in case seat is not available.

i didn't understand what you meant by decrease the seat ..
currently it changs first 2 rows to 1
this is how it should be http://imageshack.us/scaled/landing/802/86702775.png ..
and no it does'nt sign automatic it checks if seat is available or not ..

Why does the code in lines 80 to 88 add 1 to all the elements in the array when the value of seat is > 1?

i know it's wrong but what should i change to make him change elements of the array depending on number of seats entered ..

I see. So your lines 79~89 should be moved into your firstClass() method definition because you are assigning the seat outside the method; where as, your requirement asks you to implement it inside the method firstClass().

To explain the decrement of seat, please see below.

// your original code
int seat = input.nextInt();    // #1
double price=seat * 1050;      // #2
for(int i =0;i<3;i++) {        // #3
  for(int j = 0;j<7;j++) {     // #4
    if (seat > 0) {            // #5
      matrix[i][j]++;          // #6
    }
  }
}

/*
For example, a user enters 1 as the input for number of seat (#1).
The loop start from i=0 until i=2 for 3 rows (#3).
For each column in the i value up to 7 (#4)
  now i=0, j=0
  it is true for #5 (seat > 0 because seat is 1)
  matrix[0][0] = 1 because it is increased from 0 (#6)
  now i=0, j=1
  it is true for #5 (seat > 0 because seat is 1)
  matrix[0][1] = 1 because it is increased from 0 (#6)
  ...
  ...
  now i=2, j=0
  it is true for #5 (seat > 0 because seat is 1)
  matrix[2][0] = 1 because it is increased from 0 (#6)
  now i=2, j=1
  it is true for #5 (seat > 0 because seat is 1)
  matrix[2][1] = 1 because it is increased from 0 (#6)
  ...

So you should see that every single value in the matrix will be 1 because
the seat variable is always greater than 0.
*/

Can you post the current version of the code that has fixed the problems mentioned so far?

`

Inline Code Example Here
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package airplane.seating;
import java.util.Scanner;

/**
 *
 * @author Home
 */
public class AirplaneSeating {

public static double firstClass(int [][] airplane, int seats, double price) 
{ 
    boolean emptySit = false;

   for(int i =0;i<airplane.length;i++)
        {
         for(int j = 0;j<airplane[i].length;j++)
         {

              if (airplane[i][j] == 0)
          {
              emptySit = true;

          }
         System.out.print(airplane[i][j] + "   ");
         }
         System.out.println();
        }

         return price;

}
    public static void reSetSystem(int [][] airplane)
{
    Scanner input = new Scanner(System.in);
    System.out.println("Do you want to exit the program (Y/N)?: ");
    char letter;
    String answer = input.nextLine(); 
    letter = answer.charAt(0);
    while(letter != 'Y' && letter != 'y');
    }
     public static void displayMenu()
         {
        System.out.println("Welcome To The Airplane Managment System");
        System.out.println("-------------------------------------------\n" +
                           "1. Book First Class Seats\n" +
                           "2. Book Buisness Class Seats\n" +
                           "3. Book Economy Class Seats\n" +
                           "4. View air Plane Seating assignment\n" +
                           "5. Know Fair\n" +
                           "6. System Reset\n" +
                           "7. Exit From The Booking System\n" +
                           "Enter Your Choice :" );
         }

public static void displaySeat(int [][] airplane)  
{
     System.out.println("-------------------------------------------");
        for(int i =0;i<airplane.length;i++)
        {
         for(int j = 0;j<airplane[i].length;j++)
         {
             System.out.print(airplane[i][j] + "   ");
         }
              System.out.println();
        }     
        System.out.println("-------------------------------------------\n");
}
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);
        displayMenu();
        int [][] matrix = new int[12][7];

        int choice = input.nextInt();

        switch(choice)
        {
            case 1: 
            System.out.println("Enter How Many seats you want to book in First Class ?");
            int seat = input.nextInt();  
            double price= seat * 1050;

             for(int i =0;i<2;i++)
        {
         for(int j = 0;j<7;j++)
         {
               if (seat > 0) 
               {
                  matrix[i][j] =1;    
              } 
         }
        }
            System.out.println("Total Fair of the booking is " + price);
            firstClass(matrix,seat,price);

            break;

        //Displaying Matrix   
        case 4:     
        displaySeat(matrix);
            break;


        case 6:
        reSetSystem(matrix);
        break;
        }
    }
}

`

The code still has hardcoded numbers in the if statements. They shoukd be replaced by either the array's length attribute or by varialbes.

       for(int i =0;i<2;i++)
        {
         for(int j = 0;j<7;j++)

Can you copy the contents of the console for when you execute the program showing what you entered and what the program printed out? Add some comments to the print out describing what is wrong with it.

What is the firstClass() method supposed to do? It returns a value that is ignored. The emptySeat variable is never used.

i don't want it to be one always it needs to be like in the picture

I think you cannot imagine how matrix is related to a plane seats right now. How about this...

/*
Imagine that the matrix represents each seat in an air plane
            ......
          ,        ,
        /            \
      /                \
     | 0 0 0    0 0 0 0 |       row 1, first class  -- index 0
     | 0 0 0    0 0 0 0 |       row 2, first class  -- index 1
     | 0 0 0    0 0 0 0 |       row 3, business class  -- index 2
     | 0 0 0    0 0 0 0 |       row 4, business class  -- index 3
     | 0 0 0    0 0 0 0 |       row 5, business class  -- index 4
     | 0 0 0    0 0 0 0 |       row 6, business class  -- index 5
    /| 0 0 0    0 0 0 0 |\      row 7, business class  -- index 6
   / | 0 0 0    0 0 0 0 | \     row 8, economic class  -- index 7
  /  | 0 0 0    0 0 0 0 |  \    row 9, economic class  -- index 8
     | 0 0 0    0 0 0 0 |       row 10, economic class  -- index 9
     | 0 0 0    0 0 0 0 |       row 11, economic class  -- index 10
     | 0 0 0    0 0 0 0 |       row 12, economic class  -- index 11
     +------------------+
Column 1 2 3    4 5 6 7

So if the seat is empty, the value inside the matrix should be 0.
If someone has already booked the seat, the value inside the seat
  should be non-zero (or 1).
When a client wants to buy 3 seats at the same time, your program
  should automatically assign 3 consecutive seats in the matrix to
  the client. 3 consecutive does not need to be on the same row,
  but program-wised ordering.

  For example, row 1 (0 index) seats from column 1 to 6 are booked
  (look below)
            ......
          ,        ,
        /            \
      /                \
     | 1 1 1    1 1 1 0 |       row 1, first class  -- index 0
     | 0 0 0    0 0 0 0 |       row 2, first class  -- index 1

  A client wants to book 2 seats, your program will book row 1 column 7
  and row 2 column 1 as it is a consecutive value in your matrix
  (when you use loop).
            ......
          ,        ,
        /            \
      /                \
     | 1 1 1    1 1 1 1 |       row 1, first class  -- index 0
     | 1 0 0    0 0 0 0 |       row 2, first class  -- index 1
*/

Hope the drawing gives you a clearer view.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.