Guest User

Untitled

a guest
Jan 2nd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.03 KB | None | 0 0
  1. class Employee
  2.   attr_accessor :rank, :in_a_call
  3.  
  4.   def initialize(in_a_call=false)
  5.     @in_a_call = in_a_call
  6.     @rank = nil
  7.   end
  8.  
  9.   def call_received
  10.     @in_a_call = true
  11.   end
  12.  
  13.   def call_completed
  14.     @in_a_call = false
  15.     # notify CallHandler for availability
  16.   end
  17. end
  18.  
  19. class Respondent < Employee
  20.   def initialize
  21.     super
  22.     @rank = 0
  23.   end
  24. end
  25.  
  26. class Manager < Employee
  27.   def initialize
  28.     super
  29.     @rank = 1
  30.   end
  31. end
  32.  
  33. class Director < Employee
  34.   def initialize
  35.     super
  36.     @rank = 2
  37.   end
  38. end
  39.  
  40.  
  41. class Call
  42.   attr_accessor :rank
  43.  
  44.   def initialize(rank=0)
  45.     @rank = rank
  46.   end
  47. end
  48.  
  49.  
  50. class CallHandler
  51.   attr_accessor :employees, :respondents, :managers, :directors, :call_que
  52.  
  53.   # Initialize 10 respondents, 4 managers, and 2 directors.
  54.   def initialize(respondents=10, managers=4, directors=2)
  55.     @call_que = []
  56.  
  57.     @respondents, @managers, @directors = [], [], []
  58.  
  59.     respondents.times { @respondents << Respondent.new }
  60.     managers.times { @managers << Manager.new }
  61.     directors.times { @directors << Director.new }
  62.   end
  63.  
  64.   def list_employees
  65.     @employees = [@respondents, @managers, @directors]
  66.   end
  67.  
  68.   def incoming_call(rank=0)
  69.     @call_que << Call.new(rank)
  70.     match_handler
  71.   end
  72.  
  73.   def match_handler
  74.     @call_que.empty? ? "there is currently no waiting caller" : @current_caller = @call_que.shift
  75.  
  76.     if @current_caller.rank == 0
  77.       handler = @respondents.find {|respondent| respondent.in_a_call == false && respondent.rank == 0}
  78.       handler.call_received
  79.     elsif @current_caller.rank <= 1
  80.       @managers.find {|manager| manager.in_a_call == false && manager.rank == 1}
  81.       handler.call_received
  82.     elsif @current_caller.rank <= 2
  83.       @director.find {|director| director.in_a_call == false && director.rank == 2}
  84.       handler.call_received
  85.     else
  86.       @call_que.unshift(@current_caller)
  87.       puts "We're sorry, all personnels are busy at the moment thank you for your patients and please stay on the line"
  88.     end
  89.   end
  90. end
Add Comment
Please, Sign In to add comment