Skip to content

Commit 7474f9c

Browse files
committed
add source files and do direct translation to es6 for ch7, q2
1 parent 229ba29 commit 7474f9c

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed

src/chapter7/q2/Call.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export class Call {
2+
constructor(c) {
3+
/* Minimal rank of employee who can handle this call. */
4+
this.rank = Rank.Responder();
5+
6+
/* Person who is calling. */
7+
this.caller = c;
8+
9+
/* Employee who is handling call. */
10+
this.handler;
11+
}
12+
13+
/* Set employee who is handling call. */
14+
setHandler(e) {
15+
this.handler = e;
16+
}
17+
18+
/* Play recorded message to the customer. */
19+
reply(message) {
20+
console.log(message);
21+
}
22+
23+
getRank() {
24+
return this.rank;
25+
}
26+
27+
setRank(r) {
28+
this.rank = r;
29+
}
30+
31+
incrementRank() {
32+
if (this.rank == Rank.Responder()) {
33+
this.rank = Rank.Manager();
34+
} else if (this.rank == Rank.Manager()) {
35+
this.rank = Rank.Director();
36+
}
37+
return this.rank;
38+
}
39+
40+
/* Disconnect call. */
41+
disconnect() {
42+
this.reply("Thank you for calling");
43+
}
44+
}

src/chapter7/q2/CallHandler.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* CallHandler represents the body of the program,
2+
* and all calls are funneled first through it.
3+
*/
4+
5+
/* We have 3 levels of employees: respondents, managers, directors. */
6+
const LEVELS = 3;
7+
8+
/* Initialize with 10 respondents, 4 managers, and 2 directors. */
9+
const NUM_RESPONDENTS = 10;
10+
const NUM_MANAGERS = 4;
11+
const NUM_DIRECTORS = 2;
12+
13+
export class CallHandler {
14+
constructor() {
15+
/* List of employees, by level.
16+
* employeeLevels[0] = respondents
17+
* employeeLevels[1] = managers
18+
* employeeLevels[2] = directors
19+
*/
20+
this.employeeLevels = new Array(LEVELS);
21+
/* queues for each call�s rank */
22+
this.callQueues = new Array(LEVELS);
23+
24+
// Create respondents.
25+
let respondents = new Array(NUM_RESPONDENTS);
26+
for (let k = 0; k < NUM_RESPONDENTS - 1; k++) {
27+
respondents.push(new Respondent(this));
28+
}
29+
this.employeeLevels[0] = respondents;
30+
31+
// Create managers.
32+
let managers = new Array(NUM_MANAGERS);
33+
managers.push(new Manager(this));
34+
this.employeeLevels[1] = managers;
35+
36+
// Create directors.
37+
let directors = new Array(NUM_DIRECTORS);
38+
directors.push(new Director(this));
39+
this.employeeLevels[2] = directors;
40+
}
41+
42+
/* Gets the first available employee who can handle this call. */
43+
getHandlerForCall(call) {
44+
for (let level = call.getRank().getValue(); level < LEVELS - 1; level++) {
45+
let employeeLevel = this.employeeLevels[level];
46+
for (let emp of employeeLevel) {
47+
if (emp.isFree()) {
48+
return emp;
49+
}
50+
}
51+
}
52+
return null;
53+
}
54+
55+
/* Routes the call to an available employee, or saves in a queue if no employee available. */
56+
dispatchCall(caller) {
57+
let call = new Call(caller);
58+
dispatchCall(call);
59+
}
60+
61+
/* Routes the call to an available employee, or saves in a queue if no employee available. */
62+
public void dispatchCall(Call call) {
63+
/* Try to route the call to an employee with minimal rank. */
64+
Employee emp = getHandlerForCall(call);
65+
if (emp != null) {
66+
emp.receiveCall(call);
67+
call.setHandler(emp);
68+
} else {
69+
/* Place the call into corresponding call queue according to its rank. */
70+
call.reply("Please wait for free employee to reply");
71+
callQueues.get(call.getRank().getValue()).add(call);
72+
}
73+
}
74+
75+
/* An employee got free. Look for a waiting call that he/she can serve. Return true
76+
* if we were able to assign a call, false otherwise. */
77+
assignCall(emp) {
78+
/* Check the queues, starting from the highest rank this employee can serve. */
79+
for (let rank = emp.getRank().getValue(); rank >= 0; rank--) {
80+
let que = this.callQueues[rank];
81+
82+
/* Remove the first call, if any */
83+
if (que.length) {
84+
let call = que.shift();
85+
if (call != null) {
86+
emp.receiveCall(call);
87+
return true;
88+
}
89+
}
90+
}
91+
return false;
92+
}
93+
}

src/chapter7/q2/Caller.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Caller {
2+
constructor(id, nm) {
3+
this.name = nm;
4+
this.userId = id;
5+
}
6+
}

src/chapter7/q2/Director.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Director extends Employee {
2+
constructor(callHandler) {
3+
super(callHandler);
4+
this.rank = Rank.Director();
5+
}
6+
}

src/chapter7/q2/Employee.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Employee is a super class for the Director, Manager, and Respondent classes. It is implemented as an
2+
* abstract class, since there should be no reason to instantiated an Employee type directly.
3+
*/
4+
class Employee {
5+
public Employee(CallHandler handler) {
6+
this.currentCall = null;
7+
this.rank;
8+
9+
this.callHandler = handler;
10+
}
11+
12+
/* Start the conversation */
13+
receiveCall(call) {
14+
this.currentCall = call;
15+
}
16+
17+
/* the issue is resolved, finish the call */
18+
callCompleted() {
19+
if (this.currentCall != null) {
20+
/* Disconnect the call. */
21+
this.currentCall.disconnect();
22+
23+
/* Free the employee */
24+
this.currentCall = null;
25+
}
26+
27+
/* Check if there is a call waiting in queue */
28+
this.assignNewCall();
29+
}
30+
31+
/*
32+
* The issue has not been resolved. Escalate the call, and assign a new call
33+
* to the employee.
34+
*/
35+
escalateAndReassign() {
36+
if (this.currentCall != null) {
37+
/* esthis.calate call */
38+
this.currentCall.incrementRank();
39+
this.callHandler.dispatchCall(currentCall);
40+
41+
/* free the employee */
42+
this.currentCall = null;
43+
}
44+
45+
/* assign a new call */
46+
this.assignNewCall();
47+
}
48+
49+
/* Assign a new call to an employee, if the employee is free. */
50+
assignNewCall() {
51+
if (!this.isFree()) {
52+
return false;
53+
}
54+
return this.callHandler.assignCall(this);
55+
}
56+
57+
/* Returns whether or not the employee is free. */
58+
isFree() {
59+
return this.currentCall == null;
60+
}
61+
62+
getRank() {
63+
return this.rank;
64+
}
65+
}

src/chapter7/q2/Manager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Manager extends Employee {
2+
constructor(callHandler) {
3+
super(callHandler);
4+
this.rank = Rank.Manager();
5+
}
6+
}

src/chapter7/q2/Rank.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export class Rank {
2+
Responder (0),
3+
Manager (1),
4+
Director (2);
5+
6+
private int value;
7+
8+
private Rank(int v) {
9+
value = v;
10+
}
11+
12+
public int getValue() {
13+
return value;
14+
}
15+
}
16+
export const Rank = {
17+
value: undefined,
18+
Responder : () => {
19+
this.value = 0;
20+
return this.value;
21+
},
22+
Manager : () => {
23+
this.value = 1;
24+
return this.value;
25+
},
26+
Director : () => {
27+
this.value = 2;
28+
return this.value;
29+
},
30+
getValue() {
31+
return this.value;
32+
}
33+
}

src/chapter7/q2/Respondent.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Respondent extends Employee {
2+
constructor(callHandler) {
3+
super(callHandler);
4+
this.rank = Rank.Responder();
5+
}
6+
}

src/chapter7/q2/ch7-q2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new CallHandler();

0 commit comments

Comments
 (0)