4
\$\begingroup\$

I am looking for tips on improving my implementation of an OOP design problem in Python3

Here is the problem (paraphrasing to save space):

There is a call center with three levels of employees - Respondent, Manager and Director. A call that goes to the center propagates as Respondent -> Manager -> Director depending on whether or not

a) The respondent is free and can handle the call

b) The manager is free and can handle the call

The call center needs to have a dispatchCall() method that assigns the call to the first available employee

Here is the code

#! /usr/bin/env python3
from abc import ABCMeta, abstractmethod
from random import random
class CallCenter():
 """
 A call center class that will be inherited from by the director
 Attributes:
 name: Name of the employee
 number: Mobile phone number of the employee
 level: Define level of the employee - respondent, manager or director
 """
 __metaclass__ = ABCMeta
 def __init__(self, name, number, level):
 self.name = name
 self.number = number
 self.level = level
 def canEmployeeHandle(self):
 canHandle = random()
 if canHandle > 0.5:
 return True
 else:
 return False
 def isEmployeeBusy(self):
 busy = True if random() <= 0.5 else False
 return busy
 @staticmethod
 def dispatchCall():
 return respondent.answerCall()
 @abstractmethod
 def answerCall(self):
 """Print a string indicating that this employee is currently
 answering the call or propogate up the chain"""
 pass
class Director(CallCenter):
 """
 A class definition for the director who will be the third in chain
 to answer the call
 """
 def answerCall(self):
 print("The director is now on call with you")
class Manager(Director):
 """
 A class definition for the manager who will be the second in chain to
 answer the call
 """
 def answerCall(self):
 if not self.isEmployeeBusy() and self.canEmployeeHandle():
 print("The manager is answering your call")
 else:
 super(Manager, self).answerCall()
class Respondent(Manager):
 canHandle = random() #class variable to check if respondent can handle call
 """
 A class definition for the respondent who will be the first in chain
 to answer the call
 """
 def answerCall(self):
 if not self.isEmployeeBusy() and self.canEmployeeHandle():
 print("The respondent is answering your call")
 else:
 super(Respondent, self).answerCall()
if __name__ == "__main__":
 director = Director("Alex", "+17654736791", "director")
 manager = Manager("Unai", "+331577285782", "manager")
 respondent = Respondent("Mark", "+16574872817", "respondent")
 CallCenter.dispatchCall()
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 20, 2017 at 11:43
\$\endgroup\$
4
  • \$\begingroup\$ "A call center class that will be inherited from by the manager" Are you sure that's what you're doing here? \$\endgroup\$ Commented Jul 20, 2017 at 12:29
  • \$\begingroup\$ @Mast - Yeah, sorry that was supposed to read inherited from by the director. I've updated it \$\endgroup\$ Commented Jul 20, 2017 at 20:11
  • \$\begingroup\$ Is it possible to be a director and a manager, or respondent and a manager, at the same time? \$\endgroup\$ Commented Jul 20, 2017 at 20:19
  • \$\begingroup\$ @Peilonrayz - No, those three are mutually exclusive roles \$\endgroup\$ Commented Jul 20, 2017 at 20:38

1 Answer 1

2
\$\begingroup\$

Segragated the responsilibites of call center into call center and employee

from abc import ABCMeta, abstractmethod
from random import random
class CallCenter(metaclass=ABCMeta):
 """
 A call center class that will be inherited from by the director
 Attributes:
 name: Name of the employee
 number: Mobile phone number of the employee
 level: Define level of the employee - respondent, manager or director
 """
 @classmethod
 def dispatchCall(self, cls):
 return cls.answerCall()
class Employee(metaclass=ABCMeta):
 def __init__(self, name, number, level):
 self.name = name
 self.number = number
 self.level = level
 def canEmployeeHandle(self):
 canHandle = random()
 if canHandle > 0.5:
 return True
 else:
 return False
 def isEmployeeBusy(self):
 busy = True if random() <= 0.5 else False
 return busy
 @abstractmethod
 def answerCall(self):
 """Print a string indicating that this employee is currently
 answering the call or propogate up the chain"""
 pass
class Director(Employee):
 """
 A class definition for the director who will be the third in chain
 to answer the call
 """
 def answerCall(self):
 print("The director is now on call with you")
class Manager(Director):
 """
 A class definition for the manager who will be the second in chain to
 answer the call
 """
 def answerCall(self):
 if not self.isEmployeeBusy() and self.canEmployeeHandle():
 print("The manager is answering your call")
 else:
 print("Manager is busy, escalating the call to the Director")
 super(Manager, self).answerCall()
class Respondent(Manager):
 canHandle = random() # class variable to check if respondent can handle call
 """
 A class definition for the respondent who will be the first in chain
 to answer the call
 """
 def answerCall(self):
 if not self.isEmployeeBusy() and self.canEmployeeHandle():
 print("The respondent is answering your call")
 else:
 print("Employee is busy, escalating the call to the Manager")
 super(Respondent, self).answerCall()
if __name__ == "__main__":
 director = Director("Alex", "+17654736791", "director")
 manager = Manager("Unai", "+331577285782", "manager")
 respondent = Respondent("Mark", "+16574872817", "respondent")
 CallCenter.dispatchCall(respondent)
 enter code here
answered Jul 10, 2020 at 12:05
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.