forked from Ada-C12/ride-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathride_share.rb
More file actions
138 lines (128 loc) · 3.59 KB
/
ride_share.rb
File metadata and controls
138 lines (128 loc) · 3.59 KB
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
=begin
Initialize ride data. Data structure is a hash
containing driver_ids as keys and an array containing
rides as its value. Each value in the array
contains a hash of the ride information.
=end
ride_data =
{
DR0001: [
{
Date: "3rd Feb 2016",
Cost: 10,
Rider_id: "RD0003",
Rating: 3,
},
{
Date: "3rd Feb 2016",
Cost: 30,
Rider_id: "RD0003",
Rating: 4,
},
{
Date: "5th Feb 2016",
Cost: 45,
Rider_id: "RD0003",
Rating: 2,
},
],
DR0002: [
{
Date: "3rd Feb 2016",
Cost: 25,
Rider_id: "RD0073",
Rating: 5,
},
{
Date: "4th Feb 2016",
Cost: 15,
Rider_id: "RD0013",
Rating: 1,
},
{
Date: "5th Feb 2016",
Cost: 35,
Rider_id: "RD0066",
Rating: 3,
},
],
DR0003: [
{
Date: "4th Feb 2016",
Cost: 5,
Rider_id: "RD0066",
Rating: 5,
},
{
Date: "5th Feb 2016",
Cost: 50,
Rider_id: "RD0003",
Rating: 2,
},
],
DR0004: [
{
Date: "3rd Feb 2016",
Cost: 5,
Rider_id: "RD0023",
Rating: 5,
},
{
Date: "4th Feb 2016",
Cost: 10,
Rider_id: "RD0022",
Rating: 4,
},
{
Date: "5th Feb 2016",
Cost: 20,
Rider_id: "RD0073",
Rating: 5,
},
],
}
# Initialize variables for use within each loop
current_max = 0
current_max_driver = ""
current_max_rating = 0
current_max_rating_driver = ""
# Iterate through the each driver id (key to hash) to count how many hashes
# are within it's array to determine the number of rides they've given and print
# the statement
puts "Driver information: \n \n"
ride_data.each do |driver_id, ride_array|
puts "#{driver_id} gave #{ride_array.count} rides."
# Initialize cost and rating array to hold costs and ratingss
# of each driver ride then access cost and rating values to push to the arrays
rating_array = []
cost_array = []
ride_array.each do |ride|
cost_array.push(ride[:Cost])
rating_array.push(ride[:Rating])
end
# Sum each cost array to get each driver's total earnings
puts "Total earnings: $#{cost_array.sum}"
# Use max cost index from cost array to access the date with the highest earning.
highest_earning_index = cost_array.index(cost_array.max)
highest_earning_day = ride_array[highest_earning_index][:Date]
puts "Highest earning day: #{highest_earning_day}"
# Sum rating array and divide it by the array length to determine
# each driver's average rating and print formatted
average_rating = ((rating_array.sum.to_f) / rating_array.count.to_f)
puts "Average rating: #{sprintf("%.2f", average_rating)} \n \n"
# Use previously instantiated max variables with the sum of cost_array
# to determine which driver made the most money
if cost_array.sum > current_max
current_max = cost_array.sum
current_max_driver = driver_id
end
# Use previously instantiated max rating variables and average rating to determine
# which driver had the highest average rating
if average_rating > current_max_rating
current_max_rating = average_rating
current_max_rating_driver = driver_id
end
end
# Print the driver who made the most money and the driver with the highest rating
puts "#{current_max_driver} made the most money with a total of $#{current_max}."
puts "#{current_max_rating_driver} had the highest average rating at #{sprintf("%.2f", current_max_rating)} stars!"