Careercup | Chapter 8

时间:2022-09-16 20:56:40

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

 struct Call {
string phoneNumber;
string content;
};
enum RANK {
RESPONDENT, MANAGER, DIRECTOR
};
class Employee {
public:
Employee(int level) : level(level) {}
int getLevel() const { return level; }
bool isFree() const { return calls.empty(); }
virtual bool handleCall(Call call) = ;
protected:
queue<Call> calls;
private:
int level;
}; class Respondent : public Employee {
public:
Respondent() : Employee(RANK::RESPONDENT) {}
bool handleCall(Call call) { /*...*/ return true;}
}; class Manager: public Employee {
public:
Manager() : Employee(RANK::MANAGER) {}
bool handleCall(Call call) { /*...*/ return true;}
}; class Director : public Employee {
public:
Director(): Employee(RANK::DIRECTOR) {}
bool handleCall(Call call) {/*...*/ return true; }
}; class CallCenter {
public:
bool dispatchCall(Call call) {
for (map<int, vector<Employee> >::iterator it = employees.begin();
it != employees.end(); it++) {
for (int i = ; i < it->second.size(); ++i) {
if (it->second[i].isFree() && it->second[i].handleCall(call)) {
return true;
}
}
}
return false;
} void addEmployee(Employee &employee) {
employees[employee.getLevel()].push_back(employee);
}
private:
map<int, vector<Employee> > employees;
};