Skip to content

Instantly share code, notes, and snippets.

@Githerdone
Forked from dbc-challenges/P5: OO Inheritance.rb
Created June 13, 2013 18:14
Show Gist options
  • Save Githerdone/5776010 to your computer and use it in GitHub Desktop.
Save Githerdone/5776010 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :wheels, :status, :gas
def initialize(args)
@color = args[:color]
@speed = :normal
@wheels = nil
@status = :normal_speed
@gas = :full
end
def drive
@status = :driving
end
def drive
@status = :driving
@speed = :fast
end
def brake
@status = :stopped
end
def needs_gas?
tank = [:empty, :half, :quarter, :empty, :half, :quarter ].sample.to_s
@gas = tank
return tank + " Tank"
end
def fill_tank!
@gas = :full
end
end
class Car < Vehicle
@@WHEELS = 4
def initialize(args)
super
@color = args[:color]
@wheels = @@WHEELS
end
end
class Bus < Vehicle
attr_reader :passengers, :num_seats, :fare
def initialize(args)
super
@color = args[:color]
@wheels = args[:wheels]
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
end
def admit_passenger(passenger,money)
@passengers << passenger if money == @fare
end
def stop_requested?
should_stop = [true,false].sample
if should_stop
self.brake
else
return false
end
end
end
class Motorbike < Vehicle
@@WHEELS = 2
def initialize(args)
super
@color = args[:color]
@wheels = @@WHEELS
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
# ***************************************************************************************
# Fleet Testing
fleet = []
p "Motorbike Fleet"
fleet << Motorbike.new(:color => "Yellow")
p fleet[0].color == "Yellow"
fleet[0].weave_through_traffic
p "This motorbike is..." + fleet[0].status.to_s
p "How much gas do you have? -- I have a #{fleet[0].needs_gas?}."
if fleet[0].gas == :empty
fleet[0].fill_tank!
p "Your tank is now full"
end
fleet << Motorbike.new(:color => "Blue")
p fleet[1].color == "Blue"
p "This motorbike is driving..." + fleet[1].status.to_s
p "This bike has #{fleet[1].wheels} wheels."
p "How much gas do you have? -- I have a #{fleet[1].needs_gas?}."
if fleet[1].gas == :empty
fleet[1].fill_tank!
p "Your tank is now full"
end
p "Bus Fleet"
fleet << Bus.new(:color => "White", :wheels => 10, :num_seats => 32, :fare => 1.50)
a = "a"
until fleet[2].passengers.size == fleet[2].num_seats
fleet[2].admit_passenger("person " + a, 1.50)
a = a.next
end
p "This bus is carrying #{fleet[2].passengers.size} passengers"
p "A passenger requested a stop. What's going to happen?"
if fleet[2].stop_requested? == false
p "A passenger did not really request a stop and was just joking. The bus is still driving #{fleet[2].status}"
else
p "A passenger did request a stop and the bus is now #{fleet[2].status}."
end
p "How much gas do you have? -- I have a #{fleet[2].needs_gas?}."
if fleet[2].gas == :empty
fleet[2].fill_tank!
p "Your tank is now full"
end
p fleet[2].color == "White"
p fleet[2].wheels == 10
p fleet[2].num_seats == 32
p fleet[2].fare == 1.50
fleet << Bus.new(:color => "Green", :wheels => 12, :num_seats => 65, :fare => 1.25)
a = "a"
until fleet[3].passengers.size == fleet[3].num_seats
fleet[3].admit_passenger("person " + a, 1.25)
a = a.next
end
p "This bus is carrying #{fleet[3].passengers.size} passengers"
p "A passenger requested a stop. What's going to happen?"
if fleet[3].stop_requested? == false
p "A passenger did not really request a stop and was just joking. The bus is still driving #{fleet[3].status}"
else
p "A passenger did request a stop and the bus is now #{fleet[3].status}."
end
p "How much gas do you have? -- I have a #{fleet[3].needs_gas?}."
if fleet[3].gas == :empty
fleet[3].fill_tank!
p "Your tank is now full"
end
p fleet[3].passengers.size == fleet[3].num_seats
p fleet[3].color == "Green"
p fleet[3].wheels == 12
p fleet[3].num_seats == 65
p fleet[3].fare == 1.25
p "Car Fleet"
fleet << Car.new(:color => "Red")
p fleet[4].color == "Red"
p "How much gas do you have? -- I have a #{fleet[4].needs_gas?}."
if fleet[4].gas == :empty
fleet[4].fill_tank!
p "Your tank is now full"
end
fleet << Car.new(:color => "Grey")
p fleet[5].color == "Grey"
p "How much gas do you have? -- I have a #{fleet[5].needs_gas?}."
if fleet[5].gas == :empty
fleet[5].fill_tank!
p "Your tank is now full"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment