Created
June 25, 2009 04:26
-
-
Save patrickberkeley/135682 to your computer and use it in GitHub Desktop.
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
# Simplified version of calvin stephens (http://github.com/Calvin4) elevator program. | |
class Building | |
# Ask the user for the top floor of the building | |
def get_top_floor | |
puts "Please enter the top floor: " | |
@top_floor = gets.chomp!.to_i | |
end | |
# Ask the user for the floor they want to go to | |
def get_floor | |
puts "Please enter the floor you want to go to: " | |
input = gets.chomp! | |
validate_floor(input) | |
end | |
# Check the input to make sure it's an number | |
def validate_floor(input) | |
if input =~ /(\d+)/ | |
new_floor = input.to_i | |
go_to_floor(new_floor) | |
else | |
puts "Please enter an integer between 0 and " + @top_floor.to_s + "." | |
get_floor | |
end | |
end | |
# Go to the floor the user entered if it exists in the building | |
def go_to_floor(new_floor) | |
if new_floor > @top_floor || new_floor <= 0 | |
puts "The elevator only goes to floors 1-" + @top_floor.to_s + "." | |
get_floor | |
elsif new_floor < 10 && new_floor > 0 | |
puts "Going to floor #{new_floor}" + "." | |
output_state(new_floor) | |
get_floor | |
end | |
end | |
# Let the user know they've arrived at their destination | |
def output_state(new_floor) | |
puts "You've arrived at floor " + new_floor.to_s + "." | |
end | |
end | |
# Instantiate a building | |
my_building = Building.new | |
# Get input for the top floor | |
my_building.get_top_floor | |
# Get input for floor to travel to | |
my_building.get_floor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment