Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 20, 2015 03:49
-
-
Save clay-whitley/6066247 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 | |
def initialize(args) | |
@status = :stopped | |
@color = args[:color] | |
@gas_capacity = 10 | |
@gas = 5 | |
end | |
def drive | |
unless needs_gas? | |
@gas -= 1 | |
@status = :driving | |
else | |
return "You're out of gas!" | |
end | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
@gas == 0 ? true : false | |
end | |
def fill_tank | |
@gas = @gas_capacity | |
end | |
end | |
class Car < Vehicle | |
def initialize(args) | |
super | |
@wheels = 4 | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :passengers | |
def initialize(args) | |
super | |
@wheels = 8 | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
@gas_capacity = 30 | |
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 | |
def initialize(args) | |
super | |
@wheels = 2 | |
end | |
def drive | |
super | |
@speed = :fast | |
end | |
def weave_through_traffic | |
@status = :driving_like_a_crazy_person | |
end | |
end | |
car_instance = Car.new color: "Red" | |
bus_instance = Bus.new color: "White", num_seats: 20, fare: 3 | |
bike_instance = Motorbike.new color: "Black" | |
puts "Car tests:" | |
p car_instance.drive == :driving | |
p car_instance.brake == :stopped | |
gas_status = car_instance.needs_gas? | |
p gas_status == true || gas_status == false | |
puts "Bus tests:" | |
p bus_instance.passengers == [] | |
bus_status = bus_instance.drive | |
p bus_status == :driving || bus_status == :stopped | |
p bus_instance.admit_passenger("rich", 5) == ["rich"] | |
p bus_instance.passengers == ["rich"] | |
p bus_instance.admit_passenger("poor", 2).nil? | |
p bus_instance.brake == :stopped | |
stop_status = bus_instance.stop_requested? | |
p stop_status == true || stop_status == false | |
gas_status = bus_instance.needs_gas? | |
p gas_status == true || gas_status == false | |
puts "Bike tests:" | |
p bike_instance.drive == :fast | |
p bike_instance.brake == :stopped | |
gas_status = bike_instance.needs_gas? | |
p gas_status == false || gas_status == true | |
p bike_instance.weave_through_traffic == :driving_like_a_crazy_person |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment