Java Java Data Structures Efficiency! Call Center Queue

Tan Yang
Tan Yang
13,163 Points

Please help with call center challenge, there is an exception

Please check my code

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

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

Livia Galeazzi
PRO
Livia Galeazzi
Java Web Development Techdegree Graduate 19,900 Points

The issue is here:

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

You try to find a CSR once, then you check if it's null and play the music. But then you never attempt to find a CSR again. So the CSR will stay null forever and the customer will keep listening to that music until he gives up ;-)

You need to try assigning a CSR again every time you go through the loop. The comments give you a hint on how to do this: "That while assignmentcheck loop syntax we used to read files seems pretty similar". That was this piece of code:

String line;
while((line = reader.nextLine()) != null) {
     //do something
}

Notice how the line variable is assigned a new value from inside the while condition? Try to do something like that.

Jason Poole
Jason Poole
3,123 Points

So the CSR will stay null forever and the customer will keep listening to that music until he gives up ;-)

Thank you for your help Livia.

... this problem (or at least the solution i gave) seems a big jump in difficulty from the previous problems given i had to use something that hasn't been taught in the course yet