简体   繁体   中英

How do i increment static variables in java

I'm very new to java but i have decent experience with c++ and python. So, I'm doing a question in which im required to implement an airplane booking system, which does the following -

1.initialize all seats to not occupied(false)

2.ask for input(eco or first class)

3.check if seat is not occupied

4.if seat is not occupied allocate seat else look for next seat

5.if economy seats are booked out, ask if user wants to bump up to first class

6.if user is negative display msg "next plane is in 3hrs"

but,

package oop;
import java.util.Arrays;
import java.util.Scanner;

public class AirplaneBooking {

    private final static int MAX_ECO_SEATS = 5;
    private final static int MAX_FIRST_CLASS_SEATS = 3;
    private final static boolean[] ECO_SEATS = new boolean[MAX_ECO_SEATS];
    private final static boolean[] FIRST_CLASS_SEATS = new boolean[MAX_FIRST_CLASS_SEATS];
    private static int current_eco_seat = 0;
    private static int current_first_class_seat = 0;

    public static void initialilze_seats(boolean[] first_class_seats, boolean[] eco_class_seats){
        Arrays.fill(first_class_seats, Boolean.FALSE);
        Arrays.fill(eco_class_seats, Boolean.FALSE);
    }

    public static void display(boolean[] seats){
        System.out.print("[");
        for(boolean seat : seats){
            System.out.print(seat + ",");
        }
        System.out.println("]");
    }

    public static void book_seat(boolean [] seats, int current_seat){
        seats[current_seat] = true;
        current_seat++;
        System.out.println(current_seat);
    }


    public static int user_input() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter 1 for Economy class or 2 for First class : ");
        int user_seat_prefrence = input.nextInt();
        if (user_seat_prefrence == 1){
            if(current_eco_seat < MAX_ECO_SEATS){
                book_seat(ECO_SEATS, current_eco_seat);
            }
            else{
                System.out.println("Looks like eco seats are full, would you like to book for first class insted(1/0) ?");
                Scanner next_input = new Scanner(System.in);
                int user_next_seat_prefrence = next_input.nextInt();
                if (user_next_seat_prefrence == 1){
                    book_seat(FIRST_CLASS_SEATS, current_first_class_seat);
                    user_seat_prefrence = 2;
                }
                else{
                    System.out.println("next flight leaves in 3 hrs");
                }
            }

        }

        else if  (user_seat_prefrence == 2){
            if (current_first_class_seat < MAX_FIRST_CLASS_SEATS){
                book_seat(FIRST_CLASS_SEATS, current_first_class_seat);
            }
            else{
                System.out.println("Looks like first class seats are full, would you like to book economy instead?(1/0)");
                int user_next_seat_prefrence = input.nextInt();
                if (user_next_seat_prefrence == 1){
                    book_seat(ECO_SEATS, current_eco_seat);
                    user_seat_prefrence = 1;
                }
                else{
                    System.out.println("Next flight leaves in 3hrs");
                }

            }
        }

        else {
            System.out.println("Enter valid option");
        }

        return user_seat_prefrence;
    }

    public static void print_boarding_pass(int user_seat_prefrence){
        if (user_seat_prefrence == 1){
            System.out.println("eco");
            System.out.println(current_eco_seat - 1);
        }
        else{
            System.out.println("first class");
            System.out.println(current_first_class_seat - 1); 
        }
    }
    public static void main(String args[]){
        initialilze_seats(FIRST_CLASS_SEATS, ECO_SEATS);
        display(FIRST_CLASS_SEATS);
        display(ECO_SEATS);
        while(true){
            int user_seat_prefrence = user_input();
            print_boarding_pass(user_seat_prefrence);    
            display(FIRST_CLASS_SEATS);
            display(ECO_SEATS);
            System.out.print("book another seat:");
            Scanner choice = new Scanner(System.in);
            boolean book_another_seat = choice.nextBoolean();
            if (book_another_seat == false)
                break;
         
        }
    }

}

The problem i'm having with this code is if the seats for eco class(for example) are full, the program is supposed to ask if i want to book for first class instead and wait for my input, if I press 1 it should book in first class but the program does not await for my input and proceeds to else statement instead.

Also, i use a static variable current_eco_seat and current_first_class_seat to keep track of the current seat being booked, and i pass that static variable to book_seat function, the program then books the seat and increments the current_eco_seat or current_first_class_seat (depending which is passed) so that next seat can be booked in next interation. But the static variable does not get incremented at all.

These are the only problems i have with the program.

Any help is appreciated, thanks

As Java calls methods by value,

Your problem about static is you are passing the value of current_seat to the book_seat method, so changing the value doesn't affect that variable after returning from the method.

To solve it just call the method and do not pass your static vars. It's static, so you have access it from everywhere.

ie:

public static void book_seat(boolean [] seats){
        seats[current_seat] = true;
        current_first_class_seat++;
        System.out.println(current_seat);
    }
  1. Checking Inout stream

Not sure wether your question is related to "static" variables or more related to "How to handle Input Stream?".

Regarding:

if I press 1 it should book in first class but the program does not await for my input and proceeds to else statement instead.

You should think about "flushing" the Input Stream before reading again. flush-clear-system-in-stdin-before-reading

  1. Method Parameter usage

On the other hand this method is wrong

    public static void book_seat(boolean [] seats, int current_seat){
    seats[current_seat] = true;
    current_seat++;
    System.out.println(current_seat);
}

this command has no affect, but printing an information to the User. The variable you used in the caller "current_eco_seat" will not change at all.

you don't need to insist incrementing the exact variable, just do the following:

  1. make book_seat() to return incremented value
    public static int book_seat(boolean [] seats, int current_seat) {
        seats[current_seat] = true;
        System.out.println(current_seat + 1);
        return current_seat + 1;
    }
  1. set returned value to current_first_class_seat or current_eco_seat
            if (current_first_class_seat < MAX_FIRST_CLASS_SEATS){
                current_first_class_seat = book_seat(FIRST_CLASS_SEATS, current_first_class_seat);
            }
            else{
                System.out.println("Looks like first class seats are full, would you like to book economy instead?(1/0)");
                int user_next_seat_prefrence = input.nextInt();
                if (user_next_seat_prefrence == 1){
                    current_eco_seat = book_seat(ECO_SEATS, current_eco_seat);
                    user_seat_prefrence = 1;
                }
                else{
                    System.out.println("Next flight leaves in 3hrs");
                }

            }

Then you can use book_seat() for both eco and first class reservations handling as previously you have intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM