Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Efficiency! Call Center Queue

Matthew Alexander
Matthew Alexander
14,000 Points

Can you help me? I don't understand the task at hand.

This is a Call Center application. There is a group of Customer Support Representatives that assist Customers. When representatives become available they enter the queue. The acceptCustomer method gets called when a new customer phones in. I've left 3 things for you todo in the comments below.

All other code is only for your reference, no need to change it

CallCenter.java
import java.util.ArrayDeque;
import java.util.Queue;

public class CallCenter {
  Queue<CustomerSupportRep> mSupportReps;

  public CallCenter(Queue<CustomerSupportRep> queue) {
    mSupportReps = queue;
  }

  public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    /********************************************
     * TODO (1) 
     * Wait until there is an available rep in the queue.
     * While there is not one available, playHoldMusic
     * HINT: That while assignmentcheck loop syntax we used to 
      *      read files seems pretty similar
     ********************************************
     */
    while (

    /********************************************
     * TODO (2) 
     * After we have assigned the rep, call the 
     * assist method and pass in the customer
     ********************************************
     */

    /********************************************
     * TODO (3) 
     * Since the customer support rep is done with
     * assisting, put them back into the queue.
     ********************************************
     */

  }

  public void playHoldMusic() {
    System.out.println("Smooooooth Operator.....");
  }

}
CustomerSupportRep.java
import java.util.List;
import java.util.ArrayList;

public class CustomerSupportRep {
  private String mName;
  private List<Customer> mAssistedCustomers;

  public CustomerSupportRep(String name) {
    mName = name;
    mAssistedCustomers = new ArrayList<Customer>();
  }

  public void assist(Customer customer) {
    System.out.printf("Hello %s, my name is %s, how can I assist you.%n",
                      customer.getName(),
                      mName);
    System.out.println("...");
    System.out.println("Is there anything else I can help you with?");
    mAssistedCustomers.add(customer);
  }

  public List<Customer> getAssistedCustomers() {
    return mAssistedCustomers;
  }

}
Customer.java
public class Customer {
  private String mName;

  public Customer(String name) {
    mName = name;
  }

  public String getName() {
    return mName;
  }

}

3 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Matt,

This is a tricky challenge at first look but it actually is quit simple once you understand what it's asking you to do. I'll break it down in a easy way. So let's start with the first step:

(1) So while we have no customer support reps in our queue, we want music to play. We can use a while loop for this easily. Use a while loop to check if our queue has nothing in it. Check the java docs on queues. It only has 6 methods and you'll be able to see what you gotta use. And then inside that while loop, just call the music method! Ill let you try this first and you can look at my answer at the bottom if your still confused.

(2) Ok onto the second step. So for this step, we have finally got our customer rep as it passed that while loop. So lets take the first person out of the queue. Do you remember which method you gotta use? Once we pull the person out of the queue, let's assign to this variable that is just chillin at the top of the method. It looks like this:

CustomerSupportRep csr;

so take the person from the queue and assign it to that. Now we can reference our customer support easily by just typing "csr". So once we got our "csr", lets call the method that does the assisting and pass in the customer.

(3) Lastly all we do here is pop that "csr" back into the queue. I think you can do that easily

Back to the first, In case you dont know which methods to do, I will give you a hint. We can use two. Either peek() or poll(). Peek checks if there's anything in the queue and poll tries to take the next thing out of the queue. If there's nothing inside, it'll return null.

For the second to pull out a person of the queue, we use the poll() method which takes the next thing out of the queue. This returns null if theres nothing inside and that's why you could use it above too.

If your still stumped, I've put my answer at the botton.

Answer.java
public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    /********************************************
     * TODO (1) 
     * Wait until there is an available rep in the queue.
     * While there is not one available, playHoldMusic
     * HINT: That while assignmentcheck loop syntax we used to 
      *      read files seems pretty similar
     ********************************************
     */

    while  (mSupportReps.peek() == null) { // or we can use .poll() as they both return null if theres nothing in there
      playHoldMusic();
    }

    /********************************************
     * TODO (2) 
     * After we have assigned the rep, call the 
     * assist method and pass in the customer
     ********************************************
     */

    csr = mSupportReps.poll();
    csr.assist(customer);

    /********************************************
     * TODO (3) 
     * Since the customer support rep is done with
     * assisting, put them back into the queue.
     ********************************************
     */

    mSupportReps.add(csr);

  }

I hope this solved all your questions

Happy Coding,

Kevin

Matthew Alexander
Matthew Alexander
14,000 Points

THANK YOU SO MUCH. YOU HAVE BEEN A GREAT HELP.

Ebenezer Nkrumah
Ebenezer Nkrumah
1,469 Points

That was an amazing explanation. Thank you for walking through the process first instead of flat out giving the answer. Cheers!!!!!

Sena Sari
Sena Sari
1,462 Points

fyi if you use poll instead of peek (like me) it doesn't compile. Thank you Kevin :)

Spoiler Alert!

import java.util.ArrayDeque;
import java.util.Queue;

public class CallCenter {
  Queue<CustomerSupportRep> mSupportReps;

  public CallCenter(Queue<CustomerSupportRep> queue) {
    mSupportReps = queue;
  }

public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    /********************************************
     * TODO (1) 
     * Wait until there is an available rep in the queue.
     * While there is not one available, playHoldMusic
     * HINT: That while assignmentcheck loop syntax we used to 
      *      read files seems pretty similar
     ********************************************
     */

    while  (mSupportReps.peek() == null) { // or we can use .poll() as they both return null if theres nothing in there
      playHoldMusic();
    }

    /********************************************
     * TODO (2) 
     * After we have assigned the rep, call the 
     * assist method and pass in the customer
     ********************************************
     */

    csr = mSupportReps.poll();
    csr.assist(customer);

    /********************************************
     * TODO (3) 
     * Since the customer support rep is done with
     * assisting, put them back into the queue.
     ********************************************
     */

    mSupportReps.add(csr);

  }

  public void playHoldMusic() {
    System.out.println("Smooooooth Operator.....");
  }

}
Mike Nedelko
Mike Nedelko
8,991 Points

Hi Kevin. So I have been working on the aforementioned issue, and can't see how my code is different from the above. Would you be happy to point me in the right direction?

import java.util.ArrayDeque;
import java.util.Queue;

public class CallCenter {
  Queue<CustomerSupportRep> mSupportReps;

  public CallCenter(Queue<CustomerSupportRep> queue) {
    mSupportReps = queue;
  }

  public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    csr = mSupportReps.poll();
    while(csr == null){
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(csr);
  }

  public void playHoldMusic() {
    System.out.println("Smooooooth Operator.....");
  }

}

I am receiving the following error:

Hello Bob, my name is Canary, how can I assist you.
...
Is there anything else I can help you with?
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3236)
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
    at java.io.PrintStream.write(PrintStream.java:480)
    at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
    at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
    at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
    at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
    at java.io.PrintStream.write(PrintStream.java:527)
    at java.io.PrintStream.print(PrintStream.java:669)
    at java.io.PrintStream.println(PrintStream.java:806)
    at CallCenter.playHoldMusic(CallCenter.java:22)
    at CallCenter.acceptCustomer(CallCenter.java:15)
    at JavaTester.run(JavaTester.java:110)
    at JavaTester.main(JavaTester.java:41)
Adriano Martinelli Ianase
Adriano Martinelli Ianase
9,621 Points

Hey Mike, how are you?

This problem occur because you called mSupportReps.poll() before the while loop. Let's assume that there is no rep in the queue, so your while loop will keep running because in your code csr will always be null. Try something like this:

while( (csr = mSupportReps.poll()) == null){
      playHoldMusic();
}

This loop will keep trying to grab a csr out of the queue. If it gets nothing (null) it will call the playHoldMusic and then try again until it assigns a rep to the csr variable.

Best regards, Adriano

Mehmet Arabaci
Mehmet Arabaci
4,432 Points

I got the same code. It seems right but doesn't pass. Anyone care to comment why? Is it because we never seem to define what type of queue this is i.e. an ArrayDeque