class Elevatore::Elevator
Attributes
current_floor[R]
current_time[R]
direction[R]
people[R]
Public Class Methods
new(opts={})
click to toggle source
# File lib/elevatore/elevator.rb, line 4 def initialize opts={} @current_floor = opts[:current_floor] || 0 @people = opts[:people] @current_time = 0 @direction = opts[:direction] || Elevatore::DIRECTION_UP if @direction != Elevatore::DIRECTION_UP && @direction != Elevatore::DIRECTION_DOWN raise ArgumentError.new("Your direction is misleading the elevator") end end
Public Instance Methods
print_exit_times()
click to toggle source
# File lib/elevatore/elevator.rb, line 14 def print_exit_times clear_building! output_people! end
Private Instance Methods
clear_building!()
click to toggle source
# File lib/elevatore/elevator.rb, line 20 def clear_building! while @people.any?{|person| has_inside?(person) || is_being_called?(person)} people_exit! people_enter! set_direction! travel! end end
has_delivered?(person)
click to toggle source
# File lib/elevatore/elevator.rb, line 70 def has_delivered? person has_picked_up?(person) && !person.exit_at.nil? end
has_inside?(person)
click to toggle source
# File lib/elevatore/elevator.rb, line 62 def has_inside? person has_picked_up?(person) && !has_delivered?(person) end
has_picked_up?(person)
click to toggle source
# File lib/elevatore/elevator.rb, line 66 def has_picked_up? person !person.pickup_at.nil? end
is_being_called?(person)
click to toggle source
# File lib/elevatore/elevator.rb, line 58 def is_being_called? person !has_picked_up?(person) && !has_delivered?(person) end
needs_to_roll?()
click to toggle source
# File lib/elevatore/elevator.rb, line 54 def needs_to_roll? ((Elevatore::TOP_LEVEL >= @current_floor+1 && @direction == Elevatore::DIRECTION_UP) || (Elevatore::LOWER_LEVEL <= @current_floor-1 && @direction == Elevatore::DIRECTION_DOWN)) && ((!next_pickup.nil? && people_inside.size != 0) || next_pickup != @current_floor) end
next_drop()
click to toggle source
# File lib/elevatore/elevator.rb, line 86 def next_drop if @direction==Elevatore::DIRECTION_UP people_inside.max_by{|person| person.to }&.to else people_inside.min_by{|person| person.to }&.to end end
next_pickup()
click to toggle source
# File lib/elevatore/elevator.rb, line 82 def next_pickup people_to_enter.min_by{|person| person.enter_at }&.from end
output_people!()
click to toggle source
# File lib/elevatore/elevator.rb, line 48 def output_people! people.sort_by(&:exit_at).each do |person| puts "#{person.exit_at}m P#{person.id}" end end
people_enter!()
click to toggle source
# File lib/elevatore/elevator.rb, line 40 def people_enter! people_to_enter.each do |person| if @current_floor == person.from && person.pickup_at.nil? && person.enter_at == @current_time.round(1) person.pickup_at = @current_time.round(1) end end end
people_exit!()
click to toggle source
# File lib/elevatore/elevator.rb, line 34 def people_exit! people_inside.each do |person| person.exit_at = @current_time.round(1) if @current_floor == person.to end end
people_inside()
click to toggle source
# File lib/elevatore/elevator.rb, line 74 def people_inside @people.select{|person| has_inside?(person) } end
people_to_enter()
click to toggle source
# File lib/elevatore/elevator.rb, line 78 def people_to_enter @people.select{|person| is_being_called?(person) } end
set_direction!()
click to toggle source
# File lib/elevatore/elevator.rb, line 94 def set_direction! return if (next_pickup.nil? && people_inside.size == 0) || (next_drop.nil? && people_to_enter.size == 0) if @direction==Elevatore::DIRECTION_UP unless (next_pickup.to_i > @current_floor && people_inside.size == 0) || (next_drop.to_i > @current_floor) @direction=Elevatore::DIRECTION_DOWN end else unless (next_pickup.to_i < @current_floor && people_inside.size == 0) || (next_drop.to_i < @current_floor) @direction=Elevatore::DIRECTION_UP end end end
travel!()
click to toggle source
# File lib/elevatore/elevator.rb, line 29 def travel! @current_time += Elevatore::SPEED @current_floor += @direction if needs_to_roll? end