Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save keccers/5918619 to your computer and use it in GitHub Desktop.
This file contains 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
# encoding: utf-8 | |
class Vehicle | |
attr_accessor :color, :clean, :status | |
attr_reader :needs_gas, :wheels | |
def initialize (color) | |
@color = color | |
@status = :stopped | |
@needs_gas = false | |
@wheels = 4 | |
@clean = 100 | |
end | |
def drive | |
self.clean = clean - 10 | |
self.status = :driving | |
end | |
def brake | |
self.status = :stopped | |
end | |
def is_clean? | |
clean >= 50 | |
end | |
def wash | |
self.clean = 100 | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Car < Vehicle | |
end | |
class Bus < Vehicle | |
attr_reader :num_seats, :fare | |
attr_accessor :passengers | |
def initialize(color, num_seats, fare) | |
super (color) | |
@num_seats = num_seats | |
@fare = fare | |
@passengers=[] | |
end | |
def drive | |
if stop_requested? != true | |
self.clean = clean - 10 | |
self.status = :driving | |
else | |
self.brake | |
self.clean = clean - 10 | |
end | |
end | |
def admit_passenger(passenger,money) | |
passengers << passenger if money >= fare && num_seats > passengers.length | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
def needs_gas? | |
return [true,true,true,false].sample | |
end | |
def make_an_announcement | |
"(ノ`Д ́)ノ PLEASE DO NOT SING R.KELLY AT THE TOP OF YOUR LUNGS ON THE BUS. I REPEAT, PLEASE DO NOT SING R.KELLY AT THE TOP OF YOUR LUNGS ON THE BUS. (ノ`Д ́)ノ" | |
end | |
end | |
class Motorbike < Vehicle | |
@@WHEELS = 2 | |
attr_accessor :speed | |
def initialize(color) | |
super | |
@wheels = @@WHEELS | |
@speed = :cruisin | |
end | |
def drive | |
super | |
self.speed = :fast | |
end | |
def needs_gas? | |
return [true,false,false,false].sample | |
end | |
def weave_through_traffic | |
self.status = :driving_like_a_crazy_person | |
self.speed = :lightning_fast | |
end | |
end | |
#Tests: Class car | |
corvette = Car.new('red') | |
puts corvette.color | |
puts corvette.wheels == 4 | |
puts corvette.drive | |
puts corvette.drive | |
puts corvette.brake | |
puts corvette.needs_gas? | |
puts "My car has #{corvette.wheels} and is a goregous #{corvette.color}." | |
puts corvette.color == 'green' #false | |
puts corvette.clean | |
puts corvette.wash | |
puts "Is my car clean? #{corvette.is_clean?}" | |
puts "---CLASS BREAK----" | |
#Tests: Class bus | |
cta_damen = Bus.new('white', 2, 2.25) | |
puts cta_damen.drive | |
puts cta_damen.status | |
cta_damen.admit_passenger('old lady', 2.25) | |
cta_damen.admit_passenger('skater kid', 2.25) | |
cta_damen.admit_passenger('trust fund crust punk kid', 0) | |
cta_damen.admit_passenger('mayor emanuel', 20) | |
puts "This bus has #{cta_damen.passengers} on it." | |
puts cta_damen.drive | |
puts "This is Damen and Milwaukee. Would you like to stop? #{cta_damen.stop_requested?}." | |
puts cta_damen.drive | |
puts cta_damen.make_an_announcement | |
puts cta_damen.clean | |
puts cta_damen.is_clean? | |
puts "Does the bus need gas? -- #{cta_damen.needs_gas?}." | |
puts cta_damen.wash | |
puts cta_damen.clean | |
puts "---CLASS BREAK----" | |
#Tests: Class motorbike | |
harley = Motorbike.new('green') | |
puts "My brand new motorbike is the most beautiful shade of #{harley.color}!" | |
puts harley.drive | |
puts harley.drive | |
puts harley.brake | |
puts "Hey, does your motorbike need gas?--#{harley.needs_gas?}" | |
puts harley.weave_through_traffic | |
puts harley.brake | |
puts harley.wheels == 2 | |
puts harley.status | |
harley.drive | |
puts harley.status | |
puts harley.status == :driving | |
puts harley.clean | |
puts harley.wash | |
puts harley.wheels |
This file contains 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 Car | |
@@WHEELS = 4 | |
attr_accessor :color | |
attr_reader :wheels | |
def initialize(color) | |
@color = 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(color, num_seats, fare) | |
@color = color | |
@wheels = 4 | |
@num_seats = num_seats | |
@fare = fare | |
@passengers=[] | |
end | |
def drive | |
puts @status = :driving | |
return self.brake if stop_requested? | |
return @status | |
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 | |
attr_accessor :color | |
@@WHEELS = 2 | |
def initialize(color) | |
@color = 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 | |
#Tests: Class car | |
corvette = Car.new('red') | |
puts corvette.color | |
puts corvette.wheels | |
puts corvette.drive | |
puts corvette.drive | |
puts corvette.brake | |
puts corvette.needs_gas? | |
puts "My car has #{corvette.wheels} and is a goregous #{corvette.color}." | |
puts " " | |
#Tests: Class bus | |
cta_damen = Bus.new('white', 500, 2.25) | |
puts cta_damen.drive | |
cta_damen.admit_passenger('old lady', 2.25) | |
cta_damen.admit_passenger('skater kid', 2.25) | |
cta_damen.admit_passenger('trust fund crust punk kid', 0) | |
cta_damen.admit_passenger('mayor emanuel', 20) | |
puts "This bus has #{cta_damen.passengers} on it." | |
puts cta_damen.drive | |
puts "This is Damen and Milwaukee. Would you like to stop? #{cta_damen.stop_requested?}." | |
puts cta_damen.drive | |
puts "Does the bus need gas? -- #{cta_damen.needs_gas?}." | |
puts " " | |
#Tests: Class motorbike | |
harley = Motorbike.new('green') | |
puts "My brand new motorbike is the most beautiful shade of #{harley.color}!" | |
puts harley.drive | |
puts harley.drive | |
puts harley.brake | |
puts "Hey, does your motorbike need gas?--#{harley.needs_gas?}" | |
puts harley.weave_through_traffic | |
puts harley.brake |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment