Hi,
I am learning core java fundamentals, experimenting with the logic and getting a hold of the programming concepts. I have written a simple code having a Flight class and trying to understand why the "if (passengers>seats)", condition does not get called and the message does not print on screen from handleTooMany() method when i create new passenger instances in main, where count is > 250. Here is the code:
public class Main {
public static void main(String[] args) {
Flight Flight2 = new Flight();
int i;
for (i=0; i<255; i++){
Flight2.add1Passenger();
}
System.out.println("No., of passengers = "+ Flight2.passengers);
}
public static class Flight {
int passengers;
int seats;
Flight(){
passengers=0;
seats=250;
}
public void add1Passenger(){
if (passengers<seats)
passengers +=1;
//System.out.println("No..., of passengers = "+ passengers);
else handleTooMany();
}
private void handleTooMany(){
if (passengers>seats)
System.out.println("Too many passengers. Passengers exceed seats...");
}
}
}
Thank you.