Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 12:19
-
-
Save ideahed/5954074 to your computer and use it in GitHub Desktop.
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 characters
| class Vehicle | |
| def initialize(args) | |
| @colors = args[:color] | |
| @wheels = self.wheels || 4 #args[:wheels] || 4 | |
| end | |
| def drive | |
| @status = :driving | |
| end | |
| def brake | |
| @status = :stopped | |
| end | |
| def wheels | |
| raise NotImplementedError | |
| end | |
| def needs_gas? | |
| return self.refuel_frequency.sample | |
| end | |
| def refuel_frequency | |
| [true, true, false] | |
| end | |
| end | |
| class Car < Vehicle | |
| def wheels | |
| 4 | |
| end | |
| end | |
| class Bus < Vehicle | |
| attr_accessor :stop_request | |
| attr_reader :passengers | |
| def initialize(args) | |
| super | |
| @num_seats = args[:num_seats] | |
| @fare = args[:fare] | |
| @passengers=[] | |
| end | |
| def wheels | |
| 6 | |
| end | |
| def drive | |
| return self.brake if stop_request || stop_requested? | |
| super | |
| end | |
| def admit_passenger(passenger,money) | |
| @passengers << passenger if money >= @fare | |
| end | |
| def stop_requested? | |
| return [true,false].sample | |
| end | |
| def refuel_frequency | |
| [true,true,true,false] | |
| end | |
| end | |
| class Motorbike < Vehicle | |
| def wheels | |
| 2 | |
| end | |
| def drive | |
| super | |
| @speed = :fast | |
| end | |
| def refuel_frequency | |
| [true,false, false, false] | |
| end | |
| def weave_through_traffic | |
| @status = :driving_like_a_crazy_person | |
| end | |
| end | |
| # DRIVER CODE | |
| # Car Tests | |
| p mini = Car.new({:color => :black}) | |
| p mini.drive == :driving | |
| p mini.brake == :stopped | |
| p mini.needs_gas? | |
| # Bus Tests | |
| # intialize | |
| p 1, kx = Bus.new({:color => :orange, :num_seats => 40, :fare => 500}) | |
| p 2, kx.drive == :driving | |
| p 3, kx.brake == :stopped | |
| p 4, kx.needs_gas? | |
| p 5, kx.admit_passenger("Bill", 501) == ["Bill"] # kx.passenger[0] # adjust > ==> >= | |
| p 6, kx.admit_passenger("Joe", 490).nil? # can't add, not enough $$ | |
| p 7, kx.passengers.length == 1 # False | |
| p 8, kx.passengers | |
| kx.stop_request = true | |
| p 9, kx.drive == :stopped | |
| # Drive method | |
| # Brake Method | |
| # needs_gas? | |
| # Motorbike Tests | |
| # intialize | |
| p 1, kawasaki = Motorbike.new({:color => :black, :wheels => 2}) | |
| # Drive method | |
| p 2, kawasaki.drive == :fast | |
| # Brake Method | |
| p 3, kawasaki.brake == :stopped | |
| # needs_gas? | |
| p 4, kawasaki.needs_gas? | |
| p 5, kawasaki.weave_through_traffic == :driving_like_a_crazy_person | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment