模拟售票系统java编程Word文档下载推荐.doc
《模拟售票系统java编程Word文档下载推荐.doc》由会员分享,可在线阅读,更多相关《模拟售票系统java编程Word文档下载推荐.doc(14页珍藏版)》请在冰豆网上搜索。
importjava.awt.*;
importjava.awt.event.*;
publicclassSimulateRailwayStationextendsFrameimplementsActionListener
{
//预设火车站售票大厅有10个售票窗口
protectedstaticfinalintNUM_AGANTS=10;
//预设目前正在售票的窗口为6个
protectedstaticfinalintNUM_INITIAL_AGANTS=6;
//设置每个窗口办理售票业务的时间
protectedstaticfinalintBUSINESS_DELAY=6000;
//设置有10辆火车的座位可以出售
protectedstaticfinalintMAX_TRAIN_NUM=10;
//设置每个窗口从一个顾客完成到下一个顾客开始的时间间隔
protectedstaticfinalintMAX_NO_CUSTOMERS=200;
//定义按钮,手动添加顾客。
privateButtonaddcus=newButton("
添加顾客"
);
//定义按钮,模拟顾客自己离开
privateButtondelcus=newButton("
顾客离去"
//定义按钮,增加售票窗口
privateButtonaddagent=newButton("
增加售票窗口"
//定义按钮,关闭售票窗口
privateButtondelagent=newButton("
关闭售票窗口"
//10辆火车班次的信息
protectedstaticString[]train_num={"
南京->
北京,46次"
"
上海,34次"
福州,231次"
杭州,65次"
武汉,112次"
成都,77次"
天津,21次"
徐州,134次"
乌鲁目齐,335次"
合肥,456次"
};
//与上面的信息对应的每辆火车的票务信息
protectedstaticint[]tickets={50,70,50,50,50,120,60,100,50,50};
//实例化火车站售票大厅类
privateRailwayStationrailwaystation=newRailwayStation();
//建立窗体适配器,能关闭窗口
privateclassWindowCloserextendsWindowAdapter
{
publicvoidwindowClosing(WindowEventwe)
{
railwaystation.stop();
System.exit(0);
}
}
//构造方法,完成界面初始化
publicSimulateRailwayStation()
super("
SimulationRailwayStation"
//设置面板
Panelbuttons=newPanel();
buttons.setLayout(newFlowLayout());
//在面板中添加按钮
buttons.add(addcus);
buttons.add(delcus);
buttons.add(addagent);
buttons.add(delagent);
//对按钮设置监听
addcus.addActionListener(this);
delcus.addActionListener(this);
addagent.addActionListener(this);
delagent.addActionListener(this);
//对窗体适配器设置监听
addWindowListener(newWindowCloser());
setLayout(newBorderLayout());
add("
North"
railwaystation);
South"
buttons);
setSize(500,200);
validate();
pack();
show();
//调用火车站售票大厅类的start()方法,开始售票工作
railwaystation.start();
}
publicvoidactionPerformed(ActionEventae)
if(ae.getSource()==addcus)
{
//新增顾客
railwaystation.generateCustomer();
elseif(ae.getSource()==delcus)
elseif(ae.getSource()==addagent)
//增加售票窗口
railwaystation.addAgent();
elseif(ae.getSource()==delagent)
//关闭服务窗口
railwaystation.retireAgent();
publicstaticvoidmain(String[]args)
{
SimulateRailwayStationsmlt=newSimulateRailwayStation();
}
}
/*火车站售票大厅类*/
classRailwayStationextendsPanelimplementsRunnable
//定义售票窗口数组Agent[]
protectedAgent[]agent=newAgent[SimulateRailwayStation.NUM_AGANTS];
protectedLabel[]labelAgent=newLabel[SimulateRailwayStation.NUM_AGANTS];
protectedLabellabelQueue=newLabel("
正在等待的顾客数:
0"
protectedLabellabelServed=newLabel("
已经服务的顾客数:
//定义可以进行售票服务的窗口
protectedintnumAgents=SimulateRailwayStation.NUM_INITIAL_AGANTS;
//定义存放已服务过的顾客数
publicstaticintnumCustomerServered=0;
privateThreadthread=null;
publicRailwayStation()
{
setup("
各窗口实时状态显示:
"
}
//显示各售票窗口的实时工作状态
privatevoidsetup(Stringtitle)
//定义售票窗口的工作状态面板
PanelagentPanel=newPanel();
agentPanel.setLayout(newGridLayout(SimulateRailwayStation.NUM_AGANTS,1));
//各售票窗口的工作状态
for(inti=0;
i<
SimulateRailwayStation.NUM_AGANTS;
i++)
if(i<
numAgents)
labelAgent[i]=newLabel("
窗口"
+(i+1)+"
:
空闲中..."
agentPanel.add(labelAgent[i]);
//实例化售票窗口
agent[i]=newAgent(i);
//售票窗口开始售票服务
agent[i].start();
else
暂停服务!
}
//定义顾客候票情况面板
PanelotherPanel=newPanel();
otherPanel.setLayout(newGridLayout(2,1));
otherPanel.add(labelQueue);
otherPanel.add(labelServed);
setLayout(newBorderLayout());
//显示各售票窗口的工作状态安排在下部
add("
agentPanel);
//显示顾客候票状况安排在中部
Center"
otherPanel);
//显示调用本方法setup()的参数安排在上部
newLabel(title));
//开始工作
publicvoidstart()
if(thread==null)
thread=newThread(this);
//启动线程
thread.start();
}
//线程,调用显示实时售票状况的updateDisplay()方法
publicvoidrun()
while(true)
this.updateDisplay();
//实时处理售票的状况
publicvoidupdateDisplay()
//定义在本窗口等候的顾客数
int