Java Java Data Structures Efficiency! Call Center Queue

Damjan Vlaic
Damjan Vlaic
19,241 Points

---JAVA---

Can someone help me fix this acceptCustomer method in CallCenter.java pls? 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.
     ********************************************
     */
  }

  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

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,546 Points

hi there,

There's three TODO instructions. The first wants you to create a while loop that calls the playHoldMusic() method while there is no-one in the mSupportReps queue. Use peek() on the queue, and loop while that returns null. Call the playHoldMusic method inside the loop.

Next, you want to assign the result of calling poll() on the mSupportReps queue into the existing csr variable.

Next, call assist() on csr and pass in the customer received as an argument.

Last, add the csr back into mSupportReps.

I hope that helps.

Steve.

Damjan Vlaic
Damjan Vlaic
19,241 Points

for now I typed this but I don't know what to pass in to assist method, can you help pls Steve?

while (mSupportReps.peek() == null) {
  playHoldMusic();
}
csr.assist();  //<--- what do i pass in the brackets
Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

The acceptCustomer method you are coding inside receives an argument called customer - pass that in. :smile: