Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 20, 2015 03:49
-
-
Save Uzmani/6066180 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 | |
| attr_reader :color, :wheels, :status, :passengers, :speeds | |
| def initialize(args) | |
| @color = args[:color] | |
| @status = :stopped | |
| @speed = :normal | |
| end | |
| def drive | |
| @status = :driving | |
| end | |
| def brake | |
| @status = :stopped | |
| end | |
| def need_gas? | |
| return [true,true,false].sample | |
| end | |
| def ran_out_gas | |
| @status = :stopped | |
| end | |
| end | |
| class Car < Vehicle | |
| attr_reader :color, | |
| @@WHEELS = 4 | |
| def initialize(args) | |
| super | |
| @wheels = @@WHEELS | |
| end | |
| def nitrous_boost | |
| if @status == :stopped | |
| puts "You might want to start Driving first...duh" | |
| else | |
| @speed = :superfast | |
| end | |
| end | |
| end | |
| class Bus < Vehicle | |
| # attr_reader :passengers, :status, :color | |
| def initialize(args) | |
| super | |
| @wheels = args[:wheels] | |
| @num_seats = args[:num_seats] | |
| @fare = args[:fare] | |
| @passengers=[] | |
| end | |
| def drive | |
| return self.brake if stop_requested? | |
| super | |
| 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 | |
| end | |
| class Motorbike < Vehicle | |
| @@WHEELS = 2 | |
| attr_reader :color | |
| def initialize(args) | |
| super | |
| @wheels = @@WHEELS | |
| end | |
| def drive | |
| @update = :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 | |
| ########### Write tests that pass | |
| p motorcycle = Motorbike.new(:color => "black") | |
| p schoolbus = Bus.new(:color => "yellow", :wheels => 8, :num_seats =>30, :fare => 2) | |
| p my_car = Car.new(:color => "green") | |
| # puts "Tests for BUS:" | |
| # schoolbus.admit_passenger("Jeremy", 2) | |
| # p schoolbus.passengers == ["Jeremy"] | |
| # p schoolbus.status == :stopped | |
| # p schoolbus.color == "yellow" | |
| # puts "Tests for CAR:" | |
| p my_car.color == "green" | |
| p my_car.drive == :driving | |
| #p my_car.ran_out_gas == :stopped | |
| p my_car.nitrous_boost == :superfast | |
| p my_car.speed == :superfast | |
| # puts "Tests for MotorBIKE:" | |
| # p motorcycle.color == "black" | |
| # p motorcycle.status == :stopped | |
| # p motorcycle.drive == :fast | |
| # p motorcycle.status == :driving | |
| # p motorcycle.brake == :stopped | |
| # p motorcycle.weave_through_traffic == :driving_like_a_crazy_person | |
| # p motorcycle.wheels |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment