通过所有方法制定航空公司计划

时间:2015-04-22 01:23:40

标签: java

我正在自学Java,我正在通过Deitel的书来编写示例程序。

以下是练习:

一家小型航空公司刚为其新的自动预订系统购买了一台计算机。您被要求开发新系统。您将编写一份申请,在航空公司唯一的飞机的每个航班上分配座位(容量:10个座位)。

您的应用程序将显示以下备选方案:“请为First Class键入1”和“请为经济键入2”。如果用户键入1,您的应用程序将在第一类中指定一个席位(席位1-5) )。如果用户键入2,您的应用程序将在经济部分(座位6-10)分配一个席位。然后,您的申请将显示一个登机牌,表明该人的座位号以及是否在飞机的头等舱或经济舱内。

使用原始类型布尔的1维数组来表示平面的座位图。将数组的所有元素初始化为false以指示所有席位都为空。在分配每个座位时,将数组的相应元素设置为true以指示座位不再可用。

您的申请将永远不会分配已分配的席位。当经济部分已满时,您的申请将询问该人是否可以被置于第一类部分(反之亦然)。如果是,请进行适当的座位分配。如果不是,则显示消息“下一班航班将在3小时内离开。”

我无法让程序完全遍历所有参数。

这是我的代码:

 import java.util. Arrays;
 import java.util.Scanner;

 public class Exercise_619 {

     public static void main(String[] args){

         boolean [] seating = new boolean [11]; // Creates a one deminsional array
         Scanner input = new Scanner(System.in);

         System.out.println("Please type 1 for First Class or 2 for Economy: "); // Asks the user to give input

         int section = input.nextInt(); // Store user's input

         if (section == 1) // If the user inputs 1 for First class, then First Class object is called
         {
             firstClassSeat();
         }

         else // If the user inputs 2 for Economy, then Economy object is called
         {
             economySeat();
         }

     }

     public static void firstClassSeat(){

         boolean [] seating = new boolean [11]; // Creates a new array
         Scanner input = new Scanner(System.in);

         for (int count = 1; count <= 5; count++){ // Starts the loop for First Class object

         if (seating[count] == false)
         {

             seating[count] = true; // Assigns passenger a seat in first class
             System.out.printf("First Class. Seat number: %d\n", count);
             break;
         }

         else if (seating[5] == true)
         {

         if (seating[10] == true) // If the psasenger does not decide to down grade to Economy, then the program prompts the passenger when the next flight is 
         {
             System.out.println("Sorry, the flight is fully booked. Next flight leaves in 3 hours.");
         }

         else // if the passenger chooses to be down graded to Economy, then the program assigns the passenger a seat in Economy
         {
             System.out.println("First class is fully booked. Would you like a seat in Economy? Type 1 for yes or 2 for No: ");
             int choice = input.nextInt();
             if (choice == 1)
             {
                 economySeat();
             }

             else // If flight is full, then the program tells the passenger when the next flight is
             {
                 System.out.println("Next flight is in 3 hours.");
             }

         }

         }

     }

     }

     public static void economySeat(){

         boolean [] seating = new boolean [11]; // Creates a new array
         Scanner input = new Scanner(System.in);

         for (int count = 6; count <= 10; count++) // Starts the loop for Economy object

             if (seating[count] == false)
             {
                 seating[count] = true; // Assigns passenger a seat in Economy
                 System.out.printf("Economy. Seat number: %d\n", count);
                 break;
             }

             else if (seating[10]  == true)
             {
                 if (seating[5] == true) // If the paasenger does not decide to up grade to First Class, then the program prompts the passenger when the next flight is
                 {
                     System.out.println("Sorry, flight is fully booked. Next flight is in 3 hours.");
                 }

                 else // if the passenger chooses to be up graded to First Class, then the program assigns the passenger a seat in First Class
                 {
                     System.out.println("Economy is fully booked. Would you like a seat in First Class? Type 1 for Yes or 2 for No: ");
                     int choice = input.nextInt();

                     if (choice == 1)
                     {
                         firstClassSeat();
                     }

                     else
                     {
                         System.out.println("Next flight is in 3 hours."); // If flight is full, then the program tells the passenger when the next flight is
                     }

                }

             }


     }

 }

有人有任何建议或者至少向我发出正确的方向吗?

感谢。

1 个答案:

答案 0 :(得分:-1)

我知道你刚开始的感觉,所以我可以帮助你一点。您应该尝试记下下次要做的事情,这将真正帮助您思考问题。你在整个地方宣布11个座位,看起来真的很困惑。我写这段代码非常快,应该让你朝着正确的方向前进。祝你好运。

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.");        
}    
}

驱动程序类

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;
    }       
}
}