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 trialGabriel E
8,916 PointsHow Do I Queue The Customers?
Hi there,
This challenge has really got me bogged down. I need to do three things in 1 Task. You can see the challenge for yourself if your confused. Would you be able to show me with code what exactly needs to happen? Thanks!
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.....");
}
}
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;
}
}
public class Customer {
private String mName;
public Customer(String name) {
mName = name;
}
public String getName() {
return mName;
}
}
2 Answers
Kevin Faust
15,353 PointsHey Gabriel,
This is a tricky challenge at first look but it actually is quit simple once you understand what it's asking you to do. I'll break it down in a easy way. So let's start with the first step:
(1) So while we have no customer support reps in our queue, we want music to play. We can use a while loop for this easily. Use a while loop to check if our queue has nothing in it. Check the java docs on queues. It only has 6 methods and you'll be able to see what you gotta use. And then inside that while loop, just call the music method! Ill let you try this first and you can look at my answer at the bottom if your still confused.
(2) Ok onto the second step. So for this step, we have finally got our customer rep as it passed that while loop. So lets take the first person out of the queue. Do you remember which method you gotta use? Once we pull the person out of the queue, let's assign to this variable that is just chillin at the top of the method. It looks like this:
CustomerSupportRep csr;
so take the person from the queue and assign it to that. Now we can reference our customer support easily by just typing "csr". So once we got our "csr", lets call the method that does the assisting and pass in the customer.
(3) Lastly all we do here is pop that "csr" back into the queue. I think you can do that easily
In case you dont know which methods to do, I will give you a hint. We can use two. Either peek() or poll(). Peek checks if there's anything in the queue and poll tries to take the next thing out of the queue. If there's nothing inside, it'll return null.
For the second to pull out a person of the queue, we use the poll() method which takes the next thing out of the queue. This returns null if theres nothing inside and that's why you could use it above too.
If your still stumped, I've put my answer at the botton.
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);
}
I hope this solved all your questions and mark me as best answer if it helped!!
Happy Coding,
Kevin
Iheke Iheke-agu
Full Stack JavaScript Techdegree Student 2,307 PointsKevin your code was so clean, but I've never heard of poll or peek.
Gabriel E
8,916 PointsGabriel E
8,916 PointsThanks a lot Kevin, I completed this Task, and more importantly I know understand it.
Patrick Bluth
13,285 PointsPatrick Bluth
13,285 PointsHi Kevin, thanks for the help!
I don't know if I am over-thinking this, but I don't understand the first TODO. I get the while loop will begin if the queue is empty, but once the loop is entered, how is it ever ended?
The later call for adding a customerSupportRep would be inaccessible because the queue is currently empty, and the only way to fill it is by adding to it, which is outside the loop.
Is someone able to explain how this works, or why I might be confused about this?
Thanks a lot!
Patrick