Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 24, 2015 05:19
-
-
Save jsedwards/6749721 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 | |
| def initialize(args) | |
| @color = args[:color] | |
| end | |
| def brake | |
| @status = :stopped | |
| end | |
| def drive | |
| @status = :driving | |
| end | |
| def ok_to_text? | |
| @status != :driving | |
| end | |
| end | |
| class Car < Vehicle | |
| @@WHEELS = 4 | |
| def initialize(args) | |
| super | |
| @wheels = args[:wheels] | |
| end | |
| def needs_gas? | |
| return [true,true,false].sample | |
| end | |
| end | |
| class Bus < Vehicle | |
| attr_reader :passengers | |
| 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 | |
| def drive | |
| super | |
| @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 | |
| #TESTS | |
| def assert(expression, message) | |
| raise message unless expression | |
| end | |
| assert(red_rocket = Car.new({color: "red"}), "new car can be created") | |
| assert(red_rocket.drive, "cars can drive") | |
| assert(red_rocket.brake, "cars can brake") | |
| #assert(!!red_rocket.needs_gas? == red_rocket.needs_gas?, " weird test cars either need gas or they don't") | |
| assert(big_bird = Bus.new(color: "yellow", fare: 2, num_seats: 48, wheels: 10), "new bus can be created") | |
| assert(big_bird.passengers == [], "new buses don't have any passengers") | |
| assert(big_bird.drive == :stopped || :driving, "buses drive") | |
| assert(big_bird.admit_passenger("Joe",5), "buses admit passengers with more money than the fare") | |
| assert(big_bird.admit_passenger("Cheap Skate",1)==nil, "buses dont' admit passengers with less money than the fare") | |
| assert(harley = Motorbike.new(color: "black"), "new bikes can be created") | |
| assert(harley.drive == :fast, "bikes can drive, they go fast") | |
| assert(harley.weave_through_traffic == :driving_like_a_crazy_person, "bikes weave through traffic like a crazy person") | |
| #part 3 tests | |
| assert(harley.color == "black", "bikes can have colors") | |
| assert(red_rocket.color == "red", "bikes can have colors") | |
| assert(big_bird.color == "yellow", "bikes can have colors") | |
| assert(red_rocket.ok_to_text? == true, "ok for car to text when stopped") | |
| red_rocket.drive | |
| assert(red_rocket.ok_to_text? == false, "not ok for car to text when driving") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment