Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 10:29
-
-
Save jesseclay/5941320 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, :status | |
def initialize(args) | |
@wheels = self.wheels | |
@seats = self.seats | |
@status = self.status | |
@color = args.fetch(:color) {self.color} | |
@speed = self.speed | |
end | |
def seats | |
raise "No default number of seats for Vehicle class" | |
end | |
def wheels | |
4 | |
end | |
def color | |
return @color if @color != nil | |
raise "No default color for Vehicle class" | |
end | |
def status | |
return @status if @status != nil | |
:stopped | |
end | |
def speed | |
return @speed if @speed != nil | |
raise "No default speed for vehicle class" | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
end | |
class Car < Vehicle | |
def seats | |
5 | |
end | |
def color | |
"grey" | |
end | |
def speed | |
:moderate | |
end | |
def needs_gas | |
return [true,true,false].sample | |
end | |
end | |
car = Car.new({}) | |
puts car.color == "grey" | |
puts car.status == :stopped | |
puts car.wheels == 4 | |
puts car.seats == 5 | |
car.drive | |
puts car.status == :driving | |
class Bus < Vehicle | |
attr_reader :passengers | |
def initialize(args) | |
super(args) | |
@passengers = args.fetch(:passengers) {[]} | |
@fare = args.fetch(:fare) {raise "No fare amount provided"} | |
end | |
def speed | |
:slow | |
end | |
def wheels | |
6 | |
end | |
def color | |
return @color if @color != nil | |
"Red and white" | |
end | |
def seats | |
52 | |
end | |
def drive | |
return self.brake if stop_requested? | |
@status = :driving | |
end | |
def admit_passenger(passenger,money) | |
@passengers << passenger if money > @fare | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
def needs_gas? | |
return [true,true,true,false].sample | |
end | |
end | |
muni = Bus.new({:color => "Green", :fare => 2.00}) | |
puts muni.wheels == 6 | |
puts muni.seats == 52 | |
puts muni.color == "Green" | |
puts muni.status == :stopped | |
muni.drive | |
# p muni.admit_passenger("Tom Jones", 2.50) | |
class Motorbike < Vehicle | |
def speed | |
:fast | |
end | |
def wheels | |
2 | |
end | |
def seats | |
1 | |
end | |
def drive | |
@status = :driving | |
@speed = :fast | |
end | |
def needs_gas? | |
return [true,false,false,false].sample | |
end | |
def weave_through_traffic | |
@status = :driving_like_a_crazy_person | |
end | |
end | |
puts "Motorcycle: " | |
ducati = Motorbike.new({:color => "fire-red"}) | |
puts ducati.wheels == 2 | |
puts ducati.seats == 1 | |
puts ducati.color == "fire-red" | |
puts ducati.status == :stopped | |
ducati.drive | |
puts ducati.status == :driving | |
ducati.weave_through_traffic | |
puts ducati.status == :driving_like_a_crazy_person | |
# require 'minitest/spec' | |
# require 'minitest/autorun' | |
# describe Car do | |
# before :each do | |
# @car = Car.new({:color => 'red'}) | |
# end | |
# describe "#new" do | |
# it "takes one parameter and returns a Car object" do | |
# @car.must_be_instance_of Car | |
# end | |
# end | |
# describe "#drive" do | |
# it "returns :driving" do | |
# @car.drive.must_equal :driving | |
# end | |
# end | |
# describe "#break" do | |
# it "returns :stopped" do | |
# @car.brake.must_equal :stopped | |
# end | |
# end | |
# describe "#needs_gas" do | |
# it "returns true or false" do | |
# # @car.needs_gas.must_equal true || false | |
# end | |
# end | |
# end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment