以下是限制条件: 餐厅每天营业时间为早上 6 点至晚上 11:59(因此在 360 分钟开门)。
平均每 5 分钟就有一位顾客来到餐厅。 (因此一分钟内有 20% 的机会找到顾客。)
餐厅需要 2 到 7 分钟才能完成一份顾客订单,而且由于整个餐厅只有一个人在运营,因此只有在将食物提供给前一位顾客后,才会为排队的下一位顾客提供服务。
虽然餐厅试图按照每个人进来的顺序为他们提供服务,但某些人群会被优先考虑。高年级学生将先于低年级学生接受服务;大三学生先于大二学生;大二学生先于大一学生。
到目前为止,我已经使用 Java 优先级队列和映射实现了下面的代码。我试图通过每个客户进来的时间(范围从 360 及以后)和他们的等级来识别他们。然而,这是我第一次使用优先级队列和映射,我不太确定我做的事情是否正确——事实上,它返回了下面的这个错误,尽管查阅了 API,但我不确定如何修复以及其他一些 java 资源:
Exception in thread "main" java.lang.ClassCastException: java.base/java.util.AbstractMap$SimpleEntry cannot be cast to java.base/java.lang.Comparable
import java.util.*;
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.Map;
class CustomerComparator implements Comparator<Customer>
{
public int compare(Customer c1, Customer c2)
{
if(c1.grade < c2.grade)
return 1;
else if(c1.grade > c2.grade)
return -1;
else
return 0;
}
}
class Customer
{
public int grade;
public double waitingTime;
public Customer(int grade, double waitingTime)
{
this.grade = grade;
this.waitingTime = waitingTime;
}
public int getGrade()
{
return grade;
}
public double getWaitingTime()
{
return waitingTime;
}
}
public class RestaurantPriority
{
public static Queue<Map.Entry<Integer, Integer>> Restaurant = new PriorityQueue<Map.Entry<Integer, Integer>>();
public static int waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
public static void main(String[] args)
{
RestaurantPriority();
}
public static void RestaurantPriority()
{
double rand = 0.0;
boolean newCustomer = false;
for(int i = 360; i<1440; i++)
{
if(Restaurant.isEmpty())
waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
if(i == 1439)
{
while(!Restaurant.isEmpty())
{
waitingTime--;
if(waitingTime == 0)
{
Restaurant.remove();
waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
}
System.out.println(i + ": " + Restaurant);
i++;
}
}
rand = Math.random();
if(rand >= 0.0 && rand < 0.2)
newCustomer = true;
else
newCustomer = false;
if(newCustomer)
{
int grade = 0;
double rand2 = Math.random();
if(rand >= 0.0 && rand < 0.25)
grade = 1;
else if(rand >= 0.25 && rand < 0.5)
grade = 2;
else if(rand >= 0.5 && rand <0.75)
grade = 3;
else
grade = 4;
Restaurant.add(new AbstractMap.SimpleEntry(grade,i));
}
if(!Restaurant.isEmpty())
{
waitingTime--;
if(waitingTime == 0)
Restaurant.poll();
}
if(!Restaurant.isEmpty() && waitingTime == 0)
{
waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
}
if (i<1439)
System.out.println(i + ": " + Restaurant);
}
}
}
我已经被困在这个问题上几天了,我真的很感激任何帮助。
最佳答案
public class RestaurantPriority {
public static Queue<Customer> Restaurant = new PriorityQueue<Customer>(new CustomerComparator());
public static int waitingTime = 2 + (int) (Math.random() * ((7 - 2) + 1));
public static void main(String[] args) {
RestaurantPriority();
}
public static void RestaurantPriority() {
double rand = 0.0;
boolean newCustomer = false;
for (int i = 360; i < 1440; i++) {
if (Restaurant.isEmpty()) {
waitingTime = 2 + (int) (Math.random() * ((7 - 2) + 1));
}
if (i == 1439) {
while (!Restaurant.isEmpty()) {
waitingTime--;
if (waitingTime == 0) {
Restaurant.remove();
waitingTime = 2 + (int) (Math.random() * ((7 - 2) + 1));
}
System.out.println(i + ": " + Restaurant);
i++;
}
}
rand = Math.random();
if (rand >= 0.0 && rand < 0.2) {
newCustomer = true;
} else {
newCustomer = false;
}
if (newCustomer) {
int grade = 0;
double rand2 = Math.random();
if (rand >= 0.0 && rand < 0.25) {
grade = 1;
} else if (rand >= 0.25 && rand < 0.5) {
grade = 2;
} else if (rand >= 0.5 && rand < 0.75) {
grade = 3;
} else {
grade = 4;
}
Restaurant.add(new Customer(grade, i));
}
if (!Restaurant.isEmpty()) {
waitingTime--;
if (waitingTime == 0) {
Restaurant.poll();
}
}
if (!Restaurant.isEmpty() && waitingTime == 0) {
waitingTime = 2 + (int) (Math.random() * ((7 - 2) + 1));
}
if (i < 1439) {
System.out.println(i + ": " + Restaurant);
}
}
}}
关于java - 对大学餐厅的队列进行建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60134814/