Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Employee
- attr_accessor :rank, :in_a_call
- def initialize(in_a_call=false)
- @in_a_call = in_a_call
- @rank = nil
- end
- def call_received
- @in_a_call = true
- end
- def call_completed
- @in_a_call = false
- # notify CallHandler for availability
- end
- end
- class Respondent < Employee
- def initialize
- super
- @rank = 0
- end
- end
- class Manager < Employee
- def initialize
- super
- @rank = 1
- end
- end
- class Director < Employee
- def initialize
- super
- @rank = 2
- end
- end
- class Call
- attr_accessor :rank
- def initialize(rank=0)
- @rank = rank
- end
- end
- class CallHandler
- attr_accessor :employees, :respondents, :managers, :directors, :call_que
- # Initialize 10 respondents, 4 managers, and 2 directors.
- def initialize(respondents=10, managers=4, directors=2)
- @call_que = []
- @respondents, @managers, @directors = [], [], []
- respondents.times { @respondents << Respondent.new }
- managers.times { @managers << Manager.new }
- directors.times { @directors << Director.new }
- end
- def list_employees
- @employees = [@respondents, @managers, @directors]
- end
- def incoming_call(rank=0)
- @call_que << Call.new(rank)
- match_handler
- end
- def match_handler
- @call_que.empty? ? "there is currently no waiting caller" : @current_caller = @call_que.shift
- if @current_caller.rank == 0
- handler = @respondents.find {|respondent| respondent.in_a_call == false && respondent.rank == 0}
- handler.call_received
- elsif @current_caller.rank <= 1
- @managers.find {|manager| manager.in_a_call == false && manager.rank == 1}
- handler.call_received
- elsif @current_caller.rank <= 2
- @director.find {|director| director.in_a_call == false && director.rank == 2}
- handler.call_received
- else
- @call_que.unshift(@current_caller)
- puts "We're sorry, all personnels are busy at the moment thank you for your patients and please stay on the line"
- end
- end
- end
Add Comment
Please, Sign In to add comment