-
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
Sigrid & Barbara, Edges, oo-ride-share #7
base: master
Are you sure you want to change the base?
Changes from all commits
e9c2e0d
114fd9f
0700e0a
1a70fb3
19523c5
8ebdc03
f9c8df4
50e6f8b
6f47520
48090a3
ee2b789
251e6d9
69519d0
8c0c924
1928564
aad1567
e4a9ad6
bdb8835
cc62f35
fb16ed7
1ca4aac
629ee67
40ff18a
85fc892
9f90936
84b06e7
2f5780a
3f388c6
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require_relative 'user' | ||
require 'pry' | ||
|
||
module RideShare | ||
class Driver < User | ||
attr_reader :vin, :driven_trips, :status | ||
|
||
def initialize(input) | ||
super | ||
|
||
@vin = input[:vin] | ||
if @vin.length != 17 | ||
raise ArgumentError.new("Invalid VIN") | ||
else | ||
@vin = input[:vin] | ||
end | ||
|
||
@driven_trips = input[:driven_trips].nil? ? [] : input[:driven_trips] | ||
|
||
if input[:status] != :AVAILABLE && input[:status] != :UNAVAILABLE | ||
@status = :UNAVAILABLE | ||
else | ||
@status = input[:status] | ||
end | ||
end | ||
|
||
def add_driven_trip(trip) | ||
if trip.is_a? Trip | ||
# binding.pry | ||
@driven_trips << trip | ||
# binding.pry | ||
else | ||
raise ArgumentError.new("Invalid trip instance") | ||
end | ||
end | ||
|
||
def average_rating | ||
total_num_rides = @driven_trips.length | ||
total_sum = 0 | ||
|
||
if total_num_rides == 0 | ||
return 0 | ||
end | ||
|
||
@driven_trips.each do |x| | ||
# binding.pry | ||
if x.rating == "In Progress" | ||
next | ||
else | ||
total_sum += x.rating | ||
end | ||
end | ||
|
||
avg_rating = total_sum.to_f / total_num_rides | ||
return avg_rating | ||
|
||
end | ||
|
||
|
||
def total_revenue | ||
total_cost_all_trips = 0 | ||
total_num_rides = 0 | ||
|
||
# if total_num_rides == 0 | ||
# return 0 | ||
# end | ||
|
||
@driven_trips.each do |x| | ||
if x.cost == "In Progress" | ||
next | ||
else | ||
total_cost_all_trips += x.cost | ||
total_num_rides += 1 | ||
end | ||
end | ||
|
||
all_fees = total_num_rides * 1.65 | ||
driver_revenue = (total_cost_all_trips - all_fees) * 0.8 | ||
driver_revenue_rounded = driver_revenue.round(2) | ||
|
||
return driver_revenue_rounded | ||
end | ||
|
||
def net_expenditures | ||
net_expenditures = super | ||
difference = net_expenditures - total_revenue | ||
# difference = super - total_revenue # can use super in place of above code. | ||
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. Good use of |
||
return difference | ||
end | ||
|
||
def change_availability | ||
if @status == :AVAILABLE | ||
@status = :UNAVAILABLE | ||
else | ||
@status = :AVAILABLE | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
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] | ||
|
@@ -11,16 +11,34 @@ def initialize(input) | |
@end_time = input[:end_time] | ||
@cost = input[:cost] | ||
@rating = input[:rating] | ||
@driver = input[:driver] | ||
|
||
if @rating > 5 || @rating < 1 | ||
if @rating == nil | ||
@rating = "In Progress" | ||
@cost = "In Progress" | ||
elsif @rating > 5 || @rating < 1 | ||
raise ArgumentError.new("Invalid rating #{@rating}") | ||
end | ||
|
||
if @end_time == nil | ||
@end_time = "In Progress" | ||
elsif @end_time < @start_time | ||
raise ArgumentError, "End time is before start time" | ||
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. I would much rather have |
||
end | ||
end | ||
|
||
def inspect | ||
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} " + | ||
"ID=#{id.inspect} " + | ||
"PassengerID=#{passenger&.id.inspect}>" | ||
end | ||
|
||
def duration | ||
if @end_time == "In Progress" | ||
return "In Progress" | ||
else | ||
return @end_time - @start_time | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
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 | ||
|
||
|
@@ -22,13 +26,30 @@ def load_users(filename) | |
input_data[:id] = line[0].to_i | ||
input_data[:name] = line[1] | ||
input_data[:phone] = line[2] | ||
|
||
users << User.new(input_data) | ||
end | ||
|
||
return users | ||
end | ||
|
||
def load_drivers(filename) | ||
drivers = [] | ||
|
||
CSV.read(filename, headers: true).each do |line| | ||
passenger = find_passenger(line[0].to_i) | ||
input_data = {} | ||
input_data[:id] = line[0].to_i | ||
input_data[:vin] = line[1] | ||
input_data[:status] = line[2].to_sym | ||
input_data[:name] = passenger.name | ||
input_data[:phone] = passenger.phone_number | ||
driver = Driver.new(input_data) | ||
passenger_index = @passengers.find_index(passenger) | ||
@passengers[passenger_index] = driver | ||
drivers << driver | ||
end | ||
return drivers | ||
end | ||
|
||
|
||
def load_trips(filename) | ||
trips = [] | ||
|
@@ -37,21 +58,31 @@ 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) | ||
passenger_as_driver = find_driver(raw_trip[:passenger_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) | ||
if passenger_as_driver != nil | ||
passenger_as_driver.add_trip(trip) | ||
end | ||
trips << trip | ||
end | ||
|
||
return trips | ||
end | ||
|
||
|
@@ -60,17 +91,77 @@ def find_passenger(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 inspect | ||
return "#<#{self.class.name}:0x#{self.object_id.to_s(16)} \ | ||
#{trips.count} trips, \ | ||
#{drivers.count} drivers, \ | ||
#{passengers.count} passengers>" | ||
end | ||
|
||
private | ||
# private | ||
|
||
def check_id(id) | ||
raise ArgumentError, "ID cannot be blank or less than zero. (got #{id})" if id.nil? || id <= 0 | ||
end | ||
|
||
|
||
|
||
# Note: The user_id must correspond to a User instance | ||
def request_trip(user_id) | ||
# Finding the User instance | ||
current_passenger = @passengers.find { |passenger| passenger.id == user_id } | ||
|
||
if current_passenger == nil | ||
raise ArgumentError.new("There is no user instance with the id #{user_id}") | ||
end | ||
|
||
|
||
# Checking for available drivers that do not have the same id as the user requesting a ride | ||
available_drivers = [] | ||
@drivers.each do |x| | ||
if x.status == :AVAILABLE && x.id != user_id.to_i | ||
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 would be a great piece of logic to move to a helper method. |
||
available_drivers << x | ||
end | ||
end | ||
|
||
# If there are available drivers, then create trip instance. | ||
if available_drivers.length > 0 | ||
# number_of_drivers = available_drivers.length | ||
# random_driver = available_drivers[rand(0...number_of_drivers)] | ||
# random_driver_id = random_driver.id | ||
first_available_driver = available_drivers.first | ||
|
||
trip_info = { | ||
id: @trips.length + 1, | ||
passenger: current_passenger, | ||
start_time: Time.now, | ||
end_time: nil, | ||
cost: nil, | ||
rating: nil, | ||
driver: first_available_driver | ||
} | ||
|
||
trip = Trip.new(trip_info) | ||
@trips << trip | ||
|
||
# Changing driver data | ||
first_available_driver.add_driven_trip(trip) | ||
first_available_driver.change_availability | ||
|
||
# Changing current passenger data | ||
current_passenger.add_trip(trip) | ||
|
||
return trip | ||
|
||
else | ||
return "No available drivers" | ||
# return nil | ||
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 should either return |
||
end | ||
end | ||
end | ||
end |
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.
You tended to use
.each
iteration to calculate totals/sum things in Driver and User, even though things like.sum
is available. Honestly, I don't believe that you ALWAYS need to use fancy Enumerable methods like.sum
over.each
iteration every time, but I want to make sure you know that those are possibilities.