Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save kevbuchanan/5918531 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, :gas_mileage | |
attr_accessor :status, :speed | |
def initialize(args) | |
@wheels = args[:wheels] | |
@color = args[:color] | |
@status = :stopped | |
@speed = 0 | |
@gas_mileage = [true, false].sample | |
end | |
def drive | |
self.status = :driving | |
accelerate | |
end | |
def accelerate | |
self.speed += 10 unless self.status == :stopped | |
end | |
def slow_down | |
return brake unless speed > 10 | |
self.speed -= 10 | |
end | |
def brake | |
self.status = :stopped | |
self.speed = 0 | |
end | |
def needs_gas? | |
gas_mileage.sample | |
end | |
end | |
class Car < Vehicle | |
def initialize(args) | |
super | |
@wheels = 4 | |
@gas_mileage = [true, true, false] | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :num_seats | |
attr_accessor :passengers, :fare | |
def initialize(args) | |
super | |
@gas_mileage = [true, true, true, false] | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
end | |
def drive | |
return brake if stop_requested? | |
super | |
end | |
def admit_passenger(passenger,money) | |
passengers << passenger if money >= fare && !full? | |
end | |
def full? | |
passengers.size == num_seats | |
end | |
def stop_requested? | |
[true,false].sample | |
end | |
end | |
class Motorbike < Vehicle | |
def initialize(args) | |
super | |
@wheels = 2 | |
@gas_mileage = [true, false, false, false] | |
end | |
def accelerate | |
self.speed += 30 unless self.status == :stopped | |
end | |
def weave_through_traffic | |
self.status = :driving_like_a_crazy_person | |
end | |
end | |
# Tests | |
puts "My Car" | |
myCar = Car.new(color: 'blue') | |
puts "My Car needs gas." if myCar.needs_gas? | |
myCar.drive | |
puts "My car is #{myCar.status} #{myCar.speed} mph" | |
myCar.brake | |
puts "My car is #{myCar.status}" | |
puts "My Car needs gas." if myCar.needs_gas? | |
puts "My Bus" | |
myBus = Bus.new(color: 'yellow', wheels: 6, num_seats: 20, fare: 2) | |
puts "My Bus has #{myBus.num_seats} seats and #{myBus.wheels} wheels." | |
puts "My Bus needs gas." if myBus.needs_gas? | |
myBus.drive | |
puts "My Bus is #{myBus.status}" | |
myBus.brake | |
myBus.admit_passenger('Tom', 3) | |
myBus.admit_passenger('Joe', 2) | |
myBus.admit_passenger('Mike', 1) | |
puts "My Bus has #{myBus.passengers.size} passengers." | |
18.times { myBus.admit_passenger('passenger', 2) } | |
puts myBus.passengers.size | |
puts "My Bus full." if myBus.full? | |
puts "My Bus needs gas." if myBus.needs_gas? | |
puts "My Motorbike" | |
myBike = Motorbike.new(color: 'black') | |
puts "My Bike is #{myBike.color}." | |
myBike.drive | |
puts "My Bike is #{myBike.status} #{myBike.speed} mph." | |
myBike.brake | |
puts "My Bike needs gas." if myBike.needs_gas? | |
myBike.weave_through_traffic | |
puts "My Bike is #{myBike.status} #{myBike.speed}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment