forked from AdaGold/oo-ride-share
-
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
Michelle and Katrina - Edges - OO-Ride-Share #23
Open
kangazoom
wants to merge
42
commits into
Ada-C10:master
Choose a base branch
from
kangazoom:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
d99ed0f
Wave 1: modified TripDispatcher#load_trips and Trip#initialize except…
kangazoom b8d529b
Added calculate_trip_duration method and its corresponding test
kaganon 4fc706e
Oops did not save the changes
kaganon 5228ce3
Wave 1.2: implemented user#net_expenditures
kangazoom deabd99
Rewrote code for user#net_expenditures and created user#total_time_spent
kangazoom 262d3c8
added driver.rb
kangazoom a4eacf8
Wave 2: added to driver.rb
kangazoom 19cc122
not sure what we did here
kaganon dc8b338
Created a driver. Great.
kangazoom 05e228b
we finally added a driver instance
kaganon 9983780
passed tests
kaganon a546107
updated trip and specs
kaganon 6779b38
Wave 2: started loading driver; ~more to come~
kangazoom eb8017e
fixed some of load_drivers in trip_dispatcher.rb
kangazoom c7933ea
loading drivers
kaganon d17b561
updated load driver
kaganon b3dff1b
WE DID IT WE LOADED A DRIVER WOOHOO
kangazoom 056c70f
worked out time conflict
kaganon 9013f41
added average and driven_trips functions to driver
kangazoom 5ae2c3b
added a note to driver about status
kangazoom 16a9aea
added average rating method
kaganon 8b8e456
added tests for total revenue, but need more
kaganon 360058d
designated default status to available if nil
kaganon 28f8ed4
added net expenditures method
kaganon 16c5dc2
finished wave 2 methods and tests (in driver.rb)
kangazoom 4bb4d1e
Added change driver status and request trip
kaganon e642d10
added notes for our to do list
kaganon 17542c2
started wave 3, added notes on what to do next
kaganon ec3885a
trying to get trip_dispatcher tests to pass
kangazoom 22853d7
commented code in trip_dispatcher
kangazoom e17a090
passed test to initialize trip dispatcher
kaganon c842deb
fixed test for initializing trip dispatcher
kaganon ffab027
changed 'connects drivers with trips' test in trip_dispatcher
kangazoom cd3a815
ignored nil values in end_time, cost, rating
kaganon 3495784
added some more nil calculation checks (average rating and total reve…
kangazoom fc2a6ab
fixed errors in average rating for driver for nil
kangazoom a8ad4f9
Added tests for wave 3
kaganon 3c3d5e0
Merge branch 'master' of https://github.com/kangazoom/oo-ride-share
kaganon 9dd73e2
finished final test for trip_dispatch (request status)
kangazoom cc9b5f0
cleaned up lib files and refactored nil tests
kangazoom 4a4dad1
cleaned up all tests
kangazoom 1ec5242
fixed whitespace in trip_dispatch_spec
kangazoom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require_relative "user" | ||
|
||
|
||
module RideShare | ||
class Driver < User | ||
attr_reader :id, :name, :vehicle_id, :driven_trips, :phone_number | ||
attr_accessor :status | ||
|
||
def initialize(input) | ||
super(input) | ||
|
||
@vehicle_id = input[:vin] | ||
@driven_trips = input[:trips].nil? ? [] : input[:trips] | ||
@status = input[:status] ? input[:status] : :AVAILABLE | ||
|
||
|
||
unless @vehicle_id.length == 17 | ||
raise ArgumentError, 'Vehicle ID must contain 17 characters' | ||
end | ||
|
||
unless @status == :AVAILABLE || @status == :UNAVAILABLE | ||
raise ArgumentError, 'Not a valid status.' | ||
end | ||
|
||
if input[:id].nil? || input[:id] <= 0 | ||
raise ArgumentError, 'ID cannot be blank or less than zero.' | ||
end | ||
end | ||
|
||
def average_rating() | ||
|
||
sum = 0 | ||
|
||
if @driven_trips.empty? | ||
return 0 | ||
else | ||
trips_amount = @driven_trips.length | ||
|
||
valid_trips = @driven_trips.reject { |trip| trip.rating.nil? } | ||
|
||
return valid_trips.sum {|trip| trip.rating} / trips_amount.to_f | ||
end | ||
end | ||
|
||
def add_driven_trip(trip) | ||
|
||
unless trip.class == RideShare::Trip | ||
raise ArgumentError, 'This is not a valid trip' | ||
end | ||
|
||
@driven_trips << trip | ||
end | ||
|
||
|
||
def total_revenue() | ||
if @driven_trips.empty? | ||
return 0 | ||
else | ||
valid_trips = @driven_trips.reject { |trip| trip.cost.nil? } | ||
|
||
revenue_made = valid_trips.sum {|trip| (trip.cost - 1.65) } * 0.80 | ||
|
||
return ("%.2f" % revenue_made).to_f | ||
end | ||
end | ||
|
||
|
||
def net_expenditures() | ||
|
||
total_driver_revenue = total_revenue() | ||
|
||
if !(super.nil? && total_driver_revenue.nil?) | ||
return ("%.2f" % (super - total_driver_revenue)).to_f | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,19 @@ | |
|
||
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) | ||
|
@@ -37,29 +41,87 @@ def load_trips(filename) | |
|
||
trip_data.each do |raw_trip| | ||
passenger = find_passenger(raw_trip[:passenger_id].to_i) | ||
driver = find_driver(raw_trip[:driver_id].to_i) | ||
|
||
start_time = Time.parse(raw_trip[:start_time]) | ||
end_time = Time.parse(raw_trip[:end_time]) | ||
|
||
parsed_trip = { | ||
id: raw_trip[:id].to_i, | ||
passenger: passenger, | ||
start_time: raw_trip[:start_time], | ||
end_time: raw_trip[:end_time], | ||
start_time: start_time, | ||
end_time: end_time, | ||
cost: raw_trip[:cost].to_f, | ||
rating: raw_trip[:rating].to_i | ||
rating: raw_trip[:rating].to_i, | ||
driver: driver | ||
} | ||
|
||
trip = Trip.new(parsed_trip) | ||
|
||
passenger.add_trip(trip) | ||
driver.add_driven_trip(trip) | ||
trips << trip | ||
end | ||
|
||
return trips | ||
end | ||
|
||
def load_drivers(filename) | ||
drivers = [] | ||
|
||
driver_data = CSV.open(filename, "r", headers:true, header_converters: :symbol) | ||
|
||
driver_data.each do |raw_driver| | ||
user = find_passenger(raw_driver[:id].to_i) | ||
|
||
parsed_driver = { | ||
id: raw_driver[:id].to_i, | ||
name: user.name, | ||
phone: user.phone_number, | ||
vin: raw_driver[:vin], | ||
status: raw_driver[:status].to_sym | ||
} | ||
|
||
driver = Driver.new(parsed_driver) | ||
drivers << driver | ||
end | ||
return drivers | ||
end | ||
|
||
def find_passenger(id) | ||
check_id(id) | ||
return @passengers.find { |passenger| passenger.id == id } | ||
end | ||
|
||
def find_driver(id) | ||
check_id(id) | ||
return @drivers.find { |driver| driver.id == id } | ||
end | ||
|
||
def request_trip(user_id) | ||
current_passenger = find_passenger(user_id) | ||
available_driver = @drivers.find { |driver| driver.status == :AVAILABLE } | ||
|
||
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. You're not screening for getting the same person who requested this trip |
||
available_driver.status = :UNAVAILABLE | ||
|
||
parsed_trip = { | ||
id: (@trips.max_by {|trip| trip.id}).id + 1, | ||
passenger: current_passenger, | ||
start_time: Time.now, | ||
end_time: nil, | ||
cost: nil, | ||
rating: nil, | ||
driver: available_driver | ||
} | ||
|
||
trip_in_progress = Trip.new(parsed_trip) | ||
current_passenger.add_trip(trip_in_progress) | ||
available_driver.add_driven_trip(trip_in_progress) | ||
@trips << trip_in_progress | ||
|
||
return trip_in_progress | ||
end | ||
|
||
def inspect | ||
return "#<#{self.class.name}:0x#{self.object_id.to_s(16)} \ | ||
#{trips.count} trips, \ | ||
|
@@ -70,7 +132,9 @@ def inspect | |
private | ||
|
||
def check_id(id) | ||
id = id.to_i | ||
raise ArgumentError, "ID cannot be blank or less than zero. (got #{id})" if id.nil? || id <= 0 | ||
return id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
require_relative "trip" | ||
require "time" | ||
|
||
|
||
module RideShare | ||
class User | ||
attr_reader :id, :name, :phone_number, :trips | ||
|
@@ -16,5 +20,19 @@ def initialize(input) | |
def add_trip(trip) | ||
@trips << trip | ||
end | ||
|
||
def net_expenditures() | ||
valid_trips = @trips.reject { |trip| trip.cost.nil? } | ||
return valid_trips.sum { |trip| trip.cost} if valid_trips.length > 0 | ||
end | ||
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. Great use of enumerables through here! |
||
|
||
def total_time_spent() | ||
|
||
valid_trips = @trips.reject { |trip| trip.calculate_trip_duration.nil? } | ||
|
||
return valid_trips.sum {|trip| trip.calculate_trip_duration} if valid_trips.length > 0 | ||
|
||
end | ||
|
||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good use of
super
here