Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Efficiency! Call Center Queue

Laszlo Blanar
Laszlo Blanar
10,220 Points

Cannot pass the Call Center task of the Java Data Structures course.

Hi, Mates!

The output looks like this:

Smooooooth Operator..... Hello Bob, my name is CSR #3, how can I assist you. ... Is there anything else I can help you with?

Still, the tester says I did not call the assist method on the csr object.

What can be the issue?

Thanks!

Regards, Laci

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 (true) {
      do {
        playHoldMusic();
      } while (mSupportReps.poll() == null);

    /********************************************
     * TODO (2) 
     * After we have assigned the rep, call the 
     * assist method and pass in the customer
     ********************************************
     */
      if (mSupportReps.poll() != null) {
        csr = mSupportReps.poll();
        break;
      } else {
        continue;
      }
     }
    csr.assist(customer);

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


  }

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

}

2 Answers

Kourosh Raeen
Kourosh Raeen
23,732 Points

You have nested loops and that's not necessary. You just need a while loop for playing hold music until a rep because available, using the peek method to see if the queue is empty or not. Once the queue is not empty the loop ends and we can get the available rep out of the queue using the poll method and assign it to the csr variable. Then as the instructions say we call the assist method on csr, passing in the customer. Finally, we use the add method to add the rep back to the queue.

Here's the code:

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) {
        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.....");
  }

}
Laszlo Blanar
Laszlo Blanar
10,220 Points

Thank you very much Kourosh!

You are right, the nested loop still freaked out the test script.

However, it was something I added to the code trying around variations to pass.

The original problem was that I used a poll method instead of a peek method in the while loop.

I just see that someone else had the same Issue 5 months ago.

Thanks again for your help.

Dustin Bryce Flanary
Dustin Bryce Flanary
17,663 Points

Thanks for that explanation Kourash. It was super helpful in understanding the problem and the solution and the use of peek() in a queue.

That was a lot easier than what I had. I tend to overthink it.

Kourosh Raeen
Kourosh Raeen
23,732 Points

Laszlo Blanar and Dustin Flanary - My pleasure! I'm glad the explanation was helpful. Happy coding!