forked from Ada-C12/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrideshare.rb
143 lines (125 loc) · 2.71 KB
/
rideshare.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
drivers = {
DR0001: [
{
date: '02/03/2016',
cost: 10,
rider_ID: 'RD0003',
rating: 3
},
{
date: '02/03/2016',
cost: 30,
rider_ID: 'RD0015',
rating: 4
},
{
date: '02/05/2016',
cost: 45,
rider_ID: 'RD0003',
rating: 2
}
],
DR0002: [
{
date: '02/03/2016',
cost: 25,
rider_ID: 'RD0073',
rating: 5
},
{
date: '02/04/2016',
cost: 15,
rider_ID: 'RD0013',
rating: 1
},
{
date: '02/05/2016',
cost: 35,
rider_ID: 'RD0066',
rating: 3
}
],
DR0003: [
{
date: '02/04/2016',
cost: 5,
rider_ID: 'RD0066',
rating: 5
},
{
date: '02/05/2016',
cost: 50,
rider_ID: 'RD0003',
rating: 2
}
],
DR0004: [
{
date: '02/03/2016',
cost: 5,
rider_ID: 'RD0022',
rating: 5
},
{
date: '02/04/2016',
cost: 10,
rider_ID: 'RD0022',
rating: 4
},
{
date: '02/05/2016',
cost: 20,
rider_ID: 'RD0073',
rating: 5
}
]
}
puts "TOTAL RIDES PER DRIVER:"
drivers.each_key do |driver|
puts "Driver #{driver} has given #{drivers[driver].count} rides."
end
puts "\nTOTAL AMOUNT MADE PER DRIVER:"
mostMoney = 0
richestDriver = ""
drivers.each do |driver, rides|
totalMoney = rides.map {|ride| ride[:cost]}.reduce (:+)
puts "Driver #{driver} has made $#{totalMoney}."
if totalMoney > mostMoney
mostMoney = totalMoney
richestDriver = driver
end
end
puts "\nAVERAGE RATING PER DRIVER:"
highestAverageRating = 0
nicestDriver = ""
drivers.each do |driver, rides|
totalRating = rides.map {|ride| ride[:rating]}.reduce(:+).to_f
averageRating = totalRating/(drivers[driver].count)
if averageRating % 1 == 0
averageRating = averageRating.to_i
else
averageRating = averageRating.round(2)
end
puts "Driver #{driver}'s average rating is #{averageRating}."
if averageRating > highestAverageRating
highestAverageRating = averageRating
nicestDriver = driver
end
end
puts "\nDriver #{richestDriver} made the most money: $#{mostMoney}."
puts "\nDriver #{nicestDriver} had the highest average rating: #{highestAverageRating}.\n\n"
puts "MOST PROFITABLE DAY PER DRIVER:"
ridesByDay = {}
drivers.each_key do |driver|
drivers[driver].each do |ride|
currentDate = ride[:date]
if ridesByDay.has_key? currentDate
ridesByDay[currentDate] += ride[:cost]
else
ridesByDay[currentDate] = ride[:cost]
end
end
max = ridesByDay.max_by{|date, total| total}
puts "Driver #{driver} made the most amount of money on #{max.first}: $#{max.last}."
ridesByDay.clear
end