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

need help with this question?

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.

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;
  }

}

5 Answers

Dan Johnson
Dan Johnson
40,533 Points

For the first item on the to do list:

  1. In a loop, check to see if mSupportReps has any elements.
  2. In that loop, call playHoldMusic.

For the next item:

  1. Remove a representative from mSupportReps, but keep a local reference using the csr variable.
  2. Call assist on the representative you just removed from the queue, passing in the customer that was passed in to acceptCustomer.

And for the final item:

  1. Add the representative back into mSupportReps.

Here's a link to the ArrayDeque documentation for reference.

public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
  try  (CustomerSupportReps crs = new CustomerSupportReps(customer);
    CustomerQueue queue = new CustomerQueue (new CustomerSupportReps(crs));
       ) {
    Customer line;
    while(( line = queue.readLine()) =null) {
    }
  }  catch(IOException ioe) {
    System.out.printf("playHoldMusic %s", customer);
Dan Johnson
Dan Johnson
40,533 Points

Don't need a try with resources for this. Try with resources can only be used with a class that implements the AutoCloseable or Closeable interface and ArrayDeque is not one that does.

You also won't need to read input from the user (ArrayDeque is also incapable of that on its own).

Here's a list of methods you can use to get started:

Dan Johnson
Dan Johnson
40,533 Points

Added code formatting to your comment.

You can see a list of all options if you click on the Markdown Cheatsheet link above the post button.

it seems i am geting my code wrong

Dan Johnson
Dan Johnson
40,533 Points

This challenge is a bit difficult to follow since it's assuming some behavior you can't really see. I'll walk through the first step:

Pseudocode:

While there are no available support representatives:
    Play hold music until one is made available

Java:

    while(mSupportReps.isEmpty()) {
      playHoldMusic(); 
    }

This part assumes that the mSupportReps ArrayDeque will be modified somehow to end the loop, so don't worry about dealing with that yourself.

For the other steps I'll just list some pseudocode:

  1. Set csr to the top of the mSupportReps ArrayDeque while also removing it.
  2. Call assist on csr passing in customer

Then

  1. Add csr in to mSupportReps

If you're still having trouble after this feel free to post your updated code and I'll go over it again.

public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    while(mSupportreps.isEmpty()) {
      playHoldMusic();
    } 
    public void Set<CustomerSupportRep>assist(Customer Customer) {
      Customer customer=mSupportReps.poll();

    }
     mSupportsREps.addCustomerSupportReps();
  }
Dan Johnson
Dan Johnson
40,533 Points

Looks like you figured out which methods to use and where so let's go over the last details:

//Remember that Java is case sensitive so we need to capitalize the 'r'
while(mSupportReps.isEmpty()) {
      playHoldMusic(); 
}

// In the pseudocode I provided I meant set to mean "assign a value to" 
// so we'll do this instead
csr = mSupportReps.poll();
// assist is already defined for the CustomerSupportRep class
// so we can just call it and pass in the customer.
csr.assist(customer);

//Same thing with the case sensitivity and naming here. 
mSupportReps.add(csr);

Spoiler Alert!

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

    while  (mSupportReps.peek() == null) { // or we can use .poll() as they both return null if theres nothing in there
      playHoldMusic();
    }

    /********************************************
     * TODO (2) 
     * After we have assigned the rep, call the 
     * assist method and pass in the customer
     ********************************************
     */

    csr = mSupportReps.poll();
    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.....");
  }

}

Dan i will keep posting you are opening my mind

Elis Amet
Elis Amet
3,230 Points
public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    while(mSupportReps.isEmpty()){
      playHoldMusic();
    }

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

Even though I got this problem solved with the following answer:

''''csr = null;
while(mSupportReps.isEmpty()) playHoldMusic();

for(CustomerSupportRep rep: mSupportReps){
  if(rep.getAssistedCustomers().size() ==0) {
    rep.assist(customer);
    mSupportReps.remove(rep);
    csr = rep;
    break;
  }
}
if(csr != null) mSupportReps.add(csr);

there is still a question left puzzling me and hopefully I can get some feedbacks from whomever would like to share opinions: Among existing customer representatives, what if there is no customer representative to assist customer?

//I don't think my answer really covers this condition yet it seems to be good enough for this problem :\