forked from Ada-C10/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayla_Edges_Rideshare.rb
161 lines (138 loc) · 3.45 KB
/
Layla_Edges_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Using the provided data, created an array to hold hashes for each entry.
rides = [
{
driver_ID: 'DR0004',
date: '3rd Feb 2016',
cost: 5,
rider_id: 'RD0022',
rating: 5
},
{
driver_ID: 'DR0001',
date: '3rd Feb 2016',
cost: 10,
rider_id: 'RD0003',
rating: 3
},
{
driver_ID: 'DR0002',
date: '3rd Feb 2016',
cost: 25,
rider_id: 'RD0073',
rating: 5
},
{
driver_ID: 'DR0001',
date: '3rd Feb 2016',
cost: 30,
rider_id: 'RD0015',
rating: 4
},
{
driver_ID: 'DR0003',
date: '4th Feb 2016',
cost: 5,
rider_id: 'RD0066',
rating: 5
},
{
driver_ID: 'DR0004',
date: '4th Feb 2016',
cost: 10,
rider_id: 'RD0022',
rating: 4
},
{
driver_ID: 'DR0002',
date: '4th Feb 2016',
cost: 10,
rider_id: 'RD0013',
rating: 1
},
{
driver_ID: 'DR0003',
date: '5th Feb 2016',
cost: 50,
rider_id: 'RD0003',
rating: 2
},
{
driver_ID: 'DR0002',
date: '5th Feb 2016',
cost: 35,
rider_id: 'RD0066',
rating: 3
},
{
driver_ID: 'DR0004',
date: '5th Feb 2016',
cost: 20,
rider_id: 'RD0073',
rating: 5,
},
{
driver_ID: 'DR0001',
date: '5th Feb 2016',
cost: 45,
rider_id: 'RD0073',
rating: 2,
},
]
# Based on the information provided, created a hash to hold the driver information.
drivers = {}
# Iterating through the rides array, checking to see if driver_id key exists, if not, it adds the id to the drivers hash
# along with information about the driver from that ride. It then continues to iterate to see if the driver id already exists,
# and if so, it adds the rest of the driver information from that ride.
rides.each do |ride|
if drivers[ride[:driver_ID]]
drivers[ride[:driver_ID]][:rides] += 1
drivers[ride[:driver_ID]][:money] += ride[:cost]
drivers[ride[:driver_ID]][:ratings].push(ride[:rating])
else
drivers[ride[:driver_ID]] = {}
drivers[ride[:driver_ID]][:rides] = 1
drivers[ride[:driver_ID]][:money] = ride[:cost]
drivers[ride[:driver_ID]][:ratings] = [ride[:rating]]
end
end
puts """
+-+-+-+-+-+-+-+-+-+-+-+-+
Based on the provided data,
here are some statistics for our rideshare drivers:
+-+-+-+-+-+-+-+-+-+-+-+-+
"""
# Loop that iterates through the hash and provides information on the money each driver made and their average rating.
drivers.each do |driver, data|
data[:average_rating] = data[:ratings].sum / data[:rides]
puts "Driver ID: #{driver}, drove #{data[:rides]} times and made a total of $#{data[:money]}."
puts "The average rating for this driver is #{data[:average_rating]} stars."
puts
end
# Created a method to determine which driver made the most money.
def most_money(driver_data)
driver_id = nil
most_money = 0
driver_data.each do |driver, data|
if data[:money] > most_money
driver_id = driver
most_money = data[:money]
end
end
return driver_id
end
puts "Overall: "
puts "#{most_money(drivers)} made the most money."
# Created a method to determine which driver has the highest average rating.
def get_top_rating(driver_data)
driver_id = nil
top_rating = 0
driver_data.each do |driver, data|
if data[:average_rating] > top_rating
driver_id = driver
top_rating = data[:average_rating]
end
end
return driver_id
end
puts "#{get_top_rating(drivers)} has the current highest average rating."
puts