设计一个Call Center

约束和假设

  • 在Call Center中会有几种类型的雇员?
    Operator, supervisor, director
  • 我们可以假设 Operator 总是得到初始化 call 吗?
  • 如果没有可用的Operators 或者 Operator 不能处理call, 这个call会去到 supervisor那里吗?
  • 如果没有可用的 supervisors, 或者 supervisor 不能处理call, 这个call 直接去 director哪里吗?
  • 我们可以假设 dircetor 可以处理所有的 call 吗?
  • 如果没有人可以回复call会发生什么?
    call 会被放进队列
  • 我们需要去处理 ‘VIP’ call吗?当我们放某人到一线?
    不需要
  • 我们可以假设输入是有效的或者我们不需要去验证他们吗?
    假设他们是有效的

编码实现

from abc import ABCMeta, abstractmethod
from collections import deque
from enum import Enum

class Rank(Enum):
	OPERATOR = 0
	SUPERVISOR = 1
	DIRECTOR = 2

class Employee(metaclass=ABCMeta):

	def __init__(self, employee_id, name, rank, call_center):
		self.employee_id = emplyee_id
		self.name = name
		self.rank = rank
		self.call = None
		slef.call_center = call_center

	def take_call(self, call):
		'''Assume the employee will always successfully take the call'''
		self.call = call
		self.call.employee = self
		self.call.state = CallState.IN_PROGRESS

	def complete_call(self):
		self.call.state = CallState.COMPLETE
		self.call_center.notify_call_completed(self.call)

	@abstractmethod
	def escalate_call(self):
		pass

	def _escalate_call(self):
		self.call.state = CallState.READY
		call = self.call
		self.call = None
		self.call_center.notify_call_escalates(call)

class Operator(Employee):
	
	def __init__(self, emplyee_id, name):
		super(Operator, self).__init__(emplyee_id, name, Rank.OPERATOR)

	def escalate_call(self):
		self.call.level = Rank.SUPERVISOR
		self._escalate_call()

class Supervisor(Employee):
	
	def __init__(self, employee_id, name):
		super(Operator, self).__init__(employee_id, name, Rank.DIRECTOR)

	def escalate_call(self):
		raise NotImplemented('Directors must be able to handle any call')

class CallState(Enum):
	READY = 0
	IN_PROGRESS = 1
	COMPLETE = 2

class Call(object):
	def __init__(self, rank):
		self.state = CallState.READY
		self.rank = rank
		self.employee = None

class CallCenter(object):
	def __init__(self, operators, supervisors, directors):
		self.operators = operators
		self.supervisors = supervisors
		self.directors = directors
		self.queued_calls = deque()

	def dispatch_call(self,call):
		if call.rank not in (Rank.OPERATOR, Rank.SUPERVISOR, Rank.DIRECTOR):
			raise ValueError('Invalid call rank: {}'.format(call.rank))
		if call.rank == Rank.OPERATOR:
			employee = self._dispatch_call(call, self.operators)
		if call.rank == Rank.SUPERVISOR or employee is None:
			employee = self._dispatch_call(call, self.supervisors)
		if call.rank == Rank.DIRECTOR or employee is None:
			employee = self._dispatch_call(call, self.directors)
		if employee is None:
       self.queued_calls.append(call)

	def _dispatch_call(self, call, employee):
		for employee in employees:
			if employee.call is None:
				employee.take_call(call)
				return employee
			return None

	def notify_call_escalated(self,call): #...
	def notify_call_completed(self, call): #...
	def dispatch_queued_call_to_newly_freed_eplyee(self, call, employee): #...
			

你可能感兴趣的:(算法,python,设计)