模拟售票系统java编程.docx
《模拟售票系统java编程.docx》由会员分享,可在线阅读,更多相关《模拟售票系统java编程.docx(22页珍藏版)》请在冰点文库上搜索。
模拟售票系统java编程
/*
项目:
用多线程设计一个模拟火车站售票大厅的工作情形。
问题描述:
火车站有许多售票窗口,有些开放,有些不开放。
顾客进入火车站售票厅后,到某个售票窗口排队等候,排到了就办理业务,然后离去。
如图2.1所示。
*/
/*
*共有五个类:
*SimulateRailwayStation:
具体运行主类;
*RailwayStation:
火车站售票大厅类
*Agent类:
代表火车站售票窗口类;
*Customer类:
顾客类;
*List类:
存储类
*/
importjava.util.Date;
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);
add("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("已经服务的顾客数:
0");
//定义可以进行售票服务的窗口
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{
if(i{
labelAgent[i]=newLabel("窗口"+(i+1)+":
空闲中...");
agentPanel.add(labelAgent[i]);
//实例化售票窗口
agent[i]=newAgent(i);
//售票窗口开始售票服务
agent[i].start();
}
else
{
labelAgent[i]=newLabel("窗口"+(i+1)+":
暂停服务!
");
agentPanel.add(labelAgent[i]);
}
}
//定义顾客候票情况面板
PanelotherPanel=newPanel();
otherPanel.setLayout(newGridLayout(2,1));
otherPanel.add(labelQueue);
otherPanel.add(labelServed);
setLayout(newBorderLayout());
//显示各售票窗口的工作状态安排在下部
add("South",agentPanel);
//显示顾客候票状况安排在中部
add("Center",otherPanel);
//显示调用本方法setup()的参数安排在上部
add("North",newLabel(title));
}
//开始工作
publicvoidstart()
{
if(thread==null)
{
thread=newThread(this);
//启动线程
thread.start();
}
}
//线程,调用显示实时售票状况的updateDisplay()方法
publicvoidrun()
{
while(true)
{
this.updateDisplay();
}
}
//实时处理售票的状况
publicvoidupdateDisplay()
{
//定义在本窗口等候的顾客数
inttotalSize=0;
//对可以服务的窗口进行循环
for(inti=0;i{
//getCIdOfHandling()方法为正在办理业务的顾客编号
if(agent[i].getCIdOfHandling()!
=0)
{
//统计在本窗口等候的顾客数
totalSize+=agent[i].getCusCountOfQueue();
Strings="窗口"+(i+1)+":
正在办理顾客"+agent[i].getCIdOfHandling()+"业务";
//显示在本窗口等候的顾客数
if(agent[i].getCusCountOfQueue()>0)
labelAgent[i].setText(s+"["+agent[i].getCusOfQueue()+"正在等待]");
else
labelAgent[i].setText(s);
}
else
{
labelAgent[i].setText("窗口"+(i+1)+":
空闲中...");
}
}
for(inti=numAgents;ilabelAgent[i].setText("窗口"+(i+1)+":
暂停服务!
");
labelQueue.setText("正在等待的顾客数:
"+totalSize);
labelServed.setText("已经服务的顾客数:
"+numCustomerServered);
}
//火车站售票窗口关闭
publicvoidstop()
{
thread=null;
for(inti=0;i{
//停止售票服务
agent[i].halt();
}
}
//添加窗口
publicvoidaddAgent()
{
if(numAgents{
agent[numAgents]=newAgent(numAgents);
agent[numAgents].start();
numAgents++;
}
}
//关闭窗口,该方法暂时没有使用
publicvoidretireAgent()
{
if(numAgents>1)
{
agent[numAgents-1].halt();
numAgents--;
}
}
//接待顾客的方法
publicvoidgenerateCustomer()
{
//所有工作窗口的队列中,至少有一个顾客在排队时为真.
booleanallAgentQueueHasOne=true;
/*如果所有正在工作窗口的队列中至少有一个顾客在排队,
就把新顾客添加到队列最少的那个队.
否则,就把顾客添加到没有业务处理的窗口中.
*/
//对可以服务的窗口进行循环
for(inti=0;i{
//如果本窗口队列中没有顾客
if(agent[i].getCusCountOfQueue()==0&&agent[i].getCIdOfHandling()==0)
{
//添加新顾客
agent[i].joinNewCustomer(newCustomer());
allAgentQueueHasOne=false;
break;
}
}
//如果所有工作窗口都有顾客在等候
if(allAgentQueueHasOne)
{
//定义变量index存放最少等候顾客数的窗口编号
intindex=0;
//对可以服务的窗口进行循环
for(inti=0;i{
if(agent[i].getCusCountOfQueue(){
(见教材)
}
}
//往最少顾客的窗口加新顾客
agent[index].joinNewCustomer(newCustomer());
}
}
}
/*火车站售票窗口类*/
classAgentextendsPanelimplementsRunnable
{
//窗口开放标志
privatebooleanrunning=false;
privateintID=-1;
privateintnumCustomers=0;
privateinthandlingCId=0;
//该窗口中排队的顾客
privateListcustomersofqueue=newList();
//该窗口中已办理的顾客
privateListcustomersofhandled=newList();
privateLabellabelHandling=newLabel();
privateLabellabelThisQueue=newLabel();
privateThreadthread=null;
//构造方法,定义售票窗口编号
publicAgent(intID)
{
(见教材)
}
//售票窗口开始售票服务
publicvoidstart()
{
if(thread==null)
{
running=true;
thread=newThread(this);
//启动线程
thread.start();
}
}
//停止售票服务
publicvoidhalt()
{
running=false;
}
//获得正在办理业务的顾客ID
publicintgetCIdOfHandling()
{
returnhandlingCId;
}
//从本窗口的队列中获得将要服务的顾客
publicCustomerrequestCustomerFor()
{
if(customersofqueue.getSize()>0)
{
Customerc=(Customer)customersofqueue.get(0);
customersofqueue.delete(0);
returnc;
}
else
{
returnnull;
}
}
//本窗口已办理业务的顾客数
publicintgetCusCountOfHandled()
{
returnnumCustomers;
}
//本窗口已办理业务的顾客列表
publicStringgetCusOfHandled()
{
if(customersofhandled.getSize()>0)
{
StringBuffersbuf=newStringBuffer();
sbuf.append("顾客");
for(inti=0;i{
sbuf.append(((Customer)customersofhandled.get(i)).getCustomerId());
if(i!
=customersofhandled.getSize()-1)
sbuf.append(",");
}
returnsbuf.toString();
}
else
{
returnnewString("");
}
}
//在窗口的队列中添加新顾客
publicsynchronizedvoidjoinNewCustomer(Customerc)
{
if(!
customersofqueue.isFull())
{
customersofqueue.add(c);
System.out.println("jointoagent"+(this.ID+1));
}
}
//获得本窗口的队列中的顾客列表
publicsynchronizedStringgetCusOfQueue()
{
if(customersofqueue.getSize()>0)
{
StringBuffersbuf=newStringBuffer();
sbuf.append("Customer");
for(inti=0;i{
sbuf.append(((Customer)customersofqueue.get(i)).getCustomerId());
if(i!
=customersofqueue.getSize()-1)
sbuf.append(",");
}
returnsbuf.toString();
}
else
{
returnnewString("");
}
}
//获得本窗口的队列中的顾客数
publicintgetCusCountOfQueue()
{
returncustomersofqueue.getSize();
}
//本窗口队列中的顾客未办理业务离去
publicvoidCustomerLeft()
{
if(customersofqueue.getSize()>0)
customersofqueue.delete(customersofqueue.getSize()-1);
}
//顾客办理完业务离去
publicvoidreleaseCustomer(Customerc)
{
numCustomers++;
customersofhandled.add(c);
}
//本窗口在不断的处理业务
publicvoidrun()
{
while(running)
{
try
{
thread.sleep((int)(Math.random()*SimulateRailwayStation.MAX_NO_CUSTOMERS)+1000);
Customercustomer=requestCustomerFor();//获得服务的顾客
if(customer!
=null)
{
handlingCId=customer.getCustomerId();//获得顾客ID
//办理业务时间:
主要是询问等
thread.sleep((int)(Math.random()*SimulateRailwayStation.BUSINESS_DELAY)/2);
synchronized(this)
{
//检索对应的票务信息
for(inti=0;i{
if(customer.getCustomerWilling()==i+1)
SimulateRailwayStation.tickets[i]--;//对应票数减一
}
}
//办理业务时间:
打印票、交钱等
thread.sleep((int)(Math.random()*SimulateRailwayStation.BUSINESS_DELAY)/2);
releaseCustomer(customer);//顾客办理后离开。
RailwayStation.numCustomerServered+=1;//服务顾客数+1
}
else
{
handlingCId=0;
}
}
catch(InterruptedExceptionie)
{
System.out.println("TellerException:
"+ie);
}
}
}
}
/*顾客类*/
classCustomer
{
//顾客开始排队的时间
privateDatecreated;
//顾客ID,每个顾客都有唯一的ID,不能重复
privatestaticintcId;
//顾客购买票务的意愿,比如去哪里,班次等.
private