forked from Ada-C13/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ride_share.rb
135 lines (131 loc) · 3.45 KB
/
ride_share.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
#Structure of my_company datas looks like this:
my_company = [
{
DR0001:
[
{
date: "3rd Feb 2016",
cost: 10,
ride_id: "RD0003",
rating: 3
},
{
date: "3rd Feb 2016",
cost: 30,
ride_id:"RD0015",
rating: 4
},
{
date: "6th Feb 2016",
cost: 45,
ride_id: "RD0003",
rating: 2
}
],
},
{
DR0002:
[
{
date: "3rd Feb 2016",
cost: 25,
ride_id: "RD0073",
rating:5
},
{
date: "4th Feb 2016",
cost: 15,
ride_id: "RD0013",
rating:1
},
{
date: "7th Feb 2016",
cost: 35,
ride_id: "RD0066",
rating:3
}
],
},
{
DR0003:
[
{
date: "4th Feb 2016",
cost: 5,
ride_id: "RD0066",
rating:5
},
{
date: "5th Feb 2016",
cost: 50,
ride_id: "RD0003",
rating:2}
],
},
{DR0004:
[
{
date: "3rd Feb 2016",
cost: 5,
ride_id: "RD0022",
rating:5
},
{
date: "4th Feb 2016",
cost: 10,
ride_id: "RD0022",
rating:4
},
{date: "8th Feb 2016",
cost: 20,
ride_id: "RD0073",
rating:5
}
]
}
]
# Total of RIDES, EARNINGS , AVERAGE RATING, MOST EARNING PER DATE for each driver.
# Max of earnin and averge rating from all the drivers
def statistic_for_each_driver(my_company)
drivers_list = []
drivers_total_earnings = []
drivers_avg_ratings = []
my_company.each do |drivers|
drivers.each do |driver, ride_details|
puts "\nDriver with ID: #{driver} made #{ride_details.length} rides."
cost = []
average = []
date = []
ride_details.each do |ride|
cost << ride[:cost]
average << ride[:rating]
date << ride[:date]
end
# The total amount of money each driver has made
puts "The total amount of money that driver with ID #{driver} has made is $#{cost.sum} in total."
# The average rating for each driver
puts "The average rating for driver with ID #{driver} is #{(average.sum.to_f/ride_details.length).round(1)}."
#For each driver, on which day did they make the most money?
index_max_cost = cost.rindex(cost.max)
date_max_cost = date[index_max_cost]
puts "The driver with ID #{driver} made most money ($#{cost.max}) on #{date_max_cost}."
#Save the current driver's statistics
drivers_list << driver
drivers_total_earnings << cost.sum
drivers_avg_ratings << (average.sum.to_f/ride_details.length).round(1)
end
end
# Which driver made the most money?
index_most_earnings = drivers_total_earnings.rindex(drivers_total_earnings.max)
highest_earning_driver = drivers_list[index_most_earnings]
highest_earning_driver_amount = drivers_total_earnings.max
puts ""
puts "The driver with ID #{highest_earning_driver} was highest earning ($#{highest_earning_driver_amount}) among all drivers"
# Which driver has the highest average rating?
index_highest_avg_ratings = drivers_avg_ratings.rindex(drivers_avg_ratings.max)
highest_avg_driver = drivers_list[index_highest_avg_ratings]
highest_avg_driver_rating = drivers_avg_ratings.max
puts ""
puts "The driver with ID #{highest_avg_driver} had highest average rating (#{highest_avg_driver_rating}) among all drivers"
end
statistic_for_each_driver(my_company)