import java.awt.Graphics;
public class Bank extends VisibleObject {
private long count = 0;//总客户数目
private long totaltime =0;//客户总逗留时间
private int windowNum =1;//窗口数目
private Window[] windows =null;//窗口
private Events events = new Events();//离散事件
private long opentime = 0; //银行开门时间
private long closetime = 120; //银行营业总时间,单位分钟
private Event currentEvent = null;
private Customer currentCustomer = null;
public Bank(int windowNum){
//初始化
count = 1;
totaltime =0;
//生成窗口
this.windowNum = windowNum;
windows = new Window[this.windowNum];
for(int i=0;i<windowNum;i++){
windows[i] = new Window("窗口"+(i+1));
}
//事件表清空
events.clear();
//生成一个客户到达事件
Customer customer = new Customer("A"+String.valueOf(count),opentime + 0);
Event ev = new Event(Event.EventType.arrival,customer);
events.put(ev.getOccurTime(), ev);
}
public void Run(){
while(events.size()>0){
Do();
}
}
public void Do(){
if(events.size()>0){
//取得时间上最早发生的事件
String firstKey = events.firstKey();
System.out.println("Current event:"+firstKey);
//当前事件和当前客户
currentEvent = (Event)events.get(firstKey);
currentCustomer = currentEvent.getCustomer();
//当前事件是客户到达事件的时候
if(currentEvent.getEventType().equals(Event.EventType.arrival)){
System.out.println(String.format("At Time:%s Customer:%s arrived!", currentCustomer.getArrivaltime(),currentCustomer.getName()));
long durtime = getRandomInt(8,15); //随机产生的办理业务时间
long interval = getRandomInt(1,2); //下一个客户进入银行的时间
currentCustomer.setDurTime(durtime);//设置当前客户的办理业务时间
//如果下一个客户到达的时间是在银行关门时间之前,那么生成一个客户到达事件
if(currentCustomer.getArrivaltime() + interval < closetime){
count++;
Customer customer = new Customer("A"+String.valueOf(count),currentCustomer.getArrivaltime() + interval);
Event ev = new Event(Event.EventType.arrival,customer);
events.put(ev.getOccurTime(), ev);
}
//查找人数最少的窗口,当前客户在该窗口排队
int min = getWindow4Minimal();
currentCustomer.setWindow(min);
windows[min].add(currentCustomer);
//当前客户所在的窗口的客户就是当前客户,那么确定当前客户的离开时间,并且生成客户离开事件
Customer head = (Customer)windows[min].get(0);
if(head!=null && head.equals(currentCustomer)){
currentCustomer.setLefttime(currentCustomer.getArrivaltime() + durtime);
Event ev = new Event(Event.EventType.left,currentCustomer);
events.put(ev.getOccurTime(), ev);
}
}else{//当前事件是客户离开事件的时候
System.out.println(String.format("At Time:%s, Transatcion Time %s Customer:%s left!", currentCustomer.getLefttime(),currentCustomer.getDurTime(),currentCustomer.getName()));
//计算逗留事件
totaltime+=currentCustomer.getTotalTime();
//当前客户离开他所在的窗口
int currentWindow = currentCustomer.getWindow();
windows[currentWindow].remove(currentCustomer);
//当前客户所在的窗口内还有客户的话,那么确定该窗口得第一个排队客户的离开时间,并且生成该客户离开事件
if(windows[currentWindow].size()>0){
Customer nextCustomer = (Customer)windows[currentWindow].get(0);
if(nextCustomer!=null){
nextCustomer.setLefttime(currentCustomer.getLefttime() + nextCustomer.getDurTime());
Event ev = new Event(Event.EventType.left,nextCustomer);
events.put(ev.getOccurTime(), ev);
}
}
}
//当前事件从事件表中删除
events.remove(firstKey);
}
}
private int getWindow4Minimal(){
int j=0;
int number = windows[0].size();
for(int i=1;i<this.windowNum;i++){
if(number>windows[i].size()){
number = windows[i].size();
j = i;
}
}
return j;
}
private int getRandomInt(int from, int to){
int random = from;
while(true){
double value =java.lang.Math.random()*(to+1);
random =(int)value;
if(random>=from){
return random;
}
}
}
public long getCount() {
return count;
}
public long getTotaltime() {
return totaltime;
}
public int getWindowNum() {
return windowNum;
}
public Window[] getWindows() {
return windows;
}
private Event getCurrentEvent() {
return currentEvent;
}
public void paint(Graphics g){
Event currentEvent = this.getCurrentEvent();
Customer currentCustomer = currentEvent.getCustomer();
if(currentEvent!=null){
if(currentEvent.getEventType().equals(Event.EventType.arrival)){
g.drawString(String.format("At Time:%s Customer:%s arrived!", currentCustomer.getArrivaltime(),currentCustomer.getName()),20,80);
}else{
g.drawString(String.format("At Time:%s, Transatcion Time %s Customer:%s left!", currentCustomer.getLefttime(),currentCustomer.getDurTime(),currentCustomer.getName()),20,80);
}
}
g.drawLine(20, 100, this.getWidth()-20, 100);
Window[] windows = this.getWindows();
for(int i=0;i<this.getWindowNum();i++){
windows[i].setX(20);
windows[i].setY(150+i*150);
windows[i].paint(g);
}
g.drawLine(20, this.getHeight()-50, this.getWidth()-20, this.getHeight()-50);
g.drawString(String.format("Total Time:%s, Total Customers number:%s!", this.getTotaltime(),this.getCount()),this.getWidth()-300,this.getHeight()-30);
}
}
郭永荣
- 粉丝: 3
- 资源: 5
最新资源
- YOLO算法-禾本科杂草数据集-4760张图像带标签.zip
- YOLO算法-无人机俯视视角动物数据集-10140张图像带标签-斑马-骆驼-大象-牛-羊.zip
- YOLO算法-挖掘机与火焰数据集-8129张图像带标签-挖掘机.zip
- YOLO算法-塑料数据集-3029张图像带标签-塑料制品-白色塑料.zip
- PyKDL库源码,编译安装PyKDL库
- YOLO算法-红外探测数据集-10573张图像带标签-小型车-人-无人机.zip
- 基于 C++和TCP和WebSocket的即时通信系统设计与实现(源码+文档)
- 电商管理系统项目源代码全套技术资料.zip
- 全国2022年04月高等教育自学考试02326操作系统试题及答案
- YOLO算法-垃圾数据集-3818张图像带标签-可口可乐-百事可乐.zip
- YOLO算法-瓶纸盒合并数据集-1317张图像带标签-纸张-纸箱-瓶子.zip
- YOLO算法-杂草检测项目数据集-3970张图像带标签-杂草.zip
- YOLO算法-杂草检测项目数据集-3853张图像带标签-杂草.zip
- YOLO算法-挖掘机与火焰数据集-7735张图像带标签-挖掘机.zip
- 文旅项目源代码全套技术资料.zip
- YOLO算法-罐头和瓶子数据集-1531张图像带标签-鲜奶-瓶子.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈