Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oo-ride-share - Liz and Hannah #10

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8e9c900
saved with corrected bug
hertweckhr1 Aug 27, 2018
e2668fa
parsed time calculated time duration in seconds and dev'd tests - all…
elle-johnnie Aug 27, 2018
dbe337f
Merge branch 'master' of https://github.com/xLJohnsonx/oo-ride-share
elle-johnnie Aug 27, 2018
80a1732
test and method built for net expenditure - passes test
elle-johnnie Aug 28, 2018
9516489
Wave 1.2, time spent exception written
hertweckhr1 Aug 28, 2018
dc817e4
method built to pass total time spent on all trips for user - wave 1 …
elle-johnnie Aug 28, 2018
ee00be5
driver class created and passes tests updated driver_spec too
elle-johnnie Aug 28, 2018
198d51b
driver instances added to trip.rb, passes test
elle-johnnie Aug 28, 2018
00d71f6
attempt to load drivers with trip dispatcher too many errors
elle-johnnie Aug 29, 2018
7826cf1
loaded drivers successfully from csv file
hertweckhr1 Aug 29, 2018
dfde8c7
Wave 2 load drivers portion complete
hertweckhr1 Aug 29, 2018
988846e
wave 2 complete and tests pass
hertweckhr1 Aug 30, 2018
2f8884b
Request_trip method added with tests - first draft
hertweckhr1 Aug 30, 2018
1ac95f3
combined header at top of trip_request specs
hertweckhr1 Aug 30, 2018
d528dd3
tested trip_request adds to driver trips, user trips, and overall tri…
hertweckhr1 Aug 30, 2018
7beb0e2
wave 3 complete all - completed so drivers cannot drive themselves, a…
hertweckhr1 Aug 31, 2018
17c11db
added tests for wave 4 and added trip for driver 8 in test data
hertweckhr1 Aug 31, 2018
d06d6da
ideas developed for solving wave 4
hertweckhr1 Aug 31, 2018
4787ede
final cleanup
hertweckhr1 Sep 1, 2018
77ca856
tests and code complete for finding driver with no trips and then lea…
hertweckhr1 Sep 2, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/coverage/
.DS_Store
.idea
70 changes: 70 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'pry'#driver class
module RideShare
class Driver < User
attr_reader :vehicle_id, :status, :driven_trips

def initialize(input, status=:UNAVAILABLE)
super(input)

if input[:id].nil? || input[:id] <= 0
raise ArgumentError, 'ID cannot be blank or less than zero.'
end

@vehicle_id = input[:vehicle_id].to_s # binding.pry
if @vehicle_id.length != 17 || @vehicle_id.empty?
raise ArgumentError, 'Invalid VIN'
end
@status = (input[:status]).to_sym
unless @status == :UNAVAILABLE || @status == :AVAILABLE
raise ArgumentError, "invalid status"
end
@driven_trips = []
end

def average_rating
completed_trips = @driven_trips.find_all { |trip| trip.end_time != nil }

return 0 if completed_trips.empty?

total = 0
completed_trips.each do |trip|
total += trip.rating
end

average = total.to_f / completed_trips.length
return average
end

def add_driven_trip(trip)
unless trip.instance_of? Trip
raise ArgumentError, 'invalid trip data'
end
@driven_trips << trip
end

def add_trip_in_progress(trip)
@status = :UNAVAILABLE
@driven_trips << trip

Choose a reason for hiding this comment

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

Why not call add_driven_trip instead?

end

def total_revenue
completed_trips = @driven_trips.find_all { |trip| trip.end_time != nil }
total = 0
completed_trips.each do |trip|
total += (trip.cost - 1.65) * 0.80
end
return total
end

def net_expenditures
return (super - total_revenue)

Choose a reason for hiding this comment

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

👍

end

# latest drive developed for wave 4
def latest_drive

Choose a reason for hiding this comment

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

Very clever method, you're not assuming the most recent trip is the last one in the array. That's good!

latest = @driven_trips.max_by {|trip| trip.end_time }.end_time
return latest
end

end
end
23 changes: 17 additions & 6 deletions lib/trip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

module RideShare
class Trip
attr_reader :id, :passenger, :start_time, :end_time, :cost, :rating
attr_reader :id, :passenger, :start_time, :end_time, :cost, :rating, :driver

def initialize(input)
@id = input[:id]
@passenger = input[:passenger]
@start_time = input[:start_time]
@end_time = input[:end_time]
@cost = input[:cost]
@rating = input[:rating]

if @rating > 5 || @rating < 1
raise ArgumentError.new("Invalid rating #{@rating}")
@cost = input[:cost] ? input[:cost].to_f : nil
@rating = input[:rating] ? input[:rating].to_i : nil
@driver = input[:driver]
unless @end_time == nil
if @rating > 5 || @rating < 1
raise ArgumentError.new("Invalid rating #{@rating}")
end
end
unless @end_time == nil
if @start_time > @end_time
raise ArgumentError, "Invalid input. Start time must be before end time."
end
end
end

Expand All @@ -22,5 +29,9 @@ def inspect
"ID=#{id.inspect} " +
"PassengerID=#{passenger&.id.inspect}>"
end

def time_duration
return @end_time - @start_time
end
end
end
77 changes: 71 additions & 6 deletions lib/trip_dispatcher.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
require 'csv'
require 'time'
require 'pry'

require_relative 'user'
require_relative 'trip'
require_relative 'driver'

module RideShare
class TripDispatcher
attr_reader :drivers, :passengers, :trips

def initialize(user_file = 'support/users.csv',
trip_file = 'support/trips.csv')
trip_file = 'support/trips.csv',
driver_file = 'support/drivers.csv')
@passengers = load_users(user_file)
@drivers = load_drivers(driver_file)
@trips = load_trips(trip_file)


end

def load_users(filename)
Expand All @@ -25,33 +31,35 @@ def load_users(filename)

users << User.new(input_data)
end

return users
end


def load_trips(filename)
trips = []
trip_data = CSV.open(filename, 'r', headers: true,
header_converters: :symbol)

trip_data.each do |raw_trip|
passenger = find_passenger(raw_trip[:passenger_id].to_i)
driver = find_driver(raw_trip[:driver_id].to_i)

parsed_trip = {
id: raw_trip[:id].to_i,
passenger: passenger,
start_time: raw_trip[:start_time],
end_time: raw_trip[:end_time],
driver: driver,
# Modify load_trips to store the start_time and end_time
start_time: Time.parse(raw_trip[:start_time]),
end_time: Time.parse(raw_trip[:end_time]),
cost: raw_trip[:cost].to_f,
rating: raw_trip[:rating].to_i
}

trip = Trip.new(parsed_trip)
driver.add_trip(trip)
driver.add_driven_trip(trip)
passenger.add_trip(trip)
trips << trip
end

return trips
end

Expand All @@ -67,6 +75,63 @@ def inspect
#{passengers.count} passengers>"
end

# Load the Drivers from the support/drivers.csv file and
# return a collection of Driver instances, note that drivers
# can be passengers too! Replace the instance of User in the
# passengers array with a corresponding instance of Driver
def load_drivers(filename)
# initialize drivers
drivers = []
# parse csv for drivers
CSV.open(filename, 'r', headers: true).map do |line|
id = line[0].to_i
passenger = find_passenger(line[0].to_i)
line[1].length

driver_data = Driver.new({id: line[0].to_i,
name: passenger.name,
phone_number: passenger.phone_number,
trips: passenger.trips,
vehicle_id: line[1],
status: line[2]})
drivers << driver_data
end
return drivers
end

def find_driver(id)
check_id(id)
return @drivers.find { |driver| driver.id == id }
end

def request_trip(user_id)

Choose a reason for hiding this comment

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

This method is well done, but it's a bit long. You could break it into some helper methods doing the tasks of:

  1. Selecting an available driver
  2. Creating a new trip

passenger = find_passenger(user_id)

available_drivers = @drivers.find_all { |driver| driver.status == :AVAILABLE && driver.id != user_id}
raise ArgumentError, 'No available drivers' if available_drivers == []

Choose a reason for hiding this comment

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

Minor issue: Shouldn't be indented


driver = available_drivers.find { |driver| driver.driven_trips.empty? } # of .empty?
if driver.nil?
driver = available_drivers.min_by { |driver| driver.latest_drive }
end

parsed_trip = {
id: "id",
passenger: user_id,
driver: driver.id,
start_time: Time.now,
end_time: nil,
cost: nil,
rating: nil
}

trip = Trip.new(parsed_trip)
driver.add_trip_in_progress(trip)
passenger.add_trip(trip)
@trips << trip
return trip
end

private

def check_id(id)
Expand Down
22 changes: 22 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,27 @@ def initialize(input)
def add_trip(trip)
@trips << trip
end
# Add an instance method, net_expenditures, to User that will return
# the total amount of money that user has spent on their trips
def net_expenditures

total = 0
@trips.each do |trip_inst|
total += trip_inst.cost
end
return total
end

# Add an instance method, total_time_spent to User that will return
# the total amount of time that user has spent on their trips
def total_time_spent
completed_trips = @trips.find_all { |trip| trip.end_time != nil }

total_time = 0
completed_trips.each do |trip_inst|
total_time += (trip_inst.end_time - trip_inst.start_time)
end
return total_time
end
end
end
Loading