📄 supermarket.java
字号:
/*
* 创建日期 2005-2-26
*
* TODO 要更改此生成的文件的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package simulator;
import java.awt.*;
import java.awt.event.*;
public class Supermarket extends Business {
/**
*
* @uml.property name="queue"
* @uml.associationEnd multiplicity="(0 -1)"
*/
private Queue[] queue = new Queue[Simulator.NUM_AGENTS];
//声明一个等待队列数组;
//每个代理都有一个顾客等待队列
public Supermarket() {
super("The Supermarket"); //调用Business类的构造方法
//该构造方法创建一个面板
//面板上通过多个标签显示各种信息
//并创建多个服务代理
for (int i = 0; i < queue.length; i++)
queue[i] = new Queue(); //每个代理都分配一个顾客等待队列对象
}
public void updateDisplay() {
labelServed.setText("Customers served: " + numCustomer);
//显示有多少个顾客已经服务
if (numCustomer != 0)
labelWait.setText("Average wait: " + (totalWait / numCustomer));
//显示顾客的平均等待时间
int totalSize = 0;
for (int i = 0; i < numAgents; i++) {
totalSize += queue[i].getSize(); //累计所有的等待顾客数
String s = "Agent " + i + ": served " + agent[i].getHandled();
labelAgent[i].setText(s + " [" + queue[i].getSize() + " waiting]");
//显示第I个代理已接待的顾客数,同时还显示有多少个
//顾客在等待,这个方法在这一点上不同于Bank类
}
for (int i = numAgents; i < Simulator.NUM_AGENTS; i++)
labelAgent[i].setText("Agent " + i + ": inactive");
//显示第numAgents个以后的代理没有接待顾客
labelQueue.setText("Customers waiting: " + totalSize);
//显示所有的顾客队列总共还有多少顾客在等待
}
public void generateCustomer() {
int index = 0;
//在numAgents个队列中选出等待顾客最少的那个队列
for (int i = 0; i < numAgents; i++)
if (queue[i].getSize() < queue[index].getSize())
index = i;
//index为等待顾客最少的那个队列
queue[index].insert(new Customer()); //在该队列中插入一个顾客
}
public Customer requestCustomerFor(int ID) { //从第ID顾客队列中取出一个顾客
return queue[ID].requestCustomer();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -