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 trialAdmire Dhodho
5,411 Pointsneed 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.
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;
}
}
5 Answers
Dan Johnson
40,533 PointsFor the first item on the to do list:
- In a loop, check to see if mSupportReps has any elements.
- In that loop, call playHoldMusic.
For the next item:
- Remove a representative from mSupportReps, but keep a local reference using the csr variable.
- 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:
- Add the representative back into mSupportReps.
Here's a link to the ArrayDeque documentation for reference.
Zach Freitag
20,341 PointsSpoiler 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.....");
}
}
Admire Dhodho
5,411 PointsDan i will keep posting you are opening my mind
Elis Amet
3,230 Pointspublic void acceptCustomer(Customer customer) {
CustomerSupportRep csr;
while(mSupportReps.isEmpty()){
playHoldMusic();
}
csr = mSupportReps.poll();
csr.assist(customer);
mSupportReps.add(csr);
}
yuehwencheng
29,154 PointsEven 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 :\
Admire Dhodho
5,411 PointsAdmire Dhodho
5,411 PointsDan Johnson
40,533 PointsDan Johnson
40,533 PointsDon'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
40,533 PointsDan Johnson
40,533 PointsAdded 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.
Admire Dhodho
5,411 PointsAdmire Dhodho
5,411 Pointsit seems i am geting my code wrong
Dan Johnson
40,533 PointsDan Johnson
40,533 PointsThis 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:
Java:
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:
Then
If you're still having trouble after this feel free to post your updated code and I'll go over it again.
Admire Dhodho
5,411 PointsAdmire Dhodho
5,411 PointsDan Johnson
40,533 PointsDan Johnson
40,533 PointsLooks like you figured out which methods to use and where so let's go over the last details: