Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions ride_share.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# This is a hash of arrays and the arrays cont contains hashes
drivers = {

DR0001: [

{
date: '3rd Feb 2016',
cost: 10,
rider_id: 'RD0003',
rating: 3
},
{
date: '3rd Feb 2016',
cost: 30,
rider_id: 'RD0015',
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: 'RD0022',
rating: 5
},
{
date: '4th Feb 2016',
cost: 10,
rider_id: 'RD0022',
rating: 4
},
{
date: '5th Feb 2016',
cost: 20,
rider_id: 'RD0073',
rating: 5
}
]
}
# ####################################################################

# # This method calculates the number of rides each driver has given.
puts "This is how many rides each driver has given."
def driver_info(drivers)
drivers.each do |key, value|
ride_count = drivers[key].length
puts special_string = "This driver:#{key} has given a total of #{value.length} rides. "
end
end

driver_info(drivers)

# # #########################################################################

# # The total amount of money each driver has made!
puts "This is how much money each driver has made!"

def sum_drivers_income(drivers)

driver_earnings = {}

drivers.each do |driver_id, rides|
total_money = rides.sum do |ride|
ride[:cost]
end
driver_earnings[driver_id] = total_money
end

return driver_earnings

end

total_income_for_each_driver = sum_drivers_income(drivers)
total_income_for_each_driver.each do |driver, income|
puts "This driver: #{driver} made this much money: $#{income}"
Comment on lines +120 to +121

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation!

end

# #########################################################

# # This driver made the most money!
def highest_income(drivers)

each_money = sum_drivers_income(drivers)
most_made = each_money.max_by { |driver, earnings| earnings }[0]
puts "This driver: #{most_made} made the MOST money!"
Comment on lines +129 to +131

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation!


end

highest_income(drivers)

# # ##########################################################

# # Which driver has the highest average rating?

def highest_avg_rating(drivers)

avg_ratings_each_driver = {}

drivers.each do |driver_id, rides|
sum_of_ratings = rides.sum do |ride|
ride[:rating]
end
avg_ratings_each_driver[driver_id] = sum_of_ratings / rides.length
end

return avg_ratings_each_driver.max_by do |driver, rating|
rating
end
end

highest_rated = [highest_avg_rating(drivers)].to_h
highest_rated.each do |driver, rating|
puts "The highest average rating of: #{rating} belongs to this driver: #{driver}!"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation!

end