简单航班订票模拟系统(Airline Reservation Sysytem, a simple simulation)

// Airline Reservation System, a simple simulation
// Java how to program, 5/e, Exercise 7.18
import  javax.swing. * ;
import  java.awt. * ;
import  java.awt.event. * ;
public   class  AirlineReservation  extends  JApplet  implements  ActionListener  {
 
int seat[]=new int[10];
 
boolean seatStatus[]=new boolean[10];
 
int seatNumber,classType;
 JLabel classTypeLabel;
 JTextField classTypeField;
 JTextArea output;
 
int FirstClassCounter=3,EconomyCounter=7;
 
public void init()
 
{
     
     Container  container
=getContentPane();
     container.setLayout(
new FlowLayout());
     
     classTypeLabel
= new JLabel("Please type 1 for First Class and 2 for Economy:");
     container.add(classTypeLabel);
     
     classTypeField
=new JTextField(10);
     container.add(classTypeField);
     
     classTypeField.addActionListener(
this);
     
     output
=new JTextArea();
     container.add(output);
     
     
for (int i=0;i<seat.length;i++)
         seat[i]
=i+1;

 }

 
 
public void actionPerformed (ActionEvent event)
 
{
     
     classType
=Integer.parseInt(classTypeField.getText());
     
if (classType==1&&FirstClassCounter>0)
     
{
        
for (int i=0;i<3;i++)
        
{
            
if (seatStatus[i]!=true)
            
{
                seatNumber
=seat[i];
                seatStatus[i]
=true;
                FirstClassCounter
--;
                output.append(
"Thanks! Your have reserved a First Class seat and got a boarding pass."
                        
+"/nYour seat number is: "+seatNumber+"/n");
                
break;
            }

            
        }

        
     }

     
else if(classType==1&&FirstClassCounter<=0&&EconomyCounter>0)
     
{
         output.append(
"Sorry, all the First Class seats have been reserved./n"+
                 
"You can type 2 to reserve an Economy one instead! /n");
     }

     
else if (classType==2&&EconomyCounter>0)
     
{
         
for (int i=3;i<10;i++)
            
{
                
if (seatStatus[i]!=true)
                
{
                    seatNumber
=seat[i];
                    seatStatus[i]
=true;
                    EconomyCounter
--;
                    output.append(
"Thanks! Your have reserved an Economy seat and got a boarding pass."
                            
+"/n. Your seat number is: "+seatNumber+"/n");
                    
break;
                }

            }

            
     }

     
else if(classType==2&&EconomyCounter<=0&&FirstClassCounter>0)
     
{
         output.append(
"Sorry, all the Economy seats reserved. "+
                 
"/nYou can type 1 to reserve an First Class one instead!/n ");
     }

     
else if(FirstClassCounter<=0&&EconomyCounter<=0)
    
{
         output.append(
"Sorry, all the seats have been reserved. "+
                 
"/nNext flight leaves in 3 hours./n");
     }

    

 }

 
 }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值