学以致用——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;i0) {  //先检查头等舱受否有空座
		 for (int i=0;i 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 0)  //如果头等舱已满,检查经济舱是否有座
	         {
	         System.out.printf("对不起,经济舱已满,如需预定头等舱可输入1:");
				seatType =input.nextInt();
				if (seatType==1)
					bookFirstClass();	//预定头等舱
	         
	    }
	    else if (economicAvailable ==0 && firstClassAvailable ==0) 
	    	System.out.println("对不起,本次航班所有舱位已满,请查询其它航班。");
	 }
	 
 }

 

你可能感兴趣的:(Java编程(Java,Programming))