Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save brettsanders/5918611 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
# puts "/Users/apprentice/Dropbox/DBC-Chicago/Grasshoppers/brettsanders/week_2/\ | |
# wednesday/p5_oo_inheritance.rb" | |
# puts "- - - - - - - - -" | |
class Car | |
@@WHEELS = 4 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Bus | |
attr_reader :passengers | |
def initialize(args) | |
@color = args[:color] | |
@wheels = args[:wheels] | |
@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 brake | |
@status = :stopped | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
def needs_gas? | |
return [true,true,true,false].sample | |
end | |
end | |
class Motorbike | |
@@WHEELS = 2 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
@speed = :fast | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,false,false,false].sample | |
end | |
def weave_through_traffic | |
@status = :driving_like_a_crazy_person | |
end | |
end | |
# Part 1: Write tests Write a FULL set of tests to test this code (at least 10 tests). Make sure all the tests pass BEFORE refactoring the code. | |
my_car = Car.new(color:"red") | |
gas_for_car = my_car.needs_gas? | |
puts | |
puts "5 CAR TESTS" | |
puts "(1) car stores color" if my_car.instance_eval { @color } == "red" | |
puts "(2) cars has 4 wheels" if my_car.instance_eval { @wheels } == 4 | |
puts "(3) car drives" if my_car.drive == :driving | |
puts "(4) car brakes" if my_car.brake == :stopped | |
puts "(5) car knows if needs gas" if ((gas_for_car == true) || (gas_for_car == false)) | |
my_bus = Bus.new(color:"yellow",wheels:4,num_seats:30,fare:5) | |
25.times { my_bus.admit_passenger('passenger',6) } | |
gas_for_bus = my_bus.needs_gas? | |
stop_requested = my_bus.stop_requested? | |
puts | |
puts "9 BUS TESTS" | |
puts "(1) bus has 4 wheels" if my_bus.instance_eval { @wheels } == 4 | |
puts "(2) bus stores color" if my_bus.instance_eval { @color } == "yellow" | |
puts "(3) bus drives" if my_bus.drive == :driving | |
puts "(4) bus brakes" if my_bus.brake == :stopped | |
puts "(5) bus knows if needs gas" if ((gas_for_bus == true) || (gas_for_bus == false)) | |
puts "(6) bus has num_seats" if my_bus.instance_eval { @num_seats } | |
puts "(7) bus has passengers" if my_bus.passengers.count == 25 | |
puts "(8) bus has fare" if my_bus.instance_eval { @fare } == 5 | |
puts "(9) bus knows if stop requested" if stop_requested == true || stop_requested == false | |
# motorbike = Motorbike.new | |
# Part 2: Refactoring with Inheritance Now factor out a base class called Vehicle that incorporates the shared behavior of the three sub classes. Your final code will have a base class and the three subclasses. Make sure all your tests still pass. | |
# Part 3: Get Creative Add some additional functionality to the base class and at least one of the subclasses. Write tests for this functionality. | |
# Optional: Extra Credit Refactor using Composition instead of Inheritance. Verify that all your tests still pass. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment