So I have to implement a checkout line using a priority queue. I'm really stuck. I have 3 classes, a Register class, Store class, and customer class.

Each customer has a service time which is the time it takes to check out.
There are ten registers.
Reach register has a line.
I have to add a customer based on which line has the lowest total service time.

I need help on how to get the total service time for a line of a register.

Here are my three classes so far:

Store:

import java.util.LinkedList;
import java.util.PriorityQueue;


public class Store {

	private PriorityQueue<Register> regs;

	public Store(){
		this.regs = new PriorityQueue<Register>();
		
		for(int i = 0;i<=10; i++){
			regs.add(new Register());
		}
	}
	
	private Register leastFullRegister() {
		double minWaitTime = Double.MAX_VALUE;
		Register leastFullRegister = null;
		for ( Register r : regs ) {
			if ( r.getTotalEnqueue() < minWaitTime ) {
				minWaitTime = r.getTotalEnqueue();
				leastFullRegister = r;
			}
		}
		return leastFullRegister;
	}



}

Register:

import java.util.PriorityQueue;

public class Register implements Comparable<Register> {

	private boolean open; // line open or not?
	private PriorityQueue<Customer> line; //the line of customers
	private int waitTime;
	
	 Register(){
	 open = false;
	 waitTime = 0;
	 
	 this.line = new PriorityQueue<Customer>();
	}
	
	public double getTotalEnqueue(){
		double totalTime = 0;
	
		//go through every customer and get the serviceTimes and add them up 
		//This is to see which line is shorter
		
		return totalTime;
	}
	
	public void addCustomer(Customer c){
		line.add(c);
	}
	
	public Customer pollCustomer(){
		return line.poll();
	}
	
	public boolean isOpen(){
		// some how open and close lines?
		return true;
	}

	@Override
	public int compareTo(Register reg) {
		
		return 0;
	}
}

Customer

public class Customer {

	private double serviceTime;
	private int count;
	
	Customer(){
		serviceTime = Math.random() + 2;
		count = 0;
	}
	
	public double getServiceTime(){
		serviceTime = Math.random() + 2;
		count++;
		return serviceTime;
		
	}
	
	public int getCount(){
		return count;
	}
}

Recommended Answers

All 6 Replies

Huh? You already have the method to call for the total time for service in your Register class. What you need is to implement it by iterating through all of the customer in its own line, add all the customer's service time - getServiceTime() - and return the total value. The only missing part is the loop to go through the line and add up the time...

Idk why I wrote that.

I meant to say that I need help loading a customer in the line. I have totalServiceTime and another method that gets the leastFullRegister.

Not sure what you mean by "loading a client in line"??? Do you mean you could get the least service time register from leastFullRegister(), correct? If so, you need to implement the "compareTo()" method. Currently, you always return 0 which means equal. You need to actually implement it in the way that compares the value of totalServiceTime instead. Return -1, 0, or 1 for the method. Then, you could simply call "remove()" from the queue and the register you want should be the one selected by the queue.

Would this work?

public int compareTo(Register reg) {

		return (int) (this.getTotalEnqueue() - reg.getTotalEnqueue());
	}

Should be OK. :) That would be in ascending order. It may throw the precision loss error though... Not sure. Better yet...

public int compareTo(Register reg) {
  double total = this.getTotalEnqueue() - reg.getTotalEnqueue();
  if (total<0) { return -1; }
  else if (total>0) { return 1; }
  return 0;
}

So I have to take a step back. I can't use a Priority Queue for my customers. My professor said not to. I'll just use a LinkedList Queue for the customer line.

Do I still need the comparable and compareTo methods now that I'm not using the priorityQueue?


So now, I've gotten this for my registers:

import java.util.LinkedList;
import java.util.Queue;

public class Register implements Comparable<Register> {

	private boolean open; // line open or not?
	private Queue<Customer> line; //the line of customers

	
	public Register(){
		 open = true;
	 
		 line = new LinkedList<Customer>();
	}
	
	public double getTotalEnqueue(){
	
		double totalTime = 0;
	
		for(Customer cust: line ){
		totalTime += cust.getServiceTime();	
		}
		
		return totalTime;
	}
	
	public void addCustomer(Customer c){
		line.add(c);
	}
	
	public Customer pollCustomer(){
		return line.poll();
	}
	
	public boolean isOpen(){
		
		return open;
	}
	
	@Override
	public int compareTo(Register reg) {
		
		if(this.isOpen() && !reg.isOpen()){
			return 1;
		} else if (!this.isOpen() && reg.isOpen()){
			return -1;
		} else if(this.isOpen() && reg.isOpen()){
			
		
		
		if (this.lineSize() > reg.lineSize()){
			return -1;
			} else return 1;
		}
		else return 1; 
	}
	
	public int lineSize(){
		return line.size();
	}
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.