Created
January 16, 2015 02:36
-
-
Save blindchaser/e846c3ca80a0cb629635 to your computer and use it in GitHub Desktop.
8.2 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 …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Title: CC150 8.2 callCenter | |
* Time: 20140115 | |
* Creator: blindchaser | |
* Time Complexity: | |
* Space Complexity: | |
* 优空间解法 | |
* 优时间解法: | |
* 心得: 可以跑 | |
* | |
* | |
*/ | |
package chapter_8; | |
import java.util.ArrayList; | |
import java.util.Stack; | |
public class callCenter { | |
static ArrayList<Employee> respondents = new ArrayList<Employee>(); | |
static ArrayList<Employee> managers = new ArrayList<Employee>(); | |
static ArrayList<Employee> directors = new ArrayList<Employee>(); | |
static Stack<Employee> stack = new Stack<Employee>(); | |
public static void main(String args[]) { | |
createStaff(); | |
respondents.get(0).status = false; | |
managers.get(0).status = false; | |
directors.get(0).status = false; | |
directors.get(1).status = false; | |
Call c1 = new Call(0,8); | |
Call c2 = new Call(1,10); | |
Call c3 = new Call(2,30); | |
System.out.println(handleCall(c1)); | |
System.out.println(handleCall(c2)); | |
System.out.println(handleCall(c3)); | |
} | |
private static void createStaff() { | |
for (int i = 0; i < 30; i++) { | |
Employee e = new Employee(i, i, true); | |
if (i < 10) | |
respondents.add(e); | |
else if (10 <= i && i < 20) | |
managers.add(e); | |
else | |
directors.add(e); | |
} | |
} | |
private static String handleCall(Call c) { | |
String str = ""; | |
// 先找到一个空的respondents; | |
for (Employee e : respondents) { | |
if (e.status == true) { | |
stack.add(e); | |
e.status = false; | |
break; | |
} | |
} | |
if (c.hardLevel < 10) | |
return str += "Respondent " + stack.peek().number | |
+ " handled the call"; | |
// 若难度过大,forward给manager | |
else if (c.hardLevel >= 10 && c.hardLevel < 20) { | |
for (Employee e : managers) { | |
if (e.status == true) { | |
stack.pop().status = true; | |
stack.add(e); | |
e.status = false; | |
return str = "Manager " + stack.peek().number | |
+ " handled the call"; | |
} | |
} | |
} | |
// 难度再过大,forward给director | |
for (Employee e : directors) { | |
if (e.status == true) { | |
stack.pop().status = true; | |
stack.add(e); | |
e.status = false; | |
return str = "Director " + stack.peek().number | |
+ " handled the call"; | |
} | |
} | |
return str = "System is busy, please wait"; | |
} | |
private static class Employee { | |
int number;// 0-9 are respondents; 10-19 are managers; 20-29 are// | |
//directors; | |
int level; | |
boolean status; | |
public Employee(int num, int level, boolean status) { | |
this.number = num; | |
this.level = level; | |
this.status = status; | |
} | |
} | |
private static class Call { | |
int caller; | |
int hardLevel; | |
public Call(int caller, int hardLevel) { | |
this.caller = caller; | |
this.hardLevel = hardLevel; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment