Careercup | Chapter 8

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 notable to handle it, then the call should be escalated to a director. Design the classes and data structures for this problem. Implement a method dispatchCaL L () which assigns a call to the first available employee

 1 struct Call {
 2     string phoneNumber;
 3     string content;
 4 };
 5 enum RANK {
 6 RESPONDENT, MANAGER, DIRECTOR
 7 };
 8 class Employee {
 9 public:
10     Employee(int level) : level(level) {}
11     int getLevel() const { return level; }
12     bool isFree() const { return calls.empty(); }
13     virtual bool handleCall(Call call) = 0;
14 protected:
15     queue<Call> calls;
16 private:
17     int level;
18 };
19
20 class Respondent : public Employee {
21 public:
22     Respondent() : Employee(RANK::RESPONDENT) {}
23     bool handleCall(Call call) { /*...*/ return true;}
24 };
25
26 class Manager: public Employee {
27 public:
28     Manager() : Employee(RANK::MANAGER) {}
29     bool handleCall(Call call) { /*...*/ return true;}
30 };
31
32 class Director : public Employee {
33 public:
34     Director(): Employee(RANK::DIRECTOR) {}
35 bool handleCall(Call call) {/*...*/ return true; }
36 };
37
38 class CallCenter {
39 public:
40     bool dispatchCall(Call call) {
41         for (map<int, vector<Employee> >::iterator it = employees.begin();
42             it != employees.end(); it++) {
43             for (int i = 0; i < it->second.size(); ++i) {
44                 if (it->second[i].isFree() && it->second[i].handleCall(call)) {
45                     return true;
46                 }
47             }
48         }
49         return false;
50     }
51
52     void addEmployee(Employee &employee) {
53         employees[employee.getLevel()].push_back(employee);
54 }
55 private:
56     map<int, vector<Employee> > employees;
57 };
时间: 2024-11-14 22:53:43

Careercup | Chapter 8的相关文章

Careercup | Chapter 4

二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,叶子都在最后一层,所有结点(除了叶子)都有两个孩子. 平衡二叉树,左右子树的高度在一定范围内. 4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two s

Careercup | Chapter 6

6.3 You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the

Careercup | Chapter 1

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? 字符串问题,需要先确定是不是只有ASCII码. 如果是,可以用char[256],也可以用位向量.位向量的实现参照<编程珠玑>.i&MASK就是取余.i>>SHIFT就是取商. 1 class BitVector {

Careercup | Chapter 5

5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to insert M into Nsuch that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all ofM. That is, ifM= 100

CareerCup chapter 1 Arrays and Strings

1.Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures? The length of ACSII code of a character is 8, so we can build a array, the length is 260, to represent the hash table of a

Careercup | Chapter 3

3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满了之后,需要把相邻的栈调整好,这是一个递归的过程. 每个stack有一些属性,所以不妨将每个stack封闭起来,我这里是用一个private的struct来实现,方便,同时对外部又不可见. 对于一些常用的操作,比如环形数组取下一个数,前一个数,都可以封装起来. 1 class XNStack { 2

Careercup | Chapter 7

7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only the add operator. 比较简单.但是要封装得好. 7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in half. Assume that th

CareerCup Chapter 4 Trees and Graphs

struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),right(NULL){} }; Not all binary trees are binary search trees. 4.1 Implement a function to check if a tree is balanced. For the purposes of this question

CareerCup之1.1字符串中字符判重

[题目] Chapter 1 | Arrays and Strings 原文: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 译文: 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构. (即只使用基本的数据结构) [分析] [思路一]首先,我们要搞清