Skip to content

Instantly share code, notes, and snippets.

@cemmanuel1
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 18, 2015 11:29
Show Gist options
  • Save cemmanuel1/5775929 to your computer and use it in GitHub Desktop.
Save cemmanuel1/5775929 to your computer and use it in GitHub Desktop.
class Vehicle
@@INFLATED_WHEELS = 4
def initialize(args)
@color = args[:color]
@wheels_needed = 4
end
def drive
@status = :driving
end
def flat_tire?
if @@INFLATED_WHEELS < @wheels_needed
puts "Uh oh you have a flat, you are now: "
@status = :stopped
else
"Your tires are good to go"
end
end
def brake
@status = :stopped
end
end
class Car < Vehicle
def needs_gas?
return [true,true,false].sample
end
def speeding_ticket(speed_limit, your_speed)
if your_speed > speed_limit
p "You just got a speeding ticket!!!!"
end
end
end
class Bus < Vehicle
@@INFLATED_WHEELS = [4,4,4,4,4,4,3].sample
attr_reader :passengers
def initialize(args)
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
end
def drive
return self.brake if stop_requested?
@status = :driving
end
def admit_passenger(passenger,money)
@passengers << passenger if money > @fare
end
def stop_requested?
return [true,false].sample
end
def needs_gas?
return [true,true,true,false].sample
end
end
class Motorbike < Vehicle
@@INFLATED_WHEELS = [2,2,2,2,2,2,2,1].sample
def initialize (args)
@wheels_needed = 2
end
def drive
@speed = :fast
end
def set_num_wheels?(arg)
@@INFLATED_WHEELS = arg
end
def brake
@status = :stopped
end
def needs_gas?
return [true,false,false,false].sample
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
######### TESTS #############
motorcycle = Motorbike.new({:color => "blue"})
puts motorcycle.drive == :fast
puts motorcycle.weave_through_traffic == :driving_like_a_crazy_person
puts motorcycle.brake == :stopped
puts motorcycle.drive == :fast
puts motorcycle.flat_tire?
bus = Bus.new({:color => "purple", :num_seats => 30, :fare => 5})
puts bus.admit_passenger("Chantal", 9) == ["Chantal"]
puts bus.admit_passenger("Bill", 5) == nil
puts bus.brake == :stopped
puts bus.brake == :stopped
car = Car.new({:color => "yellow"})
puts car.brake == :stopped
puts car.drive == :driving
puts car.speeding_ticket(40, 100) == "You just got a speeding ticket!!!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment