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

Damjan Vlaic
Damjan Vlaic
19,244 Points

---JAVA---

Can someone help me pls with acceptCustomer method; i wrote some stuff but i am still getting a error and i don't know why. The task says:

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

    /********************************************
     * 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.
     ********************************************
     */
    while (mSupportReps.peek() == null) {
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(customer);
  }

  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

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Ah yes. Sorry about that typo in the last part. Glad you can spot it.

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Damjan

You almost done it! However, if I may give you a suggestion (just a suggestion) to keep your code as close as possible to the what to do at that moment. Well it does not hurt to make other developer read your code easier.

Okay now for the main problems. The problem in your code is the usage of mSupportReps.peek() queue method. The task already reminds you that in the HINT: That while assignmentcheck loop syntax we used to read files seems pretty similar. Meaning the while loop must do two things. One check if the csr is available or not, two also assigned one csr if indeed there is one available. This needs a certain method of queue called poll().

While peek() also checked (retrieve) whether there is csr in the queue or not (null) but it does not remove the available head of csr queue when it is available. Poll() will automatically assigned the head of the csr queue to the csr object.

This is how you should code it:

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

    /********************************************
     * 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.
     ********************************************
     */
//    while (mSupportReps.peek() == null) {//this one supposed to use the m.SupportReps.poll().
//and by the way the HINT for TODO(1) also refers to csr assignments, this is how it should be done:
    while ((csr = mSupportReps.poll()) == null) {
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(customer);
  }

I hope this can help a little

Damjan Vlaic
Damjan Vlaic
19,244 Points

tnx for helping(and also for the suggeston :D), your method worked perfectly except of the "mSupportReps.add(customer);" it should look like this "mSupportReps.add(csr);"