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

Mehmet Arabaci
Mehmet Arabaci
4,432 Points

[SOLVED] It seems right but doesn't pass. Anyone care to comment why? Is it because we never seem to define queue type?

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, where would I do that?

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

public class CallCenter {
  Queue<CustomerSupportRep> mSupportReps;

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

 public void acceptCustomer(Customer customer) {

    /********************************************
     * 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
     ********************************************
     */

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

    csr = mSupportReps.poll();

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

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

}
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

Hi there,

To test if the queue is empty; don't use poll() as this removes any available item in the queue. Use peek() which just has a look without altering the queue. Use poll() once there is something in mSupportReps, as you have done to assign into csr. The rest of your code looks OK.

Steve.

Mehmet Arabaci
Mehmet Arabaci
4,432 Points

Okay, thank-you. How come we never initialize Queue<CustomerSupportRep> mSupportReps with the whole = new ArrayDeque<>(); thing? Or has this been done in a class we cant see?

Hi there,

I think that there are parts of this program that aren't visible here.

The queue is created elsewhere and is passed into the constructor of CallCenter. So, wherever that instance of CallCenter is created, the Queue is created there.

I hope that helps,

Steve.

Mehmet Arabaci
Mehmet Arabaci
4,432 Points

Okay, yeah definitely helps. Thanks.