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 Efficiency! Queueing

Miguel Angel Rodriguez
Miguel Angel Rodriguez
11,280 Points

Need help with the code challenge

Im stuck with the code challenge, please help me. I write the code but the message show me "Try again" error.

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; while ((csr = mSupportReps.poll()) != null) { csr.assist(customer); if (csr.getAssistedCustomers().contains(customer)) { mSupportReps.add(csr); } } 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
 ********************************************
 */

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

}

Miguel Angel Rodriguez
Miguel Angel Rodriguez
11,280 Points
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;
    while ((csr = mSupportReps.poll()) != null) {
      csr.assist(customer);
      if (csr.getAssistedCustomers().contains(customer)) {
        mSupportReps.add(csr);
      }
    }
    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
     ********************************************
     */

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

}

1 Answer

Dan Johnson
Dan Johnson
40,532 Points

mSupportReps not being null means someone is available to assist, so you'll want to check for the opposite:

// While there are no support reps ready
while((csr = mSupportReps.poll()) == null) {
    // Continue to play the hold music
    playHoldMusic(); 
}

If the loop stops that means someone got selected, so we can use csr:

// Assist the customer
csr.assist(customer);
// Move back into the queue since they've finished assisting
mSupportReps.add(csr);