• Support PF! Buy your school textbooks, materials and every day products Here!

Modeling the queues at a college restaurant

  • #1

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:
Java:
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);
      }
   }
}
I've been stuck on this for a few days, and I'd really like some help!

 

Answers and Replies

  • #2
33,631
5,288
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));
 

Related Threads on Modeling the queues at a college restaurant

Replies
6
Views
2K
Replies
1
Views
703
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
986
Replies
9
Views
947
Replies
2
Views
3K
Replies
3
Views
1K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
3
Views
2K
Top