Last active
September 23, 2018 12:12
-
-
Save harrisonmalone/e7de0ed2a127d49b8b7672ed272f3843 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
class Vehicle | |
def initialize(make, model, tank) | |
@make = make | |
@model = model | |
@fuel = 0 | |
@tank = tank | |
end | |
def refuel | |
input = nil | |
while @fuel < @tank && input != "y" | |
puts "how much fuel do you want to put into your car? (in litres)" | |
print "> " | |
litres = gets.chomp.to_i | |
if (@fuel + litres) > @tank | |
puts "Sorry your tank would overflow, please put in less fuel" | |
else | |
@fuel += litres | |
puts "###" | |
puts "You now have #{@fuel} litres of fuel in your tank. You can still put in #{@tank - @fuel} litres more." | |
puts "###" | |
puts "Would you like to exit (y/n)" | |
input = gets.chomp | |
end | |
end | |
end | |
def fuel_level | |
"You have #{@fuel} litres of fuel in the tank" | |
end | |
end | |
class Car < Vehicle | |
def initialize(make, model, tank, color) | |
super(make, model, tank) | |
@power_steering = true | |
@color = color | |
end | |
def wind_up_windows | |
return "windows going up" | |
end | |
end | |
class Motorbike < Vehicle | |
def wheelie | |
return "woowww wheelie" | |
end | |
end | |
# instances | |
civic = Car.new("Honda", "Civic", 50, "red") | |
civic.refuel | |
p civic.fuel_level | |
civic.refuel(10) | |
civic.refuel(1) | |
p civic | |
p civic.wind_up_windows | |
low_rider = Motorbike.new("Harley Davidson", "Low Rider") | |
p low_rider | |
p low_rider.wheelie | |
low_rider.refuel(10) | |
p low_rider | |
p low_rider.fuel_level | |
# excellent challenge for seeing why we need inheritance in classes sometimes | |
# remember to think of inheritance as something that only is the thing above it | |
# a motorbike can inherit from vehicle because a motorcycle is a vehicle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment