-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathcars.rb
More file actions
49 lines (41 loc) · 739 Bytes
/
Copy pathcars.rb
File metadata and controls
49 lines (41 loc) · 739 Bytes
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
class Car
@@total_car_count = 0
def self.total_car_count #
@@total_car_count
end
def initialize()
@fuel = 10
@distance = 0
@@total_car_count +=1
end
def to_s()
"I'm a car! I've driven #{@distance} and have #{@fuel} gallons gas left"
end
def drive(miles)
if (@fuel - miles/20.0) >= 0
@distance += miles
@fuel -= miles/20.0
else
@distance += @fuel * 20.0
@fuel = 0
puts "You're out of gas!"
end
end
def fuel_up()
gallons_needed = 10.0 - @fuel
puts "You must pay $#{3.5 * gallons_needed}"
@fuel = 10.0
end
end
car_a = Car.new()
car_b = Car.new()
puts car_a
puts car_b
car_a.drive(10)
puts car_a
puts car_b
car_a.drive(232)
car_b.drive(117)
puts car_a
puts car_b
puts Car.total_car_count