-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.rb
53 lines (44 loc) · 946 Bytes
/
car.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Car
def initialize
@fuel = 10
@distance = 0
#puts "the initialize method is running automatically"
end
def get_info
if @distance.to_i <= 200
puts "I'm a car. I've driven #{@distance} miles and have #{@fuel} gallons of gas left."
end
end
def drive
puts "How many miles are you going?"
@distance = gets.chomp
if @distance.to_i >= 200
puts "You can only go 200 miles on one tank!"
else
fuel_used = @distance.to_i / 20.0
@fuel = @fuel - fuel_used
end
end
def fuel_up
fuel_up = 10.0
cost = 3.50
if @fuel < 10
puts "You have #{@fuel} gallons left. You should get more gas!"
@fuel = fuel_up - @fuel
total_cost = cost*@fuel
puts "It will cost you #{total_cost} to fill-up."
end
end
end
new_car = Car.new
new_car.get_info
new_car.drive
new_car.get_info
new_car.drive
new_car.fuel_up
car_a = Car.new
car_b = Car.new
car_a.get_info
car_a.drive
car_a.get_info
car_b.get_info