Make an Airline Program Loop Through All of Its Methods

I know how you feel just starting out so I can help you a bit. You should try to write down what you are going to doing next time, it will really help you think about the problem. You are declaring 11 seats all over the place, and just seem really confused. I wrote this code really quick, and should get you going in the right direction. Best of luck with programming.

import java.util.Scanner;

public class Server {   

boolean[] firstClass = new boolean[5];
boolean[] economyClass = new boolean[5];
int numFirstClass = 0;
int numEconomyClass = 0;
Scanner scan = new Scanner(System.in);

public void book(){
    System.out.println("Press 1 for economy and 2 for first class.");
    int choice = scan.nextInt();
    if (choice == 1)
        economyClass();
    else if (choice == 2)
        firstClass();
    else
        System.out.println("Invalid selection");                 
}

private void firstClass(){
    if (numFirstClass < 5){            
        for (int i = 0; i < firstClass.length; i++){                
            if(!firstClass[i]){
                firstClass[i] = true;
                System.out.println("First Class. Seat number: "+(i+1)); 
                numFirstClass++;
                break;
            }
        }
    }        
    else if (numEconomyClass < 5){
        System.out.println("No more 1st class. Press 1 if you would like to book an economy class ticket.");
        int choice = scan.nextInt();            
        if (choice == 1)
           economyClass();
        else 
            System.out.println("The next flight leaves in 3 hours.");
    }        
    else
        System.out.println("All booked, the next flight leaves in 3 hours.");        
}

private void economyClass(){
    if (numEconomyClass < 5){            
        for (int i = 0; i < economyClass.length; i++){                
            if(!economyClass[i]){
                economyClass[i] = true;
                System.out.println("Economy Class. Seat number: "+(i+6)); 
                numEconomyClass++;
                break;
            }
        }
    }        
    else if (numFirstClass < 5){
        System.out.println("No more economy. Press 1 if you would like to book an first class ticket.");
        int choice = scan.nextInt();            
        if (choice == 1)
           firstClass();
        else 
            System.out.println("The next flight leaves in 3 hours.");
    }        
    else
        System.out.println("All booked, the next flight leaves in 3 hours.");        
}    
}

Driver class

import java.util.Scanner;

public class Client {
static boolean running = true;

public static void main(String[] args) {      
    Server tickets = new Server();
    Scanner scan = new Scanner(System.in);
    while(running){
        tickets.book();
        System.out.println("Type 'Quit' to quit, else any key to continue.");
        String quit = scan.next();
        if (quit.equalsIgnoreCase("Quit"))
            running = false;
    }       
}
}

Leave a Comment