Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save kevbuchanan/5918531 to your computer and use it in GitHub Desktop.
Revisions
-
dbc-apprentice revised this gist
Jul 3, 2013 . 1 changed file with 120 additions and 66 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,70 +1,124 @@ class Vehicle attr_reader :wheels, :color, :gas_mileage attr_accessor :status, :speed def initialize(args) @wheels = args[:wheels] @color = args[:color] @status = :stopped @speed = 0 @gas_mileage = [true, false].sample end def drive self.status = :driving accelerate end def accelerate self.speed += 10 unless self.status == :stopped end def slow_down return brake unless speed > 10 self.speed -= 10 end def brake self.status = :stopped self.speed = 0 end def needs_gas? gas_mileage.sample end end class Car < Vehicle def initialize(args) super @wheels = 4 @gas_mileage = [true, true, false] end end class Bus < Vehicle attr_reader :num_seats attr_accessor :passengers, :fare def initialize(args) super @gas_mileage = [true, true, true, false] @num_seats = args[:num_seats] @fare = args[:fare] @passengers=[] end def drive return brake if stop_requested? super end def admit_passenger(passenger,money) passengers << passenger if money >= fare && !full? end def full? passengers.size == num_seats end def stop_requested? [true,false].sample end end class Motorbike < Vehicle def initialize(args) super @wheels = 2 @gas_mileage = [true, false, false, false] end def accelerate self.speed += 30 unless self.status == :stopped end def weave_through_traffic self.status = :driving_like_a_crazy_person end end # Tests puts "My Car" myCar = Car.new(color: 'blue') puts "My Car needs gas." if myCar.needs_gas? myCar.drive puts "My car is #{myCar.status} #{myCar.speed} mph" myCar.brake puts "My car is #{myCar.status}" puts "My Car needs gas." if myCar.needs_gas? puts "My Bus" myBus = Bus.new(color: 'yellow', wheels: 6, num_seats: 20, fare: 2) puts "My Bus has #{myBus.num_seats} seats and #{myBus.wheels} wheels." puts "My Bus needs gas." if myBus.needs_gas? myBus.drive puts "My Bus is #{myBus.status}" myBus.brake myBus.admit_passenger('Tom', 3) myBus.admit_passenger('Joe', 2) myBus.admit_passenger('Mike', 1) puts "My Bus has #{myBus.passengers.size} passengers." 18.times { myBus.admit_passenger('passenger', 2) } puts myBus.passengers.size puts "My Bus full." if myBus.full? puts "My Bus needs gas." if myBus.needs_gas? puts "My Motorbike" myBike = Motorbike.new(color: 'black') puts "My Bike is #{myBike.color}." myBike.drive puts "My Bike is #{myBike.status} #{myBike.speed} mph." myBike.brake puts "My Bike needs gas." if myBike.needs_gas? myBike.weave_through_traffic puts "My Bike is #{myBike.status} #{myBike.speed}." -
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