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

Jay Bhatt
Jay Bhatt
6,502 Points

How do I initialize "csr"? In the video, Craig just did "String line;" and "line" was initialized for the while loop??

I am confused as to why the csr is not initialized. It is probably a very simple solution. thank you :)

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 = new CustomerSupportRep();

    while (mSupportReps.isEmpty()) {
      playHoldMusic();
    }
    /********************************************
     * 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
     ********************************************
     */
    if (mSupportReps.size() > 0) {
      csr.assist(customer);
    }
    /********************************************
     * 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;
  }

}

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Jay - You don't need to create a new instance of CustomerSupportRep. You just need to get one out of the queue when one is available. So change the line:

CustomerSupportRep csr = new CustomerSupportRep();

to

CustomerSupportRep csr;

and before you call assisit() on csr get a CustomerSupportRep from mSupportReps and assign it to csr, something like:

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

Also, you don't need the if statement to check if mSupportReps has size more than zero. The fact that the while loop ended means that mSupportReps is not empty.

Jay Bhatt
Jay Bhatt
6,502 Points

Thank you so much, you are a genius!

Kourosh Raeen
Kourosh Raeen
23,733 Points

Glad I could help! Happy coding!