学以致用——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 pandenghuang@163.com
/**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("对不起,本次航班所有舱位已满,请查询其它航班。");
	 }
	 
 }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值