Skip to content

Commit 3e6dbf5

Browse files
committed
2 parents 615920b + 46b205e commit 3e6dbf5

25 files changed

+1165
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* Represents a call from a user. Calls have a minimum rank and are assigned to the
5+
* first employee who can handle that call.
6+
*/
7+
public class Call {
8+
/* Minimal rank of employee who can handle this call. */
9+
private Rank rank;
10+
11+
/* Person who is calling. */
12+
private Caller caller;
13+
14+
/* Employee who is handling call. */
15+
private Employee handler;
16+
17+
public Call(Caller c) {
18+
rank = Rank.Responder;
19+
caller = c;
20+
}
21+
22+
/* Set employee who is handling call. */
23+
public void setHandler(Employee e) {
24+
handler = e;
25+
}
26+
27+
/* Play recorded message to the customer. */
28+
public void reply(String message) {
29+
System.out.println(message);
30+
}
31+
32+
public Rank getRank() {
33+
return rank;
34+
}
35+
36+
public void setRank(Rank r) {
37+
rank = r;
38+
}
39+
40+
public Rank incrementRank() {
41+
if (rank == Rank.Responder) {
42+
rank = Rank.Manager;
43+
} else if (rank == Rank.Manager) {
44+
rank = Rank.Director;
45+
}
46+
return rank;
47+
}
48+
49+
/* Disconnect call. */
50+
public void disconnect() {
51+
reply("Thank you for calling");
52+
}
53+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package joshua.oop.callcenter;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author Joshua.Jiang on 2016/6/2.
8+
*/
9+
public class CallCenter {
10+
private static CallCenter instance;
11+
12+
/* We have 3 levels of employees: respondents, managers, directors. */
13+
private final int LEVELS = 3;
14+
15+
/* Initialize with 10 respondents, 4 managers, and 2 directors. */
16+
private final int NUM_RESPONDENTS = 10;
17+
private final int NUM_MANAGERS = 4;
18+
private final int NUM_DIRECTORS = 2;
19+
20+
/* List of employees, by level.
21+
* employeeLevels[0] = respondents
22+
* employeeLevels[1] = managers
23+
* employeeLevels[2] = directors
24+
*/
25+
List<List<Employee>> employeeLevels;
26+
27+
/* queues for each call’s rank */
28+
List<List<Call>> callQueues;
29+
30+
protected CallCenter() {
31+
employeeLevels = new ArrayList<List<Employee>>(LEVELS);
32+
callQueues = new ArrayList<List<Call>>(LEVELS);
33+
34+
// Create respondents.
35+
ArrayList<Employee> respondents = new ArrayList<Employee>(NUM_RESPONDENTS);
36+
for (int k = 0; k < NUM_RESPONDENTS - 1; k++) {
37+
respondents.add(new Respondent());
38+
}
39+
employeeLevels.add(respondents);
40+
41+
// Create managers.
42+
ArrayList<Employee> managers = new ArrayList<Employee>(NUM_MANAGERS);
43+
managers.add(new Manager());
44+
employeeLevels.add(managers);
45+
46+
// Create directors.
47+
ArrayList<Employee> directors = new ArrayList<Employee>(NUM_DIRECTORS);
48+
directors.add(new Director());
49+
employeeLevels.add(directors);
50+
}
51+
52+
/* Get instance of singleton class. */
53+
public static CallCenter getInstance() {
54+
if (instance == null) {
55+
instance = new CallCenter();
56+
}
57+
return instance;
58+
}
59+
60+
/* Gets the first available employee who can handle this call. */
61+
public Employee getHandlerForCall(Call call) {
62+
for (int level = call.getRank().getValue(); level < LEVELS - 1; level++) {
63+
List<Employee> employeeLevel = employeeLevels.get(level);
64+
for (Employee emp : employeeLevel) {
65+
if (emp.isFree()) {
66+
return emp;
67+
}
68+
}
69+
}
70+
return null;
71+
}
72+
73+
/* Routes the call to an available employee, or saves in a queue if no employee available. */
74+
public void dispatchCall(Caller caller) {
75+
Call call = new Call(caller);
76+
dispatchCall(call);
77+
}
78+
79+
/* Routes the call to an available employee, or saves in a queue if no employee available. */
80+
public void dispatchCall(Call call) {
81+
/* Try to route the call to an employee with minimal rank. */
82+
Employee emp = getHandlerForCall(call);
83+
if (emp != null) {
84+
emp.receiveCall(call);
85+
call.setHandler(emp);
86+
} else {
87+
/* Place the call into corresponding call queue according to its rank. */
88+
call.reply("Please wait for free employee to reply");
89+
callQueues.get(call.getRank().getValue()).add(call);
90+
}
91+
}
92+
93+
/* An employee got free. Look for a waiting call that he/she can serve. Return true
94+
* if we were able to assign a call, false otherwise. */
95+
public boolean assignCall(Employee emp) {
96+
/* Check the queues, starting from the highest rank this employee can serve. */
97+
for (int rank = emp.getRank().getValue(); rank >= 0; rank--) {
98+
List<Call> que = callQueues.get(rank);
99+
100+
/* Remove the first call, if any */
101+
if (que.size() > 0) {
102+
Call call = que.remove(0);
103+
if (call != null) {
104+
emp.receiveCall(call);
105+
return true;
106+
}
107+
}
108+
}
109+
return false;
110+
}
111+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public class Caller {
7+
private String name;
8+
private int userId;
9+
10+
public Caller(int id, String nm) {
11+
name = nm;
12+
userId = id;
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public class Director extends Employee {
7+
public Director() {
8+
rank = Rank.Director;
9+
}
10+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public class Employee {
7+
private Call currentCall = null;
8+
protected Rank rank;
9+
10+
public Employee() {
11+
}
12+
13+
/* Start the conversation */
14+
public void receiveCall(Call call) {
15+
currentCall = call;
16+
}
17+
18+
/* the issue is resolved, finish the call */
19+
public void callCompleted() {
20+
if (currentCall != null) {
21+
/* Disconnect the call. */
22+
currentCall.disconnect();
23+
24+
/* Free the employee */
25+
currentCall = null;
26+
}
27+
28+
/* Check if there is a call waiting in queue */
29+
assignNewCall();
30+
}
31+
32+
/*
33+
* The issue has not been resolved. Escalate the call, and assign a new call
34+
* to the employee.
35+
*/
36+
public void escalateAndReassign() {
37+
if (currentCall != null) {
38+
/* escalate call */
39+
currentCall.incrementRank();
40+
CallCenter.getInstance().dispatchCall(currentCall);
41+
42+
/* free the employee */
43+
currentCall = null;
44+
}
45+
46+
/* assign a new call */
47+
assignNewCall();
48+
}
49+
50+
/* Assign a new call to an employee, if the employee is free. */
51+
public boolean assignNewCall() {
52+
if (!isFree()) {
53+
return false;
54+
}
55+
return CallCenter.getInstance().assignCall(this);
56+
}
57+
58+
/* Returns whether or not the employee is free. */
59+
public boolean isFree() {
60+
return currentCall == null;
61+
}
62+
63+
public Rank getRank() {
64+
return rank;
65+
}
66+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public class Manager extends Employee {
7+
public Manager() {
8+
rank = Rank.Manager;
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public enum Rank {
7+
Responder(0),
8+
Manager(1),
9+
Director(2);
10+
11+
private int value;
12+
13+
private Rank(int v) {
14+
value = v;
15+
}
16+
17+
public int getValue() {
18+
return value;
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package joshua.oop.callcenter;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public class Respondent extends Employee {
7+
public Respondent() {
8+
rank = Rank.Responder;
9+
}
10+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package joshua.oop.cardgame;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/6/2.
5+
*/
6+
public class BlackJackCard extends Card {
7+
8+
public BlackJackCard(int c, Suit s) {
9+
super(c, s);
10+
}
11+
12+
public int value() {
13+
if (isAce()) { // Ace
14+
return 1;
15+
} else if (isFaceCard()) { // Face card
16+
return 10;
17+
} else { // Number card
18+
return faceValue;
19+
}
20+
}
21+
22+
public int minValue() {
23+
if (isAce()) { // Ace
24+
return 1;
25+
} else {
26+
return value();
27+
}
28+
}
29+
30+
public int maxValue() {
31+
if (isAce()) { // Ace
32+
return 11;
33+
} else {
34+
return value();
35+
}
36+
}
37+
38+
public boolean isAce() {
39+
return faceValue == 1;
40+
}
41+
42+
public boolean isFaceCard() {
43+
return faceValue >= 11 && faceValue <= 13;
44+
}
45+
}

0 commit comments

Comments
 (0)