Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save rwarbelow/5918525 to your computer and use it in GitHub Desktop.
Revisions
-
rwarbelow revised this gist
Jul 6, 2013 . 1 changed file with 159 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ ### PART 1 (write tests) class Car @@WHEELS = 4 def initialize(args) @@ -40,24 +42,20 @@ def stop_requested? 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 @@ -67,4 +65,157 @@ def needs_gas? def weave_through_traffic @status = :driving_like_a_crazy_person end end car = Car.new({:color => "red"}) p car.drive == :driving p car.brake == :stopped p car.needs_gas? == true bus = Bus.new({:color => "blue", :wheels => 6, :num_seats => 25, :fare => 3}) p bus.drive p bus.admit_passenger("rachel", 5) == ["rachel"] p bus.admit_passenger("caitlin", 4) == ["rachel", "caitlin"] p bus.brake == :stopped p bus.stop_requested? p bus.needs_gas? motorbike = Motorbike.new({:color => "black"}) p motorbike.drive == :fast p motorbike.brake == :stopped p motorbike.needs_gas? p motorbike.weave_through_traffic == :driving_like_a_crazy_person ### PART 2 (refactoring with inheritance) and PART 3 (get creative) class Vehicle attr_reader :status, :color, :speed def initialize(args) @color = args[:color] @status = :stopped @speed = 0 end def drive @status = :driving accelerate end def brake @status = :stopped @speed = 0 end def needs_gas? return [true, true, true, false].sample end def accelerate @speed += 10 end def decelerate if @speed == 0 "The bus cannot decelerate because it is already stopped!" else @speed -= 10 end end end class Car < Vehicle attr_reader :wheels @@WHEELS = 4 def initialize(args) super(args) @wheels = @@WHEELS end end class Bus < Vehicle attr_reader :fare, :num_seats, :passengers, :wheels def initialize(args) super(args) @wheels = args[:wheels] @fare = args[:fare] @num_seats = args[:num_seats] @passengers = [] end def stop_requested? return [true, false].sample end def drive return self.brake if stop_requested? @status = :driving self.accelerate end def admit_passenger(passenger, money_paid) if money_paid < fare puts "You do not have enough money to get on the bus. Sorry!" elsif passengers.size >= num_seats puts "The bus is too full. We cannot admit another passenger. Sorry!" else passengers << passenger puts "Welcome aboard, #{passenger}!" end end end class Motorbike < Vehicle attr_reader :wheels @@WHEELS = 2 def initialize(args) super(args) @wheels = @@WHEELS end def weave_through_traffic status = :driving_like_a_crazy_person end end motorbike = Motorbike.new({:color => "red"}) p motorbike.wheels == 2 p motorbike.speed == 0 p motorbike.drive == 10 p motorbike.speed == 10 p motorbike.drive == 20 p motorbike.weave_through_traffic == :driving_like_a_crazy_person p motorbike.status == :driving p motorbike.decelerate == 10 p motorbike.speed == 10 p motorbike.brake == 0 p motorbike.status == :stopped p motorbike.needs_gas? bus = Bus.new({:color => "blue", :wheels => 6, :num_seats => 25, :fare => 3}) p bus.wheels == 6 p bus.speed == 0 p bus.stop_requested? bus.admit_passenger("rachel", 5) == "Welcome aboard, rachel!" bus.admit_passenger("caitlin", 5) == "Welcome aboard, caitlin!" p bus.admit_passenger("kylin", 2) == "You do not have enough money to get on the bus. Sorry!" p bus.decelerate p bus.brake == 0 p bus.status == :stopped p bus.needs_gas? car = Car.new({:color => "red"}) p car.wheels == 4 p car.speed == 0 p car.drive == 10 p car.speed == 10 p car.drive == 20 p car.drive == 30 p car.status == :driving p car.decelerate == 20 p car.brake == 0 p car.status == :stopped p car.needs_gas? -
dbc-challenges renamed this gist
Jun 25, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dbc-challenges created this gist
Jun 11, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ 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