Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:29
-
-
Save ryanhburbank/5919065 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 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = 4 | |
@status = :stopped | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
def pulled_over? | |
@status == :driving ? [false,false,false,false,true].sample : false | |
end | |
end | |
class Car < Vehicle | |
#attr_reader :color, :wheels, :status | |
#@@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 | |
class Bus < Vehicle | |
attr_reader :passengers,:num_seats, :fare | |
def initialize(args) | |
super | |
@wheels = args[:wheels] | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
end | |
def drive | |
super | |
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 < Vehicle | |
attr_reader :speed | |
def initialize(args) | |
super | |
#@color = args[:color] | |
@wheels = 2 | |
end | |
def drive | |
super | |
#@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 | |
def crashed? | |
if @status == :driving_like_a_crazy_person | |
[true,true,true,true,false,false,false].sample | |
elsif @status == :driving | |
[true,false,false,false,false,false,false].sample | |
else | |
false | |
end | |
end | |
end | |
test_car = Car.new(:color=>"blue") | |
test_bus = Bus.new(:color=>"yellow",:wheels=>18,:num_seats=>52,:fare=>10) | |
test_bike = Motorbike.new(:color=>"grey") | |
#Initialize Tests | |
puts "Test Car: Wheels = #{test_car.wheels}, Color = #{test_car.color}" | |
puts | |
puts "Test Bus: Wheels = #{test_bus.wheels}, Color = #{test_bus.color}, Num Seats = #{test_bus.num_seats}, Fare = #{test_bus.fare}, Passengers = #{test_bus.passengers}" | |
puts | |
puts "Test Bike: Wheels = #{test_bike.wheels}, Color= #{test_bike.color}" | |
puts | |
#Car Tests | |
puts "Test Car status = #{test_car.status}" | |
puts "Calling drive on car..." | |
test_car.drive | |
puts "Test Car status = #{test_car.status}" | |
puts "Calling test_car.pulled_over?...returns: #{test_car.pulled_over?}" | |
puts "Calling brake on car..." | |
test_car.brake | |
puts "Test Car status = #{test_car.status}" | |
puts "Calling test_car.needs_gas?...returns: #{test_car.needs_gas?}" | |
#Bus Tests | |
puts | |
puts "Test Bus status = #{test_bus.status}" | |
puts "Calling drive on bus..." | |
test_bus.drive | |
puts "Test Bus status = #{test_bus.status}" | |
puts "Calling brake on bus..." | |
test_bus.brake | |
puts "Test Bus status = #{test_bus.status}" | |
puts "Calling test_bus.needs_gas?...returns: #{test_bus.needs_gas?}" | |
test_bus.admit_passenger("Bob",9) | |
puts "Adding passenger with insufficent fare..." | |
puts "Current passengers: #{test_bus.passengers}" | |
test_bus.admit_passenger("Greg",15) | |
puts "Adding passenger with sufficent fare..." | |
puts "Current passengers: #{test_bus.passengers}" | |
puts "Calling test_bus.stop_requested?...returns: #{test_bus.stop_requested?}" | |
puts | |
#Bike Tests | |
puts "Test Motorbike status = #{test_bike.status}" | |
puts "Calling drive on bike..." | |
test_bike.drive | |
puts "Test Motorbike status = #{test_bike.status}, Test Motorbike speed = #{test_bike.speed}" | |
puts "Calling brake on bike..." | |
test_bike.brake | |
puts "Test Motorbike status = #{test_bike.status}" | |
puts "Calling test_bike.weave_through_traffic...return: #{test_bike.weave_through_traffic}" | |
test_bike.weave_through_traffic | |
puts "Calling test_bike.crashed?...returns: #{test_bike.crashed?}" | |
puts "Calling test_bike.needs_gas?...returns: #{test_bike.needs_gas?}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment