Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Priority Queue help

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Priority Queue help

    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;
    	}
    }
    Last edited by BuhRock; October 30th, 2011 at 10:09 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Priority Queue help

    For each Register, iterate over the Customers and sum the service times.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Priority Queue help

    Would this work?

    	double totalTime = 0;
     
    		for(Customer cust: line ){
    		totalTime +=cust.getServiceTime();
     
    		}

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Priority Queue help

    Did you try it?
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Priority Queue help

    Yes, I suppose it works, it printed out the double that the customer has when its created.. I made a class and added a customer to a register. I just need help on how to add customers to different registers depending on which register has the shortest time. I had the methods created for that in the store class, but don't know how to implement.

    Store store = new Store();
    		Register reg = new Register();
    		Customer customer = new Customer();
     
    		reg.addCustomer(customer);
     
    		System.out.println(reg.getTotalEnqueue());

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Priority Queue help

    I said you would have to do it for all Registers. Each time you calculate the time compare it to the previous total time. If it is less then this Register has the shortest time.
    for each Register {
        calculate time
        if time is less than previous shortest time
            update shortest time
            update shortest Register
        }
    }
    shortest Register now holds the Register with the shortest time.
    Improving the world one idiot at a time!

  7. #7
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Priority Queue help

    I guess what I'm confused on, is how to instantiate the registers. The constructor of my store class creates ten registers. So in my temporary tester class, do I even need to do this:

    Register reg = new Register();

    or do I just need to do Store store = new Store();

  8. #8
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Priority Queue help

    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();
    	}
     
    }

Similar Threads

  1. generate priority queue from hashmap help
    By Scotty33 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 23rd, 2011, 09:32 PM
  2. Implementing a priority queue using a max heap
    By jsinclair1482 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:56 AM
  3. Priority Queue using comparable
    By jkalm in forum Collections and Generics
    Replies: 6
    Last Post: December 5th, 2010, 10:02 PM
  4. Replies: 4
    Last Post: July 21st, 2010, 04:07 PM