使用java语言模拟银行双服务台排队系统
时间: 2023-10-08 10:11:35 浏览: 39
好的,这是一个比较复杂的系统,需要考虑很多因素,包括顾客到达时间、服务时间、服务台数量、排队策略等等。下面是一个简单的模拟程序,帮助你了解如何实现。
首先,定义一个顾客类,记录到达时间和服务时间:
```java
class Customer {
private int arriveTime; // 到达时间
private int serviceTime; // 服务时间
public Customer(int arriveTime, int serviceTime) {
this.arriveTime = arriveTime;
this.serviceTime = serviceTime;
}
public int getArriveTime() {
return arriveTime;
}
public int getServiceTime() {
return serviceTime;
}
}
```
然后,定义一个服务台类,记录服务状态(是否空闲)和服务时间:
```java
class ServiceDesk {
private boolean free; // 是否空闲
private int serviceTime; // 服务时间
public ServiceDesk() {
this.free = true;
this.serviceTime = 0;
}
public boolean isFree() {
return free;
}
public void setFree(boolean free) {
this.free = free;
}
public int getServiceTime() {
return serviceTime;
}
public void setServiceTime(int serviceTime) {
this.serviceTime = serviceTime;
}
public void doService() {
if (!free) {
serviceTime--;
if (serviceTime == 0) {
free = true;
}
}
}
}
```
接下来,实现排队系统的主要逻辑。我们可以使用一个队列来存储排队的顾客,使用一个计时器来模拟时间流逝,使用一个数组来存储服务台的状态。具体实现如下:
```java
import java.util.LinkedList;
import java.util.Queue;
public class BankQueueSystem {
private static final int MAX_WAIT_TIME = 20; // 最大等待时间
private static final int MAX_SERVICE_TIME = 10; // 最大服务时间
private static final int CUSTOMER_INTERVAL = 4; // 顾客到达时间间隔
private static final int TOTAL_TIME = 120; // 总时间
public static void main(String[] args) {
Queue<Customer> queue = new LinkedList<>(); // 排队队列
ServiceDesk[] desks = new ServiceDesk[2]; // 两个服务台
for (int i = 0; i < desks.length; i++) {
desks[i] = new ServiceDesk();
}
int currentTime = 0; // 当前时间
int lastArriveTime = 0; // 上一个顾客到达的时间
int customerCount = 0; // 顾客总数
int waitCount = 0; // 等待的顾客数
int totalWaitTime = 0; // 总等待时间
while (currentTime < TOTAL_TIME || !queue.isEmpty()) {
// 处理到达的顾客
if (currentTime < TOTAL_TIME && currentTime - lastArriveTime >= CUSTOMER_INTERVAL) {
int serviceTime = (int) (Math.random() * MAX_SERVICE_TIME) + 1;
Customer customer = new Customer(currentTime, serviceTime);
queue.offer(customer);
System.out.println("顾客 " + customerCount + " 到达,服务时间为 " + serviceTime);
lastArriveTime = currentTime;
customerCount++;
}
// 处理服务台
for (int i = 0; i < desks.length; i++) {
desks[i].doService();
}
// 分配服务台
while (!queue.isEmpty() && waitCount < 2) {
Customer customer = queue.peek();
int minIndex = findMinIndex(desks);
if (minIndex != -1) {
queue.poll();
desks[minIndex].setFree(false);
desks[minIndex].setServiceTime(customer.getServiceTime());
int waitTime = currentTime - customer.getArriveTime();
totalWaitTime += waitTime;
waitCount++;
System.out.println("顾客 " + (customerCount - queue.size() - 1) + " 开始在服务台 " + (minIndex + 1) + " 接受服务,等待时间为 " + waitTime);
} else {
break;
}
}
// 更新计时器和等待人数
if (!queue.isEmpty()) {
waitCount = queue.size();
} else {
waitCount = 0;
}
currentTime++;
}
System.out.println("顾客总数:" + customerCount);
System.out.println("平均等待时间:" + (totalWaitTime / customerCount));
}
private static int findMinIndex(ServiceDesk[] desks) {
int minIndex = -1;
int minServiceTime = Integer.MAX_VALUE;
for (int i = 0; i < desks.length; i++) {
if (desks[i].isFree()) {
return i;
} else {
if (desks[i].getServiceTime() < minServiceTime) {
minIndex = i;
minServiceTime = desks[i].getServiceTime();
}
}
}
return minIndex;
}
}
```
上面的程序中,我们使用一个计时器来模拟时间流逝。在每个时间步骤中,我们先处理顾客的到达,然后处理服务台的状态,最后根据排队策略分配服务台。每个顾客到达后,我们将其加入排队队列中,并输出相关信息。每个服务台在服务时,会递减服务时间,直到服务完成后才会变为空闲状态。根据排队策略,我们将等待时间最短的顾客分配给空闲的服务台。
在模拟结束后,我们输出顾客总数和平均等待时间,以评估排队系统的性能。