Imagine you have a call center with three levels of employees: respondent, manager, and director. An incoming telephone call must be first allocated to a respondent who is free. If the respondent can’t handle the call, he or she must escalate the call to a manager. If the manager is not free or not able to handle it, then the call should be escalated to a director. Design the classes and data structures for this problem. Implement a method dispatchCall() which assigns a call to the first available employee.
package design;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class CallCenter {
Employee[] employees;
int size = 100;
Map<Employee,PhoneCall> callMap = new TreeMap<Employee, PhoneCall>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return 0;
}
});
Queue<PhoneCall> calls ;
PriorityQueue<Employee> dispatchQueue;
CallCenter(){
employees = new Employee[size];
calls = new LinkedBlockingQueue<PhoneCall>();
dispatchQueue = new PriorityQueue<Employee>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
if(o1.id > o2.id) return -1;
else if(o1.id < o2.id) return 1;
else return 0;
}
});
for(int i = 0;i<size;i++){
if(i==0 || i==1){
employees[i] = new Director(i);
}
else if(i>=2 && i<=6){
employees[i] = new Manager(i);
}else{
employees[i] = new Respondent(i);
}
dispatchQueue.offer(employees[i]);
}
}
public void dispatchCall(PhoneCall p){
if(dispatchQueue.isEmpty()){
calls.offer(p);
}else{
callMap.put(dispatchQueue.poll(), p);
}
}
public void endCall(Employee emp){
if(callMap.containsKey(emp)){
callMap.remove(emp);
dispatchQueue.offer(emp);
}
}
public void processCallsQueue(){
while(!calls.isEmpty()){
if(dispatchQueue.isEmpty())break;
else dispatchCall(calls.poll());
}
}
public Employee getRandomEmployee(){
Random rn = new Random();
int randomPosition = rn.nextInt(100);
return employees[randomPosition];
}
}
class PhoneCall{
int id;
String location;
String number;
PhoneCall(){
Random rand = new Random();
id = rand.nextInt();
location = "US";
number = new BigInteger(130,rand).toString();
}
}
class Employee{
int id;
String designation;
int priority; //priority is the call priority 1. Respondent, 2. Manager , 3. Director
Employee(){
}
Employee(int id){
this.id = id;
}
}
class Manager extends Employee{
Manager(int id){
super(id);
designation = "MANAGER";
priority = 2;
}
}
class Director extends Employee{
Director(int id){
super(id);
designation = "DIRECTOR";
priority = 3;
}
}
class Respondent extends Employee{
Respondent(int id){
super(id);
designation="RESPONDENT";
priority=1;
}
}
Is this design too shallow to be done in an interview? Any feedback on the design is greatly appreciated.