forked from aaronmgdr/cars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cars.rb
94 lines (66 loc) · 1.36 KB
/
cars.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Car
@@total_car_count = 0
@@cars_per_color = {}
attr_accessor :color
def to_s()
"I'm a car! I've driven #{@distance} and have #{@fuel} gallons gas left"
end
def initialize(color)
@color = color
@fuel = 10
@distance = 0
@@total_car_count += 1
if @@cars_per_color[color] == nil
@@cars_per_color[color] = 1
else
@@cars_per_color[color] += 1
end
@@convertible = true
end
def self.most_popular_color
@@cars_per_color.sort_by {|key, value| value }
@@cars_per_color.keys[0]
end
def self.total_car_count
@@total_car_count
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
class Convertible
@roof_status = "down"
def top_down
car_a = Car.new("black")
car_b = Car.new("blue")
car_c = Car.new("blue")
car_d = Car.new("blue")
car_e = Car.new("red")
best_color = Car.most_popular_color
Car.new(best_color)
puts best_color
# Car.cars_per_color["black"]
# 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
# c1 = Car.new
# puts Car.total_car_count
# c2 = Car.new
# puts Car.total_car_count