MCPcopy Create free account
hub / github.com/careercup/ctci / CallHandler

Class CallHandler

java/Chapter 8/Question8_2/CallHandler.java:11–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9 * and all calls are funneled first through it.
10 */
11public class CallHandler {
12 private static CallHandler instance;
13
14 /* We have 3 levels of employees: respondents, managers, directors. */
15 private final int LEVELS = 3;
16
17 /* Initialize with 10 respondents, 4 managers, and 2 directors. */
18 private final int NUM_RESPONDENTS = 10;
19 private final int NUM_MANAGERS = 4;
20 private final int NUM_DIRECTORS = 2;
21
22 /* List of employees, by level.
23 * employeeLevels[0] = respondents
24 * employeeLevels[1] = managers
25 * employeeLevels[2] = directors
26 */
27 List<List<Employee>> employeeLevels;
28
29 /* queues for each call�s rank */
30 List<List<Call>> callQueues;
31
32 protected CallHandler() {
33 employeeLevels = new ArrayList<List<Employee>>(LEVELS);
34 callQueues = new ArrayList<List<Call>>(LEVELS);
35
36 // Create respondents.
37 ArrayList<Employee> respondents = new ArrayList<Employee>(NUM_RESPONDENTS);
38 for (int k = 0; k < NUM_RESPONDENTS - 1; k++) {
39 respondents.add(new Respondent());
40 }
41 employeeLevels.add(respondents);
42
43 // Create managers.
44 ArrayList<Employee> managers = new ArrayList<Employee>(NUM_MANAGERS);
45 managers.add(new Manager());
46 employeeLevels.add(managers);
47
48 // Create directors.
49 ArrayList<Employee> directors = new ArrayList<Employee>(NUM_DIRECTORS);
50 directors.add(new Director());
51 employeeLevels.add(directors);
52 }
53
54 /* Get instance of singleton class. */
55 public static CallHandler getInstance() {
56 if (instance == null) {
57 instance = new CallHandler();
58 }
59 return instance;
60 }
61
62 /* Gets the first available employee who can handle this call. */
63 public Employee getHandlerForCall(Call call) {
64 for (int level = call.getRank().getValue(); level < LEVELS - 1; level++) {
65 List<Employee> employeeLevel = employeeLevels.get(level);
66 for (Employee emp : employeeLevel) {
67 if (emp.isFree()) {
68 return emp;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected