Created
December 4, 2014 14:49
-
-
Save Jberczel/05f54b2a8ce66e32e4f1 to your computer and use it in GitHub Desktop.
Cracking the Code Interview 7.2 (OOP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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(). | |
class CallHandler | |
attr_reader :product_manager, :tech_lead, :freshers, :calls | |
def initialize(args={}) | |
@product_manager = args[:product_manager] | |
@tech_lead = args[:tech_lead] | |
@freshers = args[:freshers] | |
@calls = [] | |
end | |
def get_call_handler | |
employees_by_rank.each do |emp| | |
return emp if emp.free? | |
end | |
end | |
def employees_by_rank | |
[freshers, tech_lead, product_manager].flatten | |
end | |
def route_incoming(call) | |
employee = get_call_handler | |
if employee | |
employee.status = :busy | |
call.employee = employee | |
else | |
calls << call | |
end | |
end | |
end | |
class Employee | |
attr_reader :name, :title | |
attr_accessor :status | |
def initialize(name, title) | |
@name = name | |
@title = title | |
@status = :free | |
end | |
def free? | |
status == :free | |
end | |
end | |
class Call | |
attr_accessor :employee | |
def initialize(caller, message) | |
@caller = caller | |
@message = message | |
end | |
end | |
### Example | |
employee1 = Employee.new('John', 'Product Manager') | |
employee2 = Employee.new('Jim', 'Tech Lead') | |
employee3 = Employee.new('James', 'Fresher') | |
employee4 = Employee.new('Joe', 'Fresher') | |
call_center = CallHandler.new( | |
product_manager: employee1, | |
tech_lead: employee2, | |
freshers: [employee3, employee4] ) | |
call1 = Call.new("Mr T", "My WoW account isn't working.") | |
call2 = Call.new("Mr Kim", "Issue with login...") | |
call_center.route_incoming(call1) | |
call_center.route_incoming(call2) | |
call_center.employees_by_rank.each do |emp| | |
puts "#{emp.name}: #{emp.status}" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment