Skip to content

Instantly share code, notes, and snippets.

@fabianuribe
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 20, 2015 04:59
Show Gist options
  • Save fabianuribe/6074832 to your computer and use it in GitHub Desktop.
Save fabianuribe/6074832 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :status, :wheels
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
@status = :stopped
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def get_pulled_over
"Whooops, your #{self.class} got pulled over! Smells like trouble"
end
def crash!
@wheels = @wheels - 1
end
end
class Car < Vehicle
WHEELS = 4
def initialize(args)
super(args)
@wheels = WHEELS
end
def needs_gas?
return [true,true,false].sample
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super(args)
@color = args[:color]
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
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
def crash!
super
@wheels = @wheels - 1
end
end
class Motorbike < Vehicle
WHEELS = 2
def initialize(args)
super(args)
@wheels = WHEELS
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
# Testing set up
my_car = Car.new({color: 'red'})
my_bus = Bus.new({color: 'yellow', wheels: 6, num_seats: 55, fare: 3.56 })
my_bike = Motorbike.new({color: 'black'})
# Test car
puts 'Car tests'
puts my_car.drive == :driving
puts my_car.brake == :stopped
puts my_car.respond_to? :needs_gas?
puts [true,false].include?( my_car.needs_gas?)
# New car tests
puts my_car.wheels == 4
puts my_car.get_pulled_over == "Whooops, your Car got pulled over! Smells like trouble"
# Test bus
puts
puts 'Bus tests'
puts my_bus.respond_to? :drive
puts my_bus.brake == :stopped
puts my_bus.admit_passenger('Fabian', 1) == nil # Fare is not enought to get in the buss
puts my_bus.passengers == [] # Empty array of passengers
puts my_bus.admit_passenger('Fabian', 5) == ['Fabian']
puts my_bus.passengers == ['Fabian']
puts my_bus.respond_to? :needs_gas?
puts [true,false].include?( my_bus.needs_gas?)
# New Bus tests
puts my_bus.wheels == 6
puts my_bus.get_pulled_over == "Whooops, your Bus got pulled over! Smells like trouble"
my_bus.crash!
puts my_bus.wheels == 4
# Test Motorcycle
puts
puts 'Motorcycle tests'
puts my_bike.respond_to? :needs_gas?
puts my_bike.drive == :fast
puts my_bike.brake == :stopped
puts my_bike.weave_through_traffic == :driving_like_a_crazy_person
puts [true,false].include?( my_bus.needs_gas?)
# New Motorbike tests
puts my_bike.wheels == 2
puts my_bike.get_pulled_over == "Whooops, your Motorbike got pulled over! Smells like trouble"
my_bike.crash!
puts my_bike.wheels == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment