0

I am working on a program in which there are 10 seats available in a plane. First 5 are first class and rest and economy class. The user will type a seat number and my program will check if the seat is available. If the seat is available, print a boarding pass otherwise ask him/her to choose a different seat. Print the seats available.

In my program, I have an Array of booleans that is all true because the seats are available. Whenever a seat is picked, change the state to false.

But, I am getting a problem. My boolean array is changing to false when the seat is booked, but it is not recognizing that the seat is already picked and assign the same seat to the other user.

I really need help. I will be very thankful of you guys.

My code is below:

import java.util.Scanner;

// This program will reserve tickets for the passenger

public class Airline_Reservation 
{
// Create Arrays that will show how many tickets are available 
// Make boolean array 11 because tickets are from 1 to 10
private static boolean [] ticketsAvailable = new boolean [11];
// create a scanner
private static Scanner input = new Scanner(System.in);

// main method
public static void main(String[] args) 
{

    // initialize the array for seats
    for(int i = 1; i <= 10; i++)
    {
        // make every seat true so that it is available to book
        ticketsAvailable[i] = true;
    }

    // run a loop forever
    while(true)
    {
        // ask user to type the seat number
        System.out.println("\nPlease type the seatNumber: ");
        // get the seat number and store it in a seatNum variable
        int seatNum = input.nextInt();

        // if the seat chosen by user is available, print the boarding pass
        if (ticketsAvailable[seatNum] = true)
        {
            printBoarding(seatNum);
        }
        // if the seat chosen by user is already picked, go to shoError method
        else if (ticketsAvailable[seatNum] = false)
        {
            showError();
        }
    }

}

private static void printBoarding(int seatNumber)
{
    // This method will print the boarding pass for the user

        // check if its first class or economy class
        if (seatNumber >= 1 && seatNumber <= 5)
        {
            System.out.printf("%n%s%n%s  %s%n%s  %d%n%n",
                    "Boarding Pass", "Registered class:",
                    "First Class", "Seat Number:", seatNumber);
        }
        else if (seatNumber > 5 && seatNumber <= 10)
        {
            System.out.printf("%n%s%n%s  %s%n%s  %d%n%n",
                    "Boarding Pass", "Registered class:",
                    "Economy Class", "Seat Number:", seatNumber);
        }

        // make the seat unavailable for other people
        ticketsAvailable[seatNumber] = false;
}

private static void showError()
{
    // This method shows error when then ticket chosen is already picked

    // check the seat available and tell user if he/she wants it
    for (int i = 1; i <=10 ; i++)
    {
        // check all seats one by one and if any seat is available then prompt user t chose it
        if (ticketsAvailable[i] = true)
        {
            // check if its first class or Economy
            // First Class
            if ( i >= 1 && i <= 5)
            {
                // ask user if he/she wants the seat
                System.out.printf("%s  %s%n%s  %d%n%s",
                        "Class:", "First Class", "Seat Number:", i,
                        "Press 1 to take it or 2 to try another: ");
                int inputUser = input.nextInt(); // get user input

                // if user pick the seat
                switch(inputUser)
                {
                case 1:
                    printBoarding(i);
                    return;
                }
            }
            // Economy
            else if ( i > 5 && i <= 10)
            {
                // check if its first class or Economy
                if ( i >= 1 && i <= 5)
                {
                    // ask user if he/she wants the seat
                    System.out.printf("%s  %s%n%s  %d%n%s",
                            "Class:", "Economy Class", "Seat Number:", i,
                            "Press 1 to take it or 2 to try another: ");
                    int inputUser = input.nextInt(); // get user input

                    // if user pick the seat
                    switch(inputUser)
                    {
                    case 1:
                        printBoarding(i);
                        return;
                    }
                }
            }
        } // end if
    } // end for
}

}

Thanks!!

2
  • 3
    When you say if (ticketsAvailable[seatNum] = true) you actually set the value to true. Use == to compare and = to assign values. Commented Apr 24, 2015 at 2:00
  • I always thought that you use = for boolean variables to compare. I will correct it now. Thank you very very much Commented Apr 24, 2015 at 2:01

2 Answers 2

6

When you are checking for equality in java you should use == not =. = is an assignment operator.

Sign up to request clarification or add additional context in comments.

5 Comments

But dont you use = for boolean variables
@Deep No, you use = for assignment, and == for comparison of primitives.
No. The type of variable doesn't impact the equality check as long as you are using native types.
@Deep You can, but it does not mean what you think it means. The first if makes ticketsAvailable[seatNum] true, and always succeeds. If you want to test the state rather than change it, use ==.
Thanks guys. It was my misconception. Now I got. Appreciate you help
0

You don't even need to use "=="

    // if the seat chosen by user is available, print the boarding pass
    if (ticketsAvailable[seatNum])
    {
        printBoarding(seatNum);
    }
    // if the seat chosen by user is already picked, go to shoError method
    else if (!ticketsAvailable[seatNum])
    {
        showError();
    }

As it is a boolean array, you can directly use the values in the if conditions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.