-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: master
Are you sure you want to change the base?
Changes from all commits
8e9c900
e2668fa
dbe337f
80a1732
9516489
dc817e4
ee00be5
198d51b
00d71f6
7826cf1
dfde8c7
988846e
2f8884b
1ac95f3
d528dd3
7beb0e2
17c11db
d06d6da
4787ede
77ca856
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/coverage/ | ||
.DS_Store | ||
.idea |
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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
end | ||
|
||
# latest drive developed for wave 4 | ||
def latest_drive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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) | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
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 == [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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?