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
52 changes: 52 additions & 0 deletions Julia_rideshare6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#Rideshare program for Julia Meier HW- due on August 14

driver = {
"DR0001" => [
{date: "03 Feb 2016", riderID: "RD0003", cost: 10, rating: 3},
{date: "03 Feb 2016", riderID: "RD0015", cost: 30, rating: 4},
{date: "05 Feb 2016", riderID: "RD0003", cost: 45, rating: 2}
],
"DR0002" => [
{date: "03 Feb 2016", riderID: "RD0073", cost: 25, rating: 5},
{date: "04 Feb 2016", riderID: "RD0013", cost: 15, rating: 1}
],
"DR0003" => [
{date: "04 Feb 2016", riderID: "RD0066", cost: 5, rating: 5}
],
"DR0004" => [
{date: "03 Feb 2016", riderID: "RD0022", cost: 5, rating: 5},
{date: "04 Feb 2016", riderID: "RD0022", cost: 10, rating: 4},
{date: "05 Feb 2016", riderID: "RD0073", cost: 20, rating: 5}
]
}

trips_hash = {}
earnings_hash = {}
average_rating_hash = {}

puts "There are #{driver.length} drivers."

driver.each do |drivername, ride_array|
number_of_trips = driver[drivername].length
trips_hash.store(drivername.to_sym, number_of_trips)

total_earnings = 0
sum_of_ratings = 0
number_of_trips.times do |x|
total_earnings = total_earnings + driver[drivername][x][:cost]
sum_of_ratings = sum_of_ratings + driver[drivername][x][:rating]
Copy link

Choose a reason for hiding this comment

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

in this specific loop, driver[drivername] is actually the same as ride_array :)

end

earnings_hash.store(drivername.to_sym, total_earnings)
average_rating = (sum_of_ratings.to_f)/(number_of_trips.to_f)
average_rating_hash.store(drivername.to_sym, average_rating)
end

trips_hash.each do |drivername, number_of_trips|
puts "Driver #{drivername} gave #{number_of_trips} rides and earned $#{earnings_hash[drivername]}. His average rating was #{average_rating_hash[drivername]} out of 5.0."
end

# QUESTIONS
# 1- Why did I have to put quotes around DR0001. WHen I didn't use quotes it gave me this error:
Copy link

Choose a reason for hiding this comment

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

Were you creating the hash with hash rocket notation ( => ) or symbol notation?

# Julia_rideshare2.rb:2:in `<main>': uninitialized constant DR0001 (NameError)
# How can I make one hash to store the number_of_trips, earnings and ratings data instead of three separate ones?
Copy link

Choose a reason for hiding this comment

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

You can absolutely make one hash to store this information ;)

Within your loop, you can calculate and print the appropriate information! Instead of iterating through trips_hash on line 45, you can print that same line (line 46) in your driver loop (before it ends on line 43).

There are other alternatives:

  • You can make a hash that stores all of those as key/value pairings that you associate with all of the calculated data
  • You can make a CLASS! called Driver! and a class called Ride! :) :) :)