Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 20, 2015 03:49
-
-
Save jeffchang/6066686 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
| # Part 4 -- Extra Credit (Refactor using Composition) | |
| module Vehicle | |
| attr_reader :status, :tickets | |
| def drive | |
| @status = :driving | |
| end | |
| def brake | |
| @status = :stopped | |
| end | |
| def crash | |
| @status = [:exploded, :totaled, :fender_bender, :not_a_scratch].sample | |
| end | |
| def traffic | |
| @status = :we_will_never_get_there | |
| end | |
| def see_cop | |
| @tickets += 1 | |
| end | |
| end | |
| class Car | |
| include Vehicle | |
| def initialize(args) | |
| @color = args[:color] | |
| @tickets = 0 | |
| @wheels = 4 | |
| end | |
| def needs_gas? | |
| return [true,true,false].sample | |
| end | |
| end | |
| class Bus | |
| include Vehicle | |
| attr_reader :passengers, :fare, :moolah | |
| def initialize(args) | |
| @color = args[:color] | |
| @tickets = 0 | |
| @wheels = args[:wheels] | |
| @num_seats = args[:num_seats] | |
| @fare = args[:fare] | |
| @passengers=[] | |
| @moolah = 0.00 | |
| end | |
| def drive | |
| return self.brake if stop_requested? | |
| @status = :driving | |
| end | |
| def admit_passenger(passenger,money) | |
| if money >= @fare | |
| @passengers << passenger | |
| money_collected | |
| end | |
| end | |
| def stop_requested? | |
| return [true,false].sample | |
| end | |
| def needs_gas? | |
| return [true,true,true,false].sample | |
| end | |
| def traffic | |
| @status = :taking_the_bus_lane | |
| end | |
| def money_collected | |
| @moolah += 2.50 | |
| end | |
| end | |
| class Motorbike | |
| include Vehicle | |
| def initialize(args) | |
| @color = args[:color] | |
| @tickets = 0 | |
| @wheels = 2 | |
| end | |
| def drive | |
| @status, @speed = :driving, :fast | |
| end | |
| def needs_gas? | |
| return [true,false,false,false].sample | |
| end | |
| def weave_through_traffic | |
| @status = :driving_like_a_crazy_person | |
| end | |
| def traffic | |
| weave_through_traffic | |
| end | |
| end | |
| # Part 1 -- Full Set of Tests | |
| puts | |
| puts "Part 1" | |
| puts "------" | |
| p bike = Motorbike.new({color: "Silver"}) | |
| p bike.drive == [:driving, :fast] | |
| p bike.brake == :stopped | |
| p bike.needs_gas? | |
| p bike.needs_gas? | |
| p bike.weave_through_traffic == :driving_like_a_crazy_person | |
| p BigYellow = Bus.new({ | |
| color: "Yellow", | |
| wheels: 18, | |
| num_seats: 56, | |
| fare: 2.50 | |
| }) | |
| p BigYellow.drive | |
| p BigYellow.drive | |
| p BigYellow.drive | |
| p BigYellow.drive | |
| p BigYellow.brake == :stopped | |
| p BigYellow.needs_gas? | |
| BigYellow.admit_passenger("Sam", 4.50) | |
| BigYellow.admit_passenger("Charlie", 4.50) | |
| BigYellow.admit_passenger("Priya", 2.50) # <== Broken part of existing code | |
| BigYellow.admit_passenger("Ruth", 0.50) | |
| BigYellow.admit_passenger("Ophelia", 6.50) | |
| BigYellow.admit_passenger("Carter", 1.50) | |
| p BigYellow.passengers | |
| p Lambourghini = Car.new({color: "Shimmering Red"}) | |
| p Lambourghini.drive == :driving | |
| p Lambourghini.brake == :stopped | |
| p Lambourghini.needs_gas? | |
| p Lambourghini.needs_gas? | |
| # Part 2 -- Refactoring with Inheritance | |
| # Refactored above | |
| # Part 3 -- Get Creative | |
| # Functionality to Base Class: | |
| puts | |
| puts "Part 3" | |
| puts "------" | |
| p bike | |
| p bike.traffic == :driving_like_a_crazy_person | |
| bike.see_cop | |
| bike.see_cop | |
| bike.see_cop | |
| bike.see_cop | |
| p (bike.tickets.to_s + " tickets received") | |
| p BigYellow | |
| p BigYellow.moolah | |
| p BigYellow.traffic == :taking_the_bus_lane | |
| p Lambourghini | |
| p Lambourghini.crash | |
| p Lambourghini.traffic == :we_will_never_get_there |
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 :status, :tickets | |
| def initialize(args) | |
| @color = args[:color] | |
| @tickets = 0 | |
| end | |
| def drive | |
| @status = :driving | |
| end | |
| def brake | |
| @status = :stopped | |
| end | |
| def crash | |
| @status = [:exploded, :totaled, :fender_bender, :not_a_scratch].sample | |
| end | |
| def traffic | |
| @status = :we_will_never_get_there | |
| end | |
| def see_cop | |
| @tickets += 1 | |
| end | |
| end | |
| class Car < Vehicle | |
| def initialize(args) | |
| super | |
| @wheels = 4 | |
| end | |
| def needs_gas? | |
| return [true,true,false].sample | |
| end | |
| end | |
| class Bus < Vehicle | |
| attr_reader :passengers, :fare, :moolah | |
| def initialize(args) | |
| super | |
| @wheels = args[:wheels] | |
| @num_seats = args[:num_seats] | |
| @fare = args[:fare] | |
| @passengers=[] | |
| @moolah = 0.00 | |
| end | |
| def drive | |
| return self.brake if stop_requested? | |
| @status = :driving | |
| end | |
| def admit_passenger(passenger,money) | |
| if money >= @fare | |
| @passengers << passenger | |
| money_collected | |
| end | |
| end | |
| def stop_requested? | |
| return [true,false].sample | |
| end | |
| def needs_gas? | |
| return [true,true,true,false].sample | |
| end | |
| def traffic | |
| @status = :taking_the_bus_lane | |
| end | |
| def money_collected | |
| @moolah += 2.50 | |
| end | |
| end | |
| class Motorbike < Vehicle | |
| def initialize(args) | |
| super | |
| @wheels = 2 | |
| end | |
| def drive | |
| @status, @speed = :driving, :fast | |
| end | |
| def needs_gas? | |
| return [true,false,false,false].sample | |
| end | |
| def weave_through_traffic | |
| @status = :driving_like_a_crazy_person | |
| end | |
| def traffic | |
| weave_through_traffic | |
| end | |
| end | |
| # Part 1 -- Full Set of Tests | |
| puts | |
| puts "Part 1" | |
| puts "------" | |
| p bike = Motorbike.new({color: "Silver"}) | |
| p bike.drive == [:driving, :fast] | |
| p bike.brake == :stopped | |
| p bike.needs_gas? | |
| p bike.needs_gas? | |
| p bike.weave_through_traffic == :driving_like_a_crazy_person | |
| p BigYellow = Bus.new({ | |
| color: "Yellow", | |
| wheels: 18, | |
| num_seats: 56, | |
| fare: 2.50 | |
| }) | |
| p BigYellow.drive | |
| p BigYellow.drive | |
| p BigYellow.drive | |
| p BigYellow.drive | |
| p BigYellow.brake == :stopped | |
| p BigYellow.needs_gas? | |
| BigYellow.admit_passenger("Sam", 4.50) | |
| BigYellow.admit_passenger("Charlie", 4.50) | |
| BigYellow.admit_passenger("Priya", 2.50) # <== Broken part of existing code | |
| BigYellow.admit_passenger("Ruth", 0.50) | |
| BigYellow.admit_passenger("Ophelia", 6.50) | |
| BigYellow.admit_passenger("Carter", 1.50) | |
| p BigYellow.passengers | |
| p Lambourghini = Car.new({color: "Shimmering Red"}) | |
| p Lambourghini.drive == :driving | |
| p Lambourghini.brake == :stopped | |
| p Lambourghini.needs_gas? | |
| p Lambourghini.needs_gas? | |
| # Part 2 -- Refactoring with Inheritance | |
| # Refactored above | |
| # Part 3 -- Get Creative | |
| # Functionality to Base Class: | |
| puts | |
| puts "Part 3" | |
| puts "------" | |
| p bike | |
| p bike.traffic == :driving_like_a_crazy_person | |
| bike.see_cop | |
| bike.see_cop | |
| bike.see_cop | |
| bike.see_cop | |
| p (bike.tickets.to_s + " tickets received") | |
| p BigYellow | |
| p BigYellow.moolah | |
| p BigYellow.traffic == :taking_the_bus_lane | |
| p Lambourghini | |
| p Lambourghini.crash | |
| p Lambourghini.traffic == :we_will_never_get_there |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment