Created
July 6, 2014 07:28
-
-
Save pvin/fc22f3b55575dcfa55ba to your computer and use it in GitHub Desktop.
ruby code for lift(elevator)
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
class Lift | |
GROUND_FLOOR = 1 | |
TOTAL_FLOOR = 10 | |
@@previous_floor =0 | |
TRAVEL_TIME = 3 | |
DO_DC = 10 | |
attr_reader :elevator_on_floor | |
attr_accessor :elevator_target_floor, :new_floor | |
def initialize | |
@elevator_target_floor = GROUND_FLOOR | |
end | |
def call_elevator_to_floor( new_floor,entry_count) | |
if new_floor <= TOTAL_FLOOR | |
puts "strated to floor #{new_floor}" | |
time_to_display = ((new_floor-@@previous_floor)*TRAVEL_TIME).abs | |
puts "time left to reach #{time_to_display} seconds" | |
@@previous_floor = new_floor | |
time_passes(new_floor,time_to_display,$entry_count) | |
else | |
puts 'Invalid floor enter between (1 -10)' | |
end | |
end | |
def time_passes(new_floor,time_to_display,entry_count) | |
if @new_floor != @elevator_target_floor | |
sleep time_to_display.abs | |
$entry_count-=1 | |
puts "elevator has arrived at #{new_floor}" | |
else | |
puts "elevator has arrived at your floor" | |
end | |
end | |
end | |
lift = Lift.new | |
puts 'Enter the floor number to reach(1-10)' | |
$entry_count =0 | |
while line = gets | |
$entry_count+=1 | |
line.chomp! | |
if line | |
puts lift.call_elevator_to_floor( line.to_i,$entry_count ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment