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

Michelle and Katrina - Edges - OO-Ride-Share #23

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 Aug 27, 2018
b8d529b
Added calculate_trip_duration method and its corresponding test
kaganon Aug 27, 2018
4fc706e
Oops did not save the changes
kaganon Aug 27, 2018
5228ce3
Wave 1.2: implemented user#net_expenditures
kangazoom Aug 28, 2018
deabd99
Rewrote code for user#net_expenditures and created user#total_time_spent
kangazoom Aug 28, 2018
262d3c8
added driver.rb
kangazoom Aug 28, 2018
a4eacf8
Wave 2: added to driver.rb
kangazoom Aug 28, 2018
19cc122
not sure what we did here
kaganon Aug 28, 2018
dc8b338
Created a driver. Great.
kangazoom Aug 28, 2018
05e228b
we finally added a driver instance
kaganon Aug 28, 2018
9983780
passed tests
kaganon Aug 28, 2018
a546107
updated trip and specs
kaganon Aug 28, 2018
6779b38
Wave 2: started loading driver; ~more to come~
kangazoom Aug 29, 2018
eb8017e
fixed some of load_drivers in trip_dispatcher.rb
kangazoom Aug 29, 2018
c7933ea
loading drivers
kaganon Aug 29, 2018
d17b561
updated load driver
kaganon Aug 29, 2018
b3dff1b
WE DID IT WE LOADED A DRIVER WOOHOO
kangazoom Aug 29, 2018
056c70f
worked out time conflict
kaganon Aug 29, 2018
9013f41
added average and driven_trips functions to driver
kangazoom Aug 29, 2018
5ae2c3b
added a note to driver about status
kangazoom Aug 29, 2018
16a9aea
added average rating method
kaganon Aug 29, 2018
8b8e456
added tests for total revenue, but need more
kaganon Aug 29, 2018
360058d
designated default status to available if nil
kaganon Aug 30, 2018
28f8ed4
added net expenditures method
kaganon Aug 30, 2018
16c5dc2
finished wave 2 methods and tests (in driver.rb)
kangazoom Aug 30, 2018
4bb4d1e
Added change driver status and request trip
kaganon Aug 30, 2018
e642d10
added notes for our to do list
kaganon Aug 31, 2018
17542c2
started wave 3, added notes on what to do next
kaganon Aug 31, 2018
ec3885a
trying to get trip_dispatcher tests to pass
kangazoom Aug 31, 2018
22853d7
commented code in trip_dispatcher
kangazoom Aug 31, 2018
e17a090
passed test to initialize trip dispatcher
kaganon Aug 31, 2018
c842deb
fixed test for initializing trip dispatcher
kaganon Aug 31, 2018
ffab027
changed 'connects drivers with trips' test in trip_dispatcher
kangazoom Aug 31, 2018
cd3a815
ignored nil values in end_time, cost, rating
kaganon Aug 31, 2018
3495784
added some more nil calculation checks (average rating and total reve…
kangazoom Aug 31, 2018
fc2a6ab
fixed errors in average rating for driver for nil
kangazoom Aug 31, 2018
a8ad4f9
Added tests for wave 3
kaganon Sep 1, 2018
3c3d5e0
Merge branch 'master' of https://github.com/kangazoom/oo-ride-share
kaganon Sep 1, 2018
9dd73e2
finished final test for trip_dispatch (request status)
kangazoom Sep 1, 2018
cc9b5f0
cleaned up lib files and refactored nil tests
kangazoom Sep 1, 2018
4a4dad1
cleaned up all tests
kangazoom Sep 1, 2018
1ec5242
fixed whitespace in trip_dispatch_spec
kangazoom Sep 1, 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
77 changes: 77 additions & 0 deletions lib/driver.rb
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

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

end
end
end
24 changes: 21 additions & 3 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
require 'csv'
require 'time'

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]
@driver = input[:driver]

if @rating != nil
if @rating > 5 || @rating < 1
raise ArgumentError.new("Invalid rating #{@rating}")
end
end


if @rating > 5 || @rating < 1
raise ArgumentError.new("Invalid rating #{@rating}")
if @end_time != nil
if @end_time - @start_time < 0
raise ArgumentError.new("Trip end time is before start time.")
end
end
end

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

def calculate_trip_duration
if @end_time != nil
return @end_time - @start_time
end
end
end
end
72 changes: 68 additions & 4 deletions lib/trip_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 }

Choose a reason for hiding this comment

The 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, \
Expand All @@ -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
18 changes: 18 additions & 0 deletions lib/user.rb
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
Expand All @@ -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

Choose a reason for hiding this comment

The 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
Loading