Skip to content

Instantly share code, notes, and snippets.

@bragboy
Created December 31, 2015 12:43
Show Gist options
  • Save bragboy/bd4fc504c419532c0d0f to your computer and use it in GitHub Desktop.
Save bragboy/bd4fc504c419532c0d0f to your computer and use it in GitHub Desktop.
# models/hotel.rb
class Hotel
attr_accessor :floors
def initialize(num_floors, num_of_mc, num_of_sc, controller)
@floors = []
num_floors.times{ @floors << Floor.new(num_of_mc, num_of_sc) }
controller.add_observer(self)
end
def current_status
status = ''
@floors.each_with_index do |floor, i|
status << "Floor #{i+1}" + "\n"
status << floor.current_status
end
status
end
def update(floor, sub_corridor, movement)
if(floor < 1 || floor > @floors.size)
puts 'There is no such floor. INVALID INPUT'
return
end
current_floor = @floors[floor-1]
sub_corridors = current_floor.sub_corridors
if(sub_corridor < 1 || sub_corridor > sub_corridors.size)
puts "There is no such sub corridor for floor #{floor}. INVALID INPUT"
return
end
sc = sub_corridors[sub_corridor-1]
if movement #First detected movement
sc.move
sc.lights.each {|light| light.turn_on}
elsif !movement && sc.was_moving? #Trigger only during a state change
sc.unmove
sc.lights.each {|light| light.turn_off}
end
current_floor.check_and_reset_power
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment