Skip to content

Instantly share code, notes, and snippets.

@dammitBrandon
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 19, 2015 07:29
Show Gist options
  • Select an option

  • Save dammitBrandon/5918910 to your computer and use it in GitHub Desktop.

Select an option

Save dammitBrandon/5918910 to your computer and use it in GitHub Desktop.

Revisions

  1. dammitBrandon revised this gist Jul 5, 2013. 1 changed file with 149 additions and 64 deletions.
    213 changes: 149 additions & 64 deletions P5: OO Inheritance.rb
    Original file line number Diff line number Diff line change
    @@ -1,70 +1,155 @@
    class Car
    @@WHEELS = 4
    def initialize(args)
    @color = args[:color]
    @wheels = @@WHEELS
    end
    def drive
    @status = :driving
    end
    def brake
    @status = :stopped
    end
    def needs_gas?
    return [true,true,false].sample
    end
    # class Car
    # @@WHEELS = 4
    # def initialize(args)
    # @color = args[: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(args)
    @color = args[:color]
    @wheels = args[:wheels]
    @num_seats = args[:num_seats]
    @fare = args[:fare]
    @passengers=[]
    end
    def drive
    return self.brake if stop_requested?
    @status = :driving
    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 Bus
    # attr_reader :passengers
    # def initialize(args)
    # @color = args[:color]
    # @wheels = args[:wheels]
    # @num_seats = args[:num_seats]
    # @fare = args[:fare]
    # @passengers=[]
    # end
    # def drive
    # return self.brake if stop_requested?
    # @status = :driving
    # 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
    # @@WHEELS = 2
    # def initialize(args)
    # @color = args[: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

    class Vehicle
    def initialize
    @wheels = 4
    @fuel = ['true', 'false']
    @color = "silver"
    @status = :stopped

    end

    def drive
    @status = :driving
    end

    def brake
    @status = :stopped
    end

    def needs_gas?
    @fuel.sample
    end
    end

    class Car < Vehicle
    def initialize(args)
    @color = args[:color]
    end

    def drive
    @status = :driving
    end

    def brake
    @status = :stopped
    end

    def needs_gas?
    @fuel.sample
    end
    end

    class Bus < Vehicle
    def initialize(args)
    @color = args[:color]
    @num_seats = args[:num_seats]
    @passengers = []
    @fare = args[:fare]
    @wheels = args[:wheels]

    end

    def drive
    @status = :driving
    end

    def brake
    @status = :stopped
    end

    def needs_gas?
    @fuel.sample
    end
    end

    class Motorbike
    @@WHEELS = 2
    def initialize(args)
    @color = args[: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
    ###Test Cases###
    ###Car Tests###
    car = Car.new({{color: black}})
    puts car.wheels == 4
    car.drive
    puts "The cars current status is supposed to be driving, current status: #{car.status}"
    car.brake
    puts "The cars current status is supposed to be braking, current status: #{car.status}"
    puts car.needs_gas? == true || false
    ###Bus Tests###
    bus = Bus.new({color: red, wheels: 6, num_seats: 10, fare: 5})
    puts bus.passengers.empty?
    bus.admit_passenger("this guy", 4)
    puts "there is #{bus.passengers.length} passengers on the bus"
    bus.admit_passenger("this guy", 5)
    puts "there is #{bus.passengers.length} passengers on the bus"
    bus.drive
    puts "The current status is supposed to be driving, current status: #{bus.status}"
    bus.brake
    puts "The current status is supposed to be driving, current status: #{bus.status}"
    bus.stop_requested?
  2. @dbc-challenges dbc-challenges renamed this gist Jun 25, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @dbc-challenges dbc-challenges created this gist Jun 11, 2013.
    70 changes: 70 additions & 0 deletions vehicle.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    class Car
    @@WHEELS = 4
    def initialize(args)
    @color = args[: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(args)
    @color = args[:color]
    @wheels = args[:wheels]
    @num_seats = args[:num_seats]
    @fare = args[:fare]
    @passengers=[]
    end
    def drive
    return self.brake if stop_requested?
    @status = :driving
    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
    @@WHEELS = 2
    def initialize(args)
    @color = args[: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