Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
January 3, 2016 21:59
-
-
Save Lydiakoller/8525395 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
class Vehicle | |
attr_reader :color, :wheels, :status, :speed | |
def initialize(args) | |
@color = args[:color] | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
end | |
class Car < Vehicle | |
def initialize(args) | |
super | |
@wheels = 4 | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :passengers | |
def initialize(args) | |
super | |
@wheels = args[:wheels] | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
end | |
def drive | |
return self.brake if stop_requested? | |
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 | |
def initialize(args) | |
super | |
@wheels = 2 | |
end | |
def drive | |
@speed = :fast | |
end | |
def needs_gas? | |
return [true,false,false,false].sample | |
end | |
def weave_through_traffic | |
@status = :driving_like_a_crazy_person | |
end | |
end | |
# Bus tests | |
schoolbus = Bus.new :wheels => 6, :num_seats => 30, :fare => 2, :color => "purple" | |
schoolbus.drive | |
puts schoolbus.status | |
puts schoolbus.stop_requested? | |
puts schoolbus.admit_passenger("Marky Mark", 3) | |
# puts schoolbus.admit_passenger("Big Bird", 1) | |
puts schoolbus.needs_gas? | |
puts schoolbus.color | |
# Motorbike tests | |
coolbike = Motorbike.new :color => "silver" | |
puts coolbike.color | |
puts coolbike.wheels | |
puts coolbike.drive | |
puts coolbike.status | |
puts coolbike.weave_through_traffic | |
puts coolbike.status | |
#car tests | |
mycar = Car.new :color => "red" | |
puts mycar.wheels | |
mycar.brake | |
puts mycar.status | |
puts mycar.needs_gas? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment