1. 程式人生 > >學以致用——Java原始碼——機票預定小程式(Airline Reservation)

學以致用——Java原始碼——機票預定小程式(Airline Reservation)

記得2008年這個程式沒有完成,十年後的今天補上。改進如下:

1. 分拆出預定頭等艙和預定經濟艙兩個方法,程式邏輯更加簡單清晰

2. 消除了原來程式的bug 

這個程式是在原來的applet版本上修改而成的(大改)。程式設計途中遭遇了思路理不清的問題。然後,拿出筆記本畫了個流程圖,思路頓時清晰了。如果從零開始可能更簡單。這也說明了維護專案其實不一定比開發專案簡單!

參考文章:

簡單航班訂票模擬系統(Airline Reservation Sysytem, a simple simulation),https://blog.csdn.net/hpdlzu80100/article/details/2297918

執行結果:

航班預定小程式:

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:4

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):1

您已成功預定了頭等艙,艙位型別:頭等艙,座位號:1

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:5

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:6

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:

7

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:8

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:9

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

您已成功預定了經濟艙,艙位型別:經濟艙,座位號:10

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

對不起,經濟艙已滿,如需預定頭等艙可輸入11

您已成功預定了頭等艙,艙位型別:頭等艙,座位號:2

頭等艙預訂請輸入1 經濟艙預定請輸入2(輸入-1退出):2

對不起,經濟艙已滿,如需預定頭等艙可輸入

11

您已成功預定了頭等艙,艙位型別:頭等艙,座位號:3

 

程式碼如下:

import java.util.Scanner;

//JHTP Exercise 7.12: Duplicate Elimination
//by [email protected]
/**7.19 (Airline Reservations System) A small airline has just purchased a computer for its new automated
reservations system. You’ve been asked to develop the new system. You’re to write an application to 
assign seats on each flight of the airline’s only plane (capacity: 10 seats).
Your application should display the following alternatives: Please type 1 for First Class and Please type 2 
for Economy. If the user types 1, your application should assign a seat in the firstclass section (seats 1–5). 
If the user types 2, your application should assign a seat in the economy section (seats 6–10). 
Your application should then display a boarding pass indicating the person’s seat number and whether 
it’s in the first-class or economy section of the plane.
Use a one-dimensional array of primitive type boolean to represent the seating chart of the plane. 
Initialize all the elements of the array to false to indicate that all the seats are empty. 
As each seat is assigned, set the corresponding element of the array to true to indicate that the 
seat is no longer available.
Your application should never assign a seat that has already been assigned. 
When the economy section is full, your application should ask the person if it’s acceptable 
to be placed in the first-class section (and vice versa). If yes, make the appropriate seat assignment. 
If no, display the message "Next flight leaves in 3 hours."*/


public class AirlineReservation {

	 static final int FIRSTCLASSCAPACITY = 3,ECONOMICCAPACITY = 7; //頭等艙,經濟艙座位數分配
	 static final int FIRSTCLASS = 1;  //1為頭等艙
	 static final int ECONOMIC = 2;  // 2為經濟艙
	 static int[] seatNum=new int[FIRSTCLASSCAPACITY+ECONOMICCAPACITY];  //座位編號
	 static boolean[] seatStatus=new boolean[FIRSTCLASSCAPACITY+ECONOMICCAPACITY];  //true表示已預定
	 static int firstClassAvailable = FIRSTCLASSCAPACITY;  //可預訂的頭等艙座位數
	 static int economicAvailable = ECONOMICCAPACITY;      //可預訂的經濟艙座位數
     static int seatType= 0; //使用者選擇的艙位型別
     static int seatNumber = 0; //系統為使用者指定的座位號
	 static Scanner input=new Scanner(System.in);

 public static void main(String[] args)
 {

	 for (int i=0;i<seatNum.length;i++)  //分配座位號
         seatNum[i]=i+1;  
     
     System.out.println("航班預定小程式:");
     

		
		do{
			System.out.print("頭等艙預訂請輸入1, 經濟艙預定請輸入2(輸入-1退出):");
			seatType =input.nextInt();
			if(seatType ==-1){
				System.out.printf("已退出程式");
				break;
			}
			
			if (seatType == FIRSTCLASS)
			bookFirstClass();
			else if (seatType == ECONOMIC)
			bookEconomic();

				
			} while (seatType != -1 && !(economicAvailable ==0 && firstClassAvailable ==0));
		
		input.close();
 }
    
 public static void bookFirstClass() {
	 
	if (firstClassAvailable >0) {  //先檢查頭等艙受否有空座
		 for (int i=0;i<FIRSTCLASSCAPACITY;i++)
	     {
	         if (seatStatus[i]!=true)
	         {
	             seatNumber=seatNum[i];
	             seatStatus[i]=true;
	             firstClassAvailable--;
	             System.out.printf("您已成功預定了頭等艙,艙位型別:頭等艙,座位號:%d%n", seatNumber);
	             break;
	         }
	     }
	}
    else if(economicAvailable > 0)  //如果頭等艙已滿,檢查經濟艙是否有座
         {
         System.out.printf("對不起,頭等艙已滿,如需預定經濟艙可輸入2:");
         seatType =input.nextInt();	
			if (seatType==2)
				bookEconomic();	//預定經濟艙
         
    }
    else if  (economicAvailable ==0 && firstClassAvailable ==0)
    	System.out.println("對不起,本次航班所有艙位已滿,請查詢其它航班。");
 }
 
 
 public static void bookEconomic() {
	 
		if (economicAvailable > 0) {  //先檢查經濟艙受否有空座
			 for (int i=FIRSTCLASSCAPACITY;i<FIRSTCLASSCAPACITY+ECONOMICCAPACITY;i++)
		     {
		         if (seatStatus[i]!=true)
		         {
		             seatNumber=seatNum[i];
		             seatStatus[i]=true;
		             economicAvailable--;
		             System.out.printf("您已成功預定了經濟艙,艙位型別:經濟艙,座位號:%d%n", seatNumber);
		             break;
		         }
		     }
		}
	    else if(firstClassAvailable > 0)  //如果頭等艙已滿,檢查經濟艙是否有座
	         {
	         System.out.printf("對不起,經濟艙已滿,如需預定頭等艙可輸入1:");
				seatType =input.nextInt();
				if (seatType==1)
					bookFirstClass();	//預定頭等艙
	         
	    }
	    else if (economicAvailable ==0 && firstClassAvailable ==0) 
	    	System.out.println("對不起,本次航班所有艙位已滿,請查詢其它航班。");
	 }
	 
 }