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

Water - Mackenzie and Jessica #7

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8ee9d3c
created 2 tests for time data in trips and read end and start times a…
jwinchan Sep 30, 2020
765b606
added duration method
mvlofthus Sep 30, 2020
96e8621
added duration test, returns accurate number of seconds
mvlofthus Sep 30, 2020
b775a93
created net_expenditure method
jwinchan Sep 30, 2020
705b354
created 3 tests for net_expenditure
jwinchan Sep 30, 2020
41abf54
added method total_time_spent and dry-ed up net_expenditures
mvlofthus Sep 30, 2020
23add51
added core tests for total_time_spent, must return float and returns …
mvlofthus Sep 30, 2020
c36a5f9
added returns 0 for nil trips to total_time_spent
mvlofthus Sep 30, 2020
503f1ab
created Driver class and set up initialization
jwinchan Sep 30, 2020
aa1ceab
released driver tests
jwinchan Sep 30, 2020
97eb9bb
uncommented driver test helper
jwinchan Sep 30, 2020
3a54a0e
update fro_csv, update status.to_sym
mvlofthus Oct 1, 2020
1352de6
add driver_id, driver attributes
mvlofthus Oct 1, 2020
cd1ec45
added driver load all
mvlofthus Oct 1, 2020
7abe50a
correct errors by including driver_id
mvlofthus Oct 1, 2020
84fd603
correct errors by including driver_id
mvlofthus Oct 1, 2020
f8a7999
correct errors by including driver_id
mvlofthus Oct 1, 2020
8d4de99
created add_trip method
jwinchan Oct 1, 2020
3c4dd8e
updated connect to include driver parameter
jwinchan Oct 1, 2020
ba3864f
created find_driver method and updated connect_trips to include driver
jwinchan Oct 1, 2020
bb9ad35
unskipped driver test
jwinchan Oct 1, 2020
3b692ed
added method average_rating and passed tests
mvlofthus Oct 1, 2020
882cb1f
created total_revenue method
jwinchan Oct 1, 2020
7348d66
updated to ternary operator
jwinchan Oct 1, 2020
515cf10
updated inspect to include driver
jwinchan Oct 1, 2020
2f05c14
created 2 tests for total_revenue method
jwinchan Oct 1, 2020
42fde6c
created make_unavailable method
jwinchan Oct 1, 2020
0d66bc8
modified initial values to nil, modified exception validations to acc…
jwinchan Oct 1, 2020
f861266
created request_trip method and two helper methods
jwinchan Oct 1, 2020
ab1c91f
created test to see if we can return Trip for request_trip
jwinchan Oct 1, 2020
6e18cc6
added raise argument errror for no available drivers
mvlofthus Oct 1, 2020
185527b
added tests for updating driver and passenger trip lists, selected dr…
mvlofthus Oct 1, 2020
81e6934
refactored average_rating and total_revenue to account for trips in p…
jwinchan Oct 1, 2020
1dff25b
refactored net_expenditures and total_time_spent to account for trips…
jwinchan Oct 1, 2020
c7a88bc
refactored duration method to account for end_time == nil
jwinchan Oct 1, 2020
a7dffc0
created test to account for trips in progress
jwinchan Oct 1, 2020
7050dae
created tests to account for trips in progress
jwinchan Oct 1, 2020
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
68 changes: 68 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'csv'

Choose a reason for hiding this comment

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

CsvRecord already includes csv so including it here is not necessary. :)

require 'time'

require_relative 'csv_record'

module RideShare
class Driver < CsvRecord
attr_reader :id, :name, :vin, :status, :trips

def initialize(id:, name:, vin:, status: :AVAILABLE, trips: [])
raise ArgumentError.new("Invalid VIN length") unless vin.length == 17
raise ArgumentError.new("Invalid status") unless [:AVAILABLE, :UNAVAILABLE].include?(status.to_sym)

super(id)

@name = name
@vin = vin
@status = status.to_sym
@trips = trips
end

def add_trip(trip)
@trips << trip
end

def average_rating
all_ratings = @trips.map { |trip| trip.rating }

if all_ratings.length == 0
return 0
else
average = all_ratings.compact.sum.to_f / all_ratings.compact.length
return average
end

end

def total_revenue
all_cost = @trips.map do |trip|
case trip.cost
when nil
nil
when (0...1.65)
0
else
(trip.cost - 1.65) * 0.8
end
end

return all_cost.compact.sum
end

def make_unavailable
@status = :UNAVAILABLE
end

private

def self.from_csv(record)
return new(
id: record[:id],
name: record[:name],
vin: record[:vin],
status: record[:status]
)
end
end
end
12 changes: 12 additions & 0 deletions lib/passenger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'csv_record'
#require_relative 'trip'

module RideShare
class Passenger < CsvRecord
Expand All @@ -16,6 +17,17 @@ def add_trip(trip)
@trips << trip
end

def net_expenditures
all_cost = @trips.map { |trip| trip.cost}
return all_cost.compact.sum
end

def total_time_spent
all_times = @trips.map {|trip| trip.duration}
return all_times.compact.sum
end


private

def self.from_csv(record)
Expand Down
47 changes: 39 additions & 8 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
require 'csv'
require 'time'

require_relative 'csv_record'
require_relative 'driver'

module RideShare
class Trip < CsvRecord
attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating
attr_reader :id, :driver, :driver_id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating

def initialize(
id:,
driver: nil,
driver_id: nil,
passenger: nil,
passenger_id: nil,
start_time:,
end_time:,
end_time: nil,
cost: nil,
rating:
rating: nil
)
super(id)

if driver
@driver = driver
@driver_id = driver.id

elsif driver_id
@driver_id = driver_id

else
raise ArgumentError, 'Driver or driver_id is required'
end

if passenger
@passenger = passenger
@passenger_id = passenger.id
Expand All @@ -33,8 +48,16 @@ def initialize(
@cost = cost
@rating = rating

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

unless end_time == nil
if end_time < start_time
raise ArgumentError.new("End time cannot be before start time.")
end
end
end

Expand All @@ -43,26 +66,34 @@ def inspect
# trip contains a passenger contains a trip contains a passenger...
"#<#{self.class.name}:0x#{self.object_id.to_s(16)} " +
"id=#{id.inspect} " +
"driver_id=#{driver&.id.inspect} " +
"passenger_id=#{passenger&.id.inspect} " +
"start_time=#{start_time} " +
"end_time=#{end_time} " +
"cost=#{cost} " +
"rating=#{rating}>"
end

def connect(passenger)
def connect(passenger, driver)
@passenger = passenger
@driver = driver
passenger.add_trip(self)
driver.add_trip(self)
end

def duration
@end_time == nil ? nil : (@end_time - @start_time)
end

private

def self.from_csv(record)
return self.new(
id: record[:id],
driver_id: record[:driver_id],
passenger_id: record[:passenger_id],
start_time: record[:start_time],
end_time: record[:end_time],
start_time: Time.parse(record[:start_time]),
end_time: Time.parse(record[:end_time]),
cost: record[:cost],
rating: record[:rating]
)
Expand Down
47 changes: 46 additions & 1 deletion lib/trip_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require_relative 'passenger'
require_relative 'trip'
require_relative 'driver'

module RideShare
class TripDispatcher
Expand All @@ -11,6 +12,7 @@ class TripDispatcher
def initialize(directory: './support')
@passengers = Passenger.load_all(directory: directory)
@trips = Trip.load_all(directory: directory)
@drivers = Driver.load_all(directory: directory)
connect_trips
end

Expand All @@ -19,6 +21,11 @@ def find_passenger(id)
return @passengers.find { |passenger| passenger.id == id }
end

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

def inspect
# Make puts output more useful
return "#<#{self.class.name}:0x#{object_id.to_s(16)} \
Expand All @@ -27,12 +34,50 @@ def inspect
#{passengers.count} passengers>"
end

def available_driver
available_drivers = []
@drivers.each do |driver|
if driver.status == :AVAILABLE
available_drivers << driver
end
end
return available_drivers
end

def trip_id_generator
return @trips.last.id.to_i + 1
end

def request_trip(passenger_id)
driver = available_driver[0]
passenger = find_passenger(passenger_id)

if driver == nil
raise ArgumentError.new("No available drivers")
end

new_trip = Trip.new(
id: trip_id_generator,
driver_id: driver.id,
passenger_id: passenger_id,
start_time: Time.now,
)
driver.make_unavailable

new_trip.connect(passenger, driver)

@trips << new_trip

return new_trip
end

private

def connect_trips
@trips.each do |trip|
passenger = find_passenger(trip.passenger_id)
trip.connect(passenger)
driver = find_driver(trip.driver_id)
trip.connect(passenger, driver)
end

return trips
Expand Down
63 changes: 62 additions & 1 deletion test/driver_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'test_helper'

xdescribe "Driver class" do
describe "Driver class" do
describe "Driver instantiation" do
before do
@driver = RideShare::Driver.new(
Expand Down Expand Up @@ -128,9 +128,70 @@

expect(@driver.average_rating).must_be_close_to (5.0 + 1.0) / 2.0, 0.01
end

it"ignores trips in progress" do
trip = RideShare::Trip.new(
id: 8,
driver: @driver,
passenger_id: 3,
start_time: Time.now,
end_time: nil,
rating: nil
)
@driver.add_trip(trip)
expect(@driver.average_rating).must_equal 5
end
end

describe "total_revenue" do
# You add tests for the total_revenue method
before do
@driver = RideShare::Driver.new(
id: 54,
name: "Rogers Bartell IV",
vin: "1C9EVBRM0YBC564DZ"
)
trip = RideShare::Trip.new(
id: 8,
driver: @driver,
passenger_id: 3,
start_time: Time.new(2016, 8, 8),
end_time: Time.new(2016, 8, 8),
cost: 11.65,
rating: 5
)
@driver.add_trip(trip)
end

it "calculates accurate revenue" do
expect(@driver.total_revenue).must_be_close_to 8.0
end

it "returns 0 for trips less than 1.65" do
trip = RideShare::Trip.new(
id:29,
driver: @driver,
passenger_id: 94,
start_time: Time.new(2016, 8, 8),
end_time: Time.new(2016, 8, 8),
cost: 1.29,
rating: 4
)
expect(@driver.total_revenue).must_be_close_to 8
end

it "ignores trips in progress" do
trip = RideShare::Trip.new(
id: 9,
driver: @driver,
passenger_id: 2,
start_time: Time.new(2016, 8, 8),
end_time: nil,
cost: nil,
rating: nil
)
@driver.add_trip(trip)
expect(@driver.total_revenue).must_equal 8.0
end
end
end
Loading