Modeling the queues at a college restaurant

Click For Summary
SUMMARY

The discussion centers on implementing a priority queue in Java to model customer queues at a college restaurant using Java Priority Queues and Maps. The user encountered a ClassCastException due to attempting to add an instance of AbstractMap.SimpleEntry to a priority queue that is designed to hold Customer objects. The proposed solution is to replace the entry with a Customer object, which resolves the type mismatch and aligns with the intended use of the priority queue.

PREREQUISITES
  • Java programming language proficiency
  • Understanding of Java Collections Framework, specifically Priority Queues
  • Familiarity with Java Comparator interface for custom sorting
  • Basic knowledge of exception handling in Java
NEXT STEPS
  • Study Java Priority Queue implementation and its use cases
  • Learn about Java Comparator and how to implement custom comparison logic
  • Explore exception handling techniques in Java to manage runtime errors
  • Investigate performance implications of using different data structures in Java
USEFUL FOR

Java developers, computer science students, and software engineers interested in data structure implementation and queue management in applications.

JessicaHelena
Messages
188
Reaction score
3
Homework Statement
Here are the constraints: The restaurant runs from 6am to 11:59 pm daily (hence opens at minute 360).

On average, a customer arrives at the restaurant every 5 minutes. (Hence 20% chance of a customer in one minute.)

The restaurant requires between 2 and 7 minutes to fill a customer order, and because there is only one person running the entire restaurant, the next customer in line will be served only after the food is served to the previous customer.

While the restaurant tries to serve everyone in the order they came in, some groups of people are given a priority. Seniors will be served before Juniors; juniors before sophomores; sophomores before freshmen.
Relevant Equations
None specifically.
So far, I have implemented the code below, using Java Priority Queues and Maps. I tried to identify each customer by the time they came in (ranging from 360 and onwards) and their grades. However, this is my first time using priority queues and maps, and I am not very sure if I'm doing things right—in fact, it returns this error below, which I'm not sure how to fix despite having consulted the APIs and some other java resources:

Exception in thread "main" java.lang.ClassCastException: java.base/java.util.AbstractMap$SimpleEntry cannot be cast to java.base/java.lang.Comparable
Below is my code:
[CODE lang="java" highlight="90"]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 McRonaldPriority
{
public static Queue<Map.Entry<Integer, Integer>> McRonald = new PriorityQueue<Map.Entry<Integer, Integer>>();
public static int waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));

public static void main(String[] args)
{
McRonaldsPriority();
}

public static void McRonaldsPriority()
{
double rand = 0.0;
boolean newCustomer = false;
for(int i = 360; i<1440; i++)
{
if(McRonald.isEmpty())
waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
if(i == 1439)
{
while(!McRonald.isEmpty())
{
waitingTime--;
if(waitingTime == 0)
{
McRonald.remove();
waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
}
System.out.println(i + ": " + McRonald);
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;
McRonald.add(new AbstractMap.SimpleEntry(grade,i));
}

if(!McRonald.isEmpty())
{
waitingTime--;
if(waitingTime == 0)
McRonald.poll();
}
if(!McRonald.isEmpty() && waitingTime == 0)
{
waitingTime = 2 + (int)(Math.random() * ((7 - 2) + 1));
}
if (i<1439)
System.out.println(i + ": " + McRonald);
}
}
}[/CODE]

I've been stuck on this for a few days, and I'd really like some help!

 
Physics news on Phys.org
This apparently is the line of code that's throwing the exception:
Java:
McRonald.add(new AbstractMap.SimpleEntry(grade,i));
Your priority queue is a collection of Customer objects, so I would see if this change works:
Java:
McRonald.add(new Customer(grade, (double)i));
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K