Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save rwarbelow/5918525 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
### PART 1 (write tests) | |
class Car | |
@@WHEELS = 4 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Bus | |
attr_reader :passengers | |
def initialize(args) | |
@color = args[:color] | |
@wheels = args[:wheels] | |
@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 brake | |
@status = :stopped | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
def needs_gas? | |
return [true,true,true,false].sample | |
end | |
end | |
class Motorbike | |
@@WHEELS = 2 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
@speed = :fast | |
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 | |
car = Car.new({:color => "red"}) | |
p car.drive == :driving | |
p car.brake == :stopped | |
p car.needs_gas? == true | |
bus = Bus.new({:color => "blue", :wheels => 6, :num_seats => 25, :fare => 3}) | |
p bus.drive | |
p bus.admit_passenger("rachel", 5) == ["rachel"] | |
p bus.admit_passenger("caitlin", 4) == ["rachel", "caitlin"] | |
p bus.brake == :stopped | |
p bus.stop_requested? | |
p bus.needs_gas? | |
motorbike = Motorbike.new({:color => "black"}) | |
p motorbike.drive == :fast | |
p motorbike.brake == :stopped | |
p motorbike.needs_gas? | |
p motorbike.weave_through_traffic == :driving_like_a_crazy_person | |
### PART 2 (refactoring with inheritance) and PART 3 (get creative) | |
class Vehicle | |
attr_reader :status, :color, :speed | |
def initialize(args) | |
@color = args[:color] | |
@status = :stopped | |
@speed = 0 | |
end | |
def drive | |
@status = :driving | |
accelerate | |
end | |
def brake | |
@status = :stopped | |
@speed = 0 | |
end | |
def needs_gas? | |
return [true, true, true, false].sample | |
end | |
def accelerate | |
@speed += 10 | |
end | |
def decelerate | |
if @speed == 0 | |
"The bus cannot decelerate because it is already stopped!" | |
else | |
@speed -= 10 | |
end | |
end | |
end | |
class Car < Vehicle | |
attr_reader :wheels | |
@@WHEELS = 4 | |
def initialize(args) | |
super(args) | |
@wheels = @@WHEELS | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :fare, :num_seats, :passengers, :wheels | |
def initialize(args) | |
super(args) | |
@wheels = args[:wheels] | |
@fare = args[:fare] | |
@num_seats = args[:num_seats] | |
@passengers = [] | |
end | |
def stop_requested? | |
return [true, false].sample | |
end | |
def drive | |
return self.brake if stop_requested? | |
@status = :driving | |
self.accelerate | |
end | |
def admit_passenger(passenger, money_paid) | |
if money_paid < fare | |
puts "You do not have enough money to get on the bus. Sorry!" | |
elsif passengers.size >= num_seats | |
puts "The bus is too full. We cannot admit another passenger. Sorry!" | |
else | |
passengers << passenger | |
puts "Welcome aboard, #{passenger}!" | |
end | |
end | |
end | |
class Motorbike < Vehicle | |
attr_reader :wheels | |
@@WHEELS = 2 | |
def initialize(args) | |
super(args) | |
@wheels = @@WHEELS | |
end | |
def weave_through_traffic | |
status = :driving_like_a_crazy_person | |
end | |
end | |
motorbike = Motorbike.new({:color => "red"}) | |
p motorbike.wheels == 2 | |
p motorbike.speed == 0 | |
p motorbike.drive == 10 | |
p motorbike.speed == 10 | |
p motorbike.drive == 20 | |
p motorbike.weave_through_traffic == :driving_like_a_crazy_person | |
p motorbike.status == :driving | |
p motorbike.decelerate == 10 | |
p motorbike.speed == 10 | |
p motorbike.brake == 0 | |
p motorbike.status == :stopped | |
p motorbike.needs_gas? | |
bus = Bus.new({:color => "blue", :wheels => 6, :num_seats => 25, :fare => 3}) | |
p bus.wheels == 6 | |
p bus.speed == 0 | |
p bus.stop_requested? | |
bus.admit_passenger("rachel", 5) == "Welcome aboard, rachel!" | |
bus.admit_passenger("caitlin", 5) == "Welcome aboard, caitlin!" | |
p bus.admit_passenger("kylin", 2) == "You do not have enough money to get on the bus. Sorry!" | |
p bus.decelerate | |
p bus.brake == 0 | |
p bus.status == :stopped | |
p bus.needs_gas? | |
car = Car.new({:color => "red"}) | |
p car.wheels == 4 | |
p car.speed == 0 | |
p car.drive == 10 | |
p car.speed == 10 | |
p car.drive == 20 | |
p car.drive == 30 | |
p car.status == :driving | |
p car.decelerate == 20 | |
p car.brake == 0 | |
p car.status == :stopped | |
p car.needs_gas? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment