This is the requirement I have (from the book: Cracking the Coding Interview)
Imagine you have a call center with three levels of employees: fresher, technical lead (TL), and product manager (PM). There can be multiple employees, but only one TL or PM. An incoming telephone call must be allocated to a fresher who is free. If a fresher can't handle the call, he or she must escalate the call to technical lead. If the TL is not free or not able to handle it, then the call should be escalated to PM. Design the classes and data structures for this problem. Implement a method
getCallHandler()
.
And this is my implementation:
public interface CallAllocator {
public Employee getCallHandler() throws NoEmployeeInTheHouseException;
void setTL(TechnicalLead technicalLead);
void setPM(ProductManager productManager);
void addFresher(Fresher fresher);
}
Implementation for the interface:
public class CallAllocatorImpl implements CallAllocator {
private TechnicalLead technicalLead;
private ProductManager productManager;
private List<Fresher> freshers = new ArrayList<Fresher>();
@Override
public Employee getCallHandler() throws NoEmployeeInTheHouseException {
if (freshers.isEmpty() && technicalLead == null && productManager == null) {
throw new NoEmployeeInTheHouseException();
}
if (!freshers.isEmpty()) {
Employee fresher = freshers.get(new Random().nextInt(freshers.size()));
if (fresher.getCanHandle()) {
return fresher;
}
}
if (technicalLead != null && technicalLead.getCanHandle()) {
return technicalLead;
}
if (productManager != null && productManager.getCanHandle()) {
return productManager;
}
throw new NoEmployeeInTheHouseException();
}
@Override
public void setTL(TechnicalLead technicalLead) {
this.technicalLead = technicalLead;
}
@Override
public void setPM(ProductManager productManager) {
this.productManager = productManager;
}
@Override
public void addFresher(Fresher fresher) {
if (fresher.isFree()) {
freshers.add(fresher);
}
}
}
Employee class:
public class Employee {
private boolean free;
private boolean canHandle;
public boolean isFree() {
return free;
}
public void setFree(boolean free) {
this.free = free;
}
public boolean getCanHandle() {
return canHandle;
}
public void setCanHandle(boolean canHandle) {
this.canHandle = canHandle;
}
}
I have 3 classes with names: Fresher, TechnicalLead and ProductManager. They all extend Employee but do not override any methods or anything..
And this is my TestClass:
public class TestClass {
public static void main(String[] args) throws NoEmployeeInTheHouseException {
CallAllocator callAllocator = new CallAllocatorImpl();
Fresher fresherOne = new Fresher();
fresherOne.setCanHandle(false);
fresherOne.setFree(true);
Fresher fresherTwo = new Fresher();
fresherTwo.setCanHandle(true);
fresherTwo.setFree(true);
Fresher fresherThree = new Fresher();
fresherThree.setCanHandle(false);
fresherThree.setFree(true);
Fresher fresherFour = new Fresher();
fresherFour.setCanHandle(false);
fresherFour.setFree(false);
callAllocator.addFresher(fresherOne);
callAllocator.addFresher(fresherTwo);
callAllocator.addFresher(fresherThree);
callAllocator.addFresher(fresherFour);
TechnicalLead technicalLead = new TechnicalLead();
technicalLead.setCanHandle(false);
technicalLead.setFree(true);
callAllocator.setTL(technicalLead);
ProductManager productManager = new ProductManager();
productManager.setCanHandle(true);
productManager.setFree(true);
callAllocator.setPM(productManager);
Employee callHandler = callAllocator.getCallHandler();
System.out.println(callHandler.getClass().getSimpleName());
}
}
So how can I improve this code? Any suggestions?