Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:29
-
-
Save jmyers0022/5918654 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 :wheels, :color | |
attr_accessor :status | |
def initialize(args) | |
@wheels = args[:wheels] | |
@color = args[:color] | |
@status = :stopped | |
end | |
def drive | |
self.status = :driving | |
end | |
def brake | |
self.status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Car < Vehicle | |
@@WHEELS = 4 | |
attr_reader :status | |
def initialize(args) | |
super | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :passengers, :num_seats, :fare | |
attr_accessor :stop_requested | |
def initialize(args) | |
super | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
end | |
def drive | |
return self.brake if stop_requested? | |
super | |
end | |
def admit_passenger(passenger,money) | |
@passengers << passenger if money > @fare | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
end | |
class Motorbike < Vehicle | |
attr_reader :speed | |
@@WHEELS = 2 | |
def initialize(args) | |
super(args) | |
@speed = :slow | |
@wheels = @@WHEELS | |
end | |
def drive | |
@speed = :fast | |
super | |
end | |
def weave_through_traffic | |
@status = :driving_like_a_crazy_person | |
end | |
end | |
#vehicle = Vehicle.new(:color=>"black") | |
motorbike = Motorbike.new(:color=>"black") | |
car = Car.new(:color=>"blue") | |
bus = Bus.new(:color=>"red", :wheels=>12, :num_seats=>40, :fare=>40) | |
puts | |
puts | |
puts "=================================CAR TESTS=======================================" | |
puts "Testing initial car status Expecting: stopped || Got: #{car.status}" | |
car.drive | |
puts "Testing car.drive Expecting: driving || Got: #{car.status}" | |
car.brake | |
puts "Testing car.brake Expecting: stopped || Got: #{car.status}" | |
puts "Testing car.need_gas? Expecting: boolean || Got: #{car.needs_gas?}" | |
puts | |
puts "=================================BUS TESTS=======================================" | |
puts "Testing initial bus status Expecting: stopped || Got: #{bus.status}" | |
bus.drive | |
puts "Testing bus.drive Expecting: driving || Got: #{bus.status}#{"(Stop Requested)" if bus.status == :stopped}" | |
bus.brake | |
puts "Testing bus.status Expecting: stopped || Got: #{bus.status}" | |
puts "Testing bus.needs_gas? Expecting: boolean || Got: #{bus.needs_gas?}" | |
bus.admit_passenger("RoboCop", 50) #should definitely be admitted | |
bus.admit_passenger("Batman", 42) #should definitely be admitted | |
bus.admit_passenger("Nickelback", 30) #should definitely not be admitted | |
bus.admit_passenger("Bieber", 25) #should definitely not be admitted | |
puts "Testing bus.passenger count Expecting 2 || Got: #{bus.passengers.count}" | |
puts "Testing bus.passengers Expecting: Robocop, Batman || Got: #{bus.passengers}" | |
puts "Adding passengers to bus" | |
bus.admit_passenger("Emma Watson", 50) | |
bus.admit_passenger("Random Passerby", 41) | |
bus.admit_passenger("Man Who Can't Afford Ticket", 17) | |
puts "Testing bus.passenger count Expecting 4 || Got: #{bus.passengers.count}" | |
puts | |
puts "===============================MOTORBIKE TESTS===================================" | |
puts "Testing initial motorbike status Expecting: stopped || Got: #{motorbike.status}" | |
motorbike.drive | |
puts "Testing motorbike.drive Expecting: driving || Got: #{motorbike.status}" | |
motorbike.brake | |
puts "Testing motorbike.brake Expecting: stopped || Got: #{motorbike.status}" | |
puts "Testing motorbike.need_gas? Expecting: boolean || Got: #{motorbike.needs_gas?}" | |
puts "Testing motorbike.weaving Expecting: dlacp || Got: #{motorbike.weave_through_traffic}" | |
puts | |
puts | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment