Design the classes and data structures for a call center

Imagine you have a call center with three levels of employees: fresher, technical lead (TL), product manager (PM). There can be multiple employees, but only one TL or PM. An incoming telephone call must be allocated to a fresher who is free. If a fresher can’t handle the call, he or she must escalate the call to technical lead. If the TL is not free or not able to handle it, then the call should be escalated to PM. Design the classes and data structures for this problem. Implement a method getCallHandler().

My initial thoughts:
Class diagram for a call center

My initial codes:

	public class Employee {
		protected boolean available = true;
		protected boolean canHandle = true;

		public boolean handle() {
			return available && canHandle;
		}

		public void busy() {
			available = false;
		}

		public void free() {
			available = true;
		}

		public boolean canHandle() {
			return canHandle;
		}

		public void setCanHandle(boolean canHandle) {
			this.canHandle = canHandle;
		}

	}

	public class CallCenter {
		private Employee productManager;
		private Employee technicalHead;
		private List<Employee> freshers;

		public Employee getCallHandler() {
			for (int i = 0; i < freshers.size(); ++i) {
				Employee fresher = freshers.get(i);
				if (fresher.handle()) {
					fresher.busy();
					return fresher;
				}
			}
			if (technicalHead != null && technicalHead.handle()) {
				technicalHead.busy();
				return technicalHead;
			}
			if (productManager != null && productManager.handle()) {
				productManager.busy();
				return productManager;
			}
			return null;
		}
	}

Solution:

	public class CallHandler {
		static final int LEVELS = 3; // we have 3 levels of employees
		static final int NUM_FRESHERS = 5; // we have 5 freshers
		ArrayList<Employee>[] employeeLevels = new ArrayList[LEVELS];
		// queues for each call’s rank
		Queue<Call>[] callQueues = new LinkedList[LEVELS];

		public CallHandler() {
			// constructor
		}

		Employee getCallHandler(Call call) {
			for (int level = call.rank; level < LEVELS - 1; level++) {
				ArrayList<Employee> employeeLevel = employeeLevels[level];
				for (Employee emp : employeeLevel) {
					if (emp.free) {
						return emp;
					}
				}
			}
			return null;
		}

		// routes the call to an available employee, or adds to a queue
		void dispatchCall(Call call) {
			// try to route the call to an employee with minimal rank
			Employee emp = getCallHandler(call);
			if (emp != null) {
				emp.ReceiveCall(call);
			} else {
				// place the call into queue according to its rank
				callQueues[call.rank].add(call);
			}
		}

		void getNextCall(Employee e) {
			// look for call for e’s rank
		}
	}

	class Call {
		int rank = 0; // minimal rank of employee who can handle this call

		public void reply(String message) {
			// reply
		}

		public void disconnect() {
			// disconnect
		}
	}

	class Employee {
		CallHandler callHandler;
		int rank; // 0- fresher, 1 - technical lead, 2 - product manager
		boolean free;

		Employee(int rank) {
			this.rank = rank;
		}

		void ReceiveCall(Call call) {
			// receive call
		}

		void CallHandled(Call call) {
			// call is complete
		}

		void CannotHandle(Call call) { // escalate call
			call.rank = rank + 1;
			callHandler.dispatchCall(call);
			free = true;
			callHandler.getNextCall(this); // look for waiting call
		}
	}

	class Fresher extends Employee {
		public Fresher() {
			super(0);
		}
	}

	class TechLead extends Employee {
		public TechLead() {
			super(1);
		}
	}

	class ProductManager extends Employee {
		public ProductManager() {
			super(2);
		}
	}

Comments:

  1. The way of handling abilities to handle the call is implemented by matching employee’s level and call’s level.
  2. Escalation is implemented by increment of the level of the call.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: