import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class SimulateRailwayStation extends Frame implements ActionListener
{
protected static final int NUM_AGANTS = 10;
protected static final int NUM_INITIAL_AGANTS = 6;
protected static final int BUSINESS_DELAY = 6000;
protected static final int MAX_TRAIN_NUM = 10;
protected static final int MAX_NO_CUSTOMERS = 200;
private Button addcus = new Button ("添加顾客");
private Button delcus = new Button ("顾客离开");
private Button addagent = new Button ("增加售票窗口");
private Button delagent = new Button ("关闭售票窗口");
protected static String[] train_num = {"南京——北京","南京——上海","南京——镇江",
"南京——杭州","南京——四川","南京——无锡","南京——合肥",
"南京——乌鲁木齐","南京——天津","南京——徐州"};
protected static int[] tickets = {50,30,10,20,60,20,30,10,60,41};
private RailwayStation railwaystation = new RailwayStation ();
private class WindowCloser extends WindowAdapter
{
public void windowClosing (WindowEvent we)
{
railwaystation.stop();
System.exit(0);
}
}
public SimulateRailwayStation()
{
super("Simulate RailwayStation");
Panel buttons = new Panel();
buttons.setLayout(new FlowLayout());
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(new WindowCloser());
setLayout(new BorderLayout());
add("North",railwaystation);
add("South",buttons);
setSize(500,200);
validate();
pack();
setVisible(true);
railwaystation.start();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==addcus)
{
railwaystation.generateCustomer();
}
else if(ae.getSource()==delcus)
{
}
else if(ae.getSource()==addagent)
{
railwaystation.addAgent();
}
else if(ae.getSource()==delagent)
{
railwaystation.retireAgent();
}
}
public static void main(String[] args)
{
SimulateRailwayStation smlt = new SimulateRailwayStation();
}
}
class RailwayStation extends Panel implements Runnable
{
protected Agent[] agent = new Agent[SimulateRailwayStation.NUM_AGANTS];
protected Label[] labelAgent = new Label[SimulateRailwayStation.NUM_INITIAL_AGANTS];
protected Label labelQueue = new Label("正在等待的顾客数:0");
protected Label labelServed = new Label("已经服务的顾客数:0");
protected int numAgents = SimulateRailwayStation.NUM_INITIAL_AGANTS;
public static int numCustomerServered = 0;
private Thread thread = null;
public RailwayStation()
{
setup("各窗口实时状态显示:");
}
private void setup(String title)
{
Panel agentPanel = new Panel();
agentPanel.setLayout(new GridLayout(SimulateRailwayStation.NUM_AGANTS,1));
for(int i=0;i<SimulateRailwayStation.NUM_AGANTS;i++)
{
if(i<SimulateRailwayStation.NUM_INITIAL_AGANTS)
{
labelAgent[i]=new Label("窗口"+i+":空闲...");
agentPanel.add(labelAgent[i]);
agent[i]=new Agent(i);
agent[i].start();
}
else
{
labelAgent[i]=new Label("窗口"+i+":暂停服务!");
agentPanel.add(labelAgent[i]);
}
}
Panel otherPanel = new Panel();
otherPanel.setLayout(new GridLayout(2,1));
otherPanel.add(labelQueue);
otherPanel.add(labelServed);
setLayout(new BorderLayout());
add("South",agentPanel);
add("Center",otherPanel);
add("North",new Label(title));
}
public void start()
{
if(thread==null)
{
thread = new Thread(this);
thread.start();
}
}
public void run()
{
while(true)
{
this.updateDisplay();
}
}
public void updateDisplay()
{
int totalSize = 0;
for(int i=0;i<numAgents;i++)
{
if(agent[i].getCIdOfHandling()!=0)
{
totalSize+=agent[i].getCusCountOfQueue();
String s = "窗口"+(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(int i=numAgents;i<SimulateRailwayStation.NUM_AGANTS;i++)
{
labelAgent[i].setText("窗口"+(i+1)+":暂停服务!");
}
labelQueue.setText("正在等待的顾客数:"+totalSize);
labelServed.setText("已经服务的顾客数:"+numCustomerServered);
}
public void stop()
{
thread = null;
for(int i=0;i<numAgents;i++)
{
agent[i].halt();
}
}
public void addAgent()
{
if(numAgents<SimulateRailwayStation.NUM_AGANTS)
{
agent[numAgents] = new Agent(numAgents);
agent[numAgents].start();
numAgents++;
}
}
public void retireAgent()
{
if(numAgents>1)
{
agent[numAgents-1].halt();
numAgents--;
}
}
public void generateCustomer()
{
boolean allAgentQueueHasOne = true;
for(int i=0;i<numAgents;i++)
{
if(agent[i].getCIdOfHandling()==0&&agent[i].getCusCountOfQueue()==0)
{
agent[i].joinNewCustomer(new Customer());
allAgentQueueHasOne = false;
break;
}
}
if(allAgentQueueHasOne)
{
int index = 0;
for(int i=0;i<numAgents;i++)
{
if(agent[i].getCusCountOfQueue()<agent[index].getCusCountOfQueue())
{
index=i;
}
}
agent[index].joinNewCustomer(new Customer());
}
}
}
class Agent extends Panel implements Runnable
{
private boolean running = false;
private int ID = -1;
private int numCustomers = 0;
private int handlingCId = 0;
private List customersofqueue = new List();
private List customersofhandled = new List();
private Label labelHandling = new Label();
private Label labelThisQueue = new Label();
private Thread thread = null;
public Agent(int ID)
{
this.ID=ID;
}
public void start()
{
if(thread==null)
{
running=true;
thread=new Thread(this);
thread.start();
}
}
public void halt()
{
running=false;
}
public int getCIdOfHandling()
{
return handlingCId;
}
public Customer requestCustomerFor()
{
if(customersofqueue.getSize()>0)
{
Customer c= (Customer)customersofqueue.get(0);
customersofqueue.delete(0);
return c;
}
else
{
return null;
}
}
public int getCusCountOfHandled()
{
return numCustomers
java模拟火车站售票系统源代码
4星 · 超过85%的资源 需积分: 50 50 浏览量
2009-11-10
08:08:18
上传
评论 15
收藏 3KB RAR 举报
hacker5077
- 粉丝: 6
- 资源: 26
- 1
- 2
- 3
前往页