Created
June 24, 2009 10:40
-
-
Save mmower/135146 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
# ruby beginner elevator program | |
# We might break this stuff up into more classes as the need to separate state and | |
# behaviour of different entities emerges, for now we keep it simple and imagine | |
# the state to be that of the building as a whole. | |
# | |
# new code added by calvin... work in progress, still figuring how to make it all work | |
class Building | |
GROUND_FLOOR = 0 | |
USER_FLOOR = gets.chomp # MM: You're using a constant here, did you mean to? | |
attr_reader :elevator_on_floor | |
attr_accessor :elevator_target_floor, :new_floor | |
def initialize | |
@elevator_on_floor = GROUND_FLOOR | |
@elevator_target_floor = GROUND_FLOOR | |
@new_floor = USER_FLOOR # MM: I'd look at passing this value into initialize instead | |
end | |
def call_elevator_to_floor( new_floor ) | |
# Check that the call floor is legal (in a real elevator you wouldn't have buttons for illegal floors but this elevator isn't real! | |
# If the new floor is different to the current floor, tell the elevator where to go | |
x = 10 # In this case the number of floors in the building doesn't change | |
# but is, any way, a feature of the building and not the method | |
# Consider whether this should be a constant in the class | |
puts "The elevator does not go to that floor" if x > 10 | |
if x < 10 | |
puts "going to floor #{x}" | |
end | |
# MM: The logic of the above statements looks confused to me. You have two | |
# MM: independent 'if' tests of the same variable, consider if..else instead | |
# MM: This method should also set the state in response to calling the elevator | |
# MM: shouldn't it? | |
# MM: This very much suggests to me that the Elevator itself is a separate entity | |
# MM: which should have it's own state & behaviour. Maybe you should break out | |
# MM: Elevator into a new class that the Building class makes use of. | |
end | |
def time_passes | |
# Is the elevator on the floor it has been called to? | |
# If not move it one floor towards the target floor | |
# If the elevator has reached it's target floor, print a message | |
if @new_floor == @elevator_target_floor | |
puts "elevator has arrived at your floor" | |
end | |
end | |
def output_state | |
# print out where the elevator is | |
# if the elevator isn't at it's target floor print out whether it's heading up or down | |
puts "floor " + 0.to_s + " is where the elevator is at" | |
if @elevator_on_floor != @new_floor | |
puts "elevator is going up" # MM: but what if it's going down? | |
end | |
end | |
my_building = Building.new | |
# | |
# This part is the "game" | |
# | |
# On each move you can either call the elevator to a floor by typing in the floor number | |
# or simply allow the elevator to carry on its journey. Use ^d to exit (do you know why that | |
# works?) | |
# | |
while line = gets | |
line.chomp! | |
if line =~ /d+/ | |
puts my_building.call_elevator_to_floor( line.to_i ) | |
end | |
puts time_passes | |
puts output_state # MM: since both of these methods use 'puts' to output to the console | |
# MM: you don't need the puts here. Just call the methods | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment