From 8ee9d3c18bf16a07b1297e487e33abae0ce7873d Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 10:47:30 -0700 Subject: [PATCH 01/37] created 2 tests for time data in trips and read end and start times as an instance of Time --- lib/trip.rb | 9 +++++++-- test/trip_test.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index f59464dae..ac6a84670 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,4 +1,5 @@ require 'csv' +require 'time' require_relative 'csv_record' @@ -36,6 +37,10 @@ def initialize( if @rating > 5 || @rating < 1 raise ArgumentError.new("Invalid rating #{@rating}") end + + if end_time < start_time + raise ArgumentError.new("End time cannot be before start time.") + end end def inspect @@ -61,8 +66,8 @@ def self.from_csv(record) return self.new( id: record[: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] ) diff --git a/test/trip_test.rb b/test/trip_test.rb index 2063e28a7..4845a4a6a 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -33,6 +33,10 @@ expect(@trip.driver).must_be_kind_of RideShare::Driver end + it "stores end_time as an instance of Time" do + expect(@trip.end_time).must_be_instance_of Time + end + it "raises an error for an invalid rating" do [-3, 0, 6].each do |rating| @trip_data[:rating] = rating @@ -41,5 +45,13 @@ end.must_raise ArgumentError end end + + it "raises an error for end times before start times" do + @trip_data[:end_time] = @trip_data[:start_time] - 25 * 60 + expect do + RideShare::Trip.new(@trip_data) + end.must_raise ArgumentError + end end + end From 765b606547fd3d8dcbc369f04cfd5eb21de130dc Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 11:04:30 -0700 Subject: [PATCH 02/37] added duration method --- lib/trip.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index ac6a84670..c8852ae3e 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -60,6 +60,11 @@ def connect(passenger) passenger.add_trip(self) end + def duration + trip_duration = @end_time - @start_time + return trip_duration + end + private def self.from_csv(record) From 96e8621f8b41b6b7d80f8a11f891d63caf714976 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 11:05:03 -0700 Subject: [PATCH 03/37] added duration test, returns accurate number of seconds --- test/trip_test.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/trip_test.rb b/test/trip_test.rb index 4845a4a6a..46e0361fc 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -54,4 +54,26 @@ end end + describe "duration" do + before do + start_time = Time.now + end_time = start_time + 10 * 60 # 10 minutes + @trip_data = { + id: 8, + passenger: RideShare::Passenger.new( + id: 1, + name: "Ada", + phone_number: "412-432-7640" + ), + start_time: start_time, + end_time: end_time, + cost: 23.45, + rating: 3 + } + @trip = RideShare::Trip.new(@trip_data) + end + it "returns accurate number of seconds" do + expect(@trip.duration).must_equal 600 + end + end end From b775a935572f77dd9bdf1beb6b18debe11984d1f Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 11:41:26 -0700 Subject: [PATCH 04/37] created net_expenditure method --- lib/passenger.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/passenger.rb b/lib/passenger.rb index 12ec25982..cddf20c5d 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -16,6 +16,15 @@ def add_trip(trip) @trips << trip end + def net_expenditures + all_cost = @trips.map { |trip| trip.cost } + if all_cost.nil? + return 0 + else + return all_cost.sum + end + end + private def self.from_csv(record) From 705b354d4c332a20a97e7180f08fbb7500768693 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 11:41:55 -0700 Subject: [PATCH 05/37] created 3 tests for net_expenditure --- test/passenger_test.rb | 46 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index eb3a631df..91db27b55 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -69,6 +69,50 @@ end describe "net_expenditures" do - # You add tests for the net_expenditures method + before do + @passenger = RideShare::Passenger.new( + id: 9, + name: "Merl Glover III", + phone_number: "1-602-620-2330 x3723", + trips: [] + ) + trip = RideShare::Trip.new( + id: 8, + passenger: @passenger, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + cost: 24.5, + rating: 5 + ) + @passenger.add_trip(trip) + + end + + it "returns a numeric" do + expect(@passenger.net_expenditures).must_be_kind_of Numeric + end + + it "returns accurate net expenditure" do + trip = RideShare::Trip.new( + id: 8, + passenger: @passenger, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + cost: 39, + rating: 5 + ) + @passenger.add_trip(trip) + expect(@passenger.net_expenditures).must_equal 63.5 + end + + it "returns 0 for nil trips" do + @passenger = RideShare::Passenger.new( + id: 12, + name: "Merl Glover IV", + phone_number: "1-602-620-2330 x3723", + trips: [] + ) + expect(@passenger.net_expenditures).must_equal 0 + end end end From 41abf54b35e5b91d2cc09bb928a86824e5aeea5c Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 12:04:27 -0700 Subject: [PATCH 06/37] added method total_time_spent and dry-ed up net_expenditures --- lib/passenger.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/passenger.rb b/lib/passenger.rb index cddf20c5d..d9c0d7bc5 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -1,4 +1,5 @@ require_relative 'csv_record' +#require_relative 'trip' module RideShare class Passenger < CsvRecord @@ -18,13 +19,17 @@ def add_trip(trip) def net_expenditures all_cost = @trips.map { |trip| trip.cost } - if all_cost.nil? - return 0 - else - return all_cost.sum - end + + all_cost.nil? ? 0 : all_cost.sum end + def total_time_spent + all_times = @trips.map {|trip| trip.duration} + + all_times.nil? ? 0 : all_times.sum + end + + private def self.from_csv(record) From 23add512f78cdd1bdb8236fd626f96a46734f6e0 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 12:05:47 -0700 Subject: [PATCH 07/37] added core tests for total_time_spent, must return float and returns accurate result --- test/passenger_test.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 91db27b55..8231d374a 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -115,4 +115,33 @@ expect(@passenger.net_expenditures).must_equal 0 end end + + describe "total_time_spent" do + before do + @passenger = RideShare::Passenger.new( + id: 9, + name: "Merl Glover III", + phone_number: "1-602-620-2330 x3723", + trips: [] + ) + trip = RideShare::Trip.new( + id: 8, + passenger: @passenger, + start_time: Time.new(2016, 8, 8, 12), + end_time: Time.new(2016, 8, 8, 12, 10, 30), + cost: 24.5, + rating: 5 + ) + @passenger.add_trip(trip) + end + + it "returns a float" do + expect(@passenger.total_time_spent).must_be_instance_of Float + end + + it "returns accurate result" do + expect(@passenger.total_time_spent).must_equal 630 + end + end + end From c36a5f9f7ab43c5d1b321ed4dd1ff058c46f6a62 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 12:06:53 -0700 Subject: [PATCH 08/37] added returns 0 for nil trips to total_time_spent --- test/passenger_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 8231d374a..d9dd6341c 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -142,6 +142,16 @@ it "returns accurate result" do expect(@passenger.total_time_spent).must_equal 630 end + + it "returns 0 for nil trips" do + @passenger = RideShare::Passenger.new( + id: 12, + name: "Merl Glover IV", + phone_number: "1-602-620-2330 x3723", + trips: [] + ) + expect(@passenger.total_time_spent).must_equal 0 + end end end From 503f1abaacd6b62823b3a5f3df0327a3d88f4ab8 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 16:36:59 -0700 Subject: [PATCH 09/37] created Driver class and set up initialization --- lib/driver.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/driver.rb diff --git a/lib/driver.rb b/lib/driver.rb new file mode 100644 index 000000000..cde3beaa3 --- /dev/null +++ b/lib/driver.rb @@ -0,0 +1,22 @@ +require 'csv' +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) + + super(id) + + @name = name + @vin = vin + @status = status + @trips = trips + end + end +end \ No newline at end of file From aa1ceabe0ad72517216ab16809f6ccb1d7ff1af8 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 16:37:26 -0700 Subject: [PATCH 10/37] released driver tests --- test/driver_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/driver_test.rb b/test/driver_test.rb index 4e6076ec2..77bdaa9de 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -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( From 97eb9bb1ddf1414c9f3d40bffa8c82bfd4bdced7 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 16:37:48 -0700 Subject: [PATCH 11/37] uncommented driver test helper --- test/test_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index bdb9bf352..082369790 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,4 +12,4 @@ require_relative '../lib/passenger' require_relative '../lib/trip' require_relative '../lib/trip_dispatcher' -# require_relative '../lib/driver' +require_relative '../lib/driver' From 3a54a0e228b620fbd631559f9e83014507dd7499 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:09:29 -0700 Subject: [PATCH 12/37] update fro_csv, update status.to_sym --- lib/driver.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index cde3beaa3..fe83f58c9 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -9,14 +9,26 @@ class Driver < CsvRecord 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) + raise ArgumentError.new("Invalid status") unless [:AVAILABLE, :UNAVAILABLE].include?(status.to_sym) super(id) @name = name @vin = vin - @status = status + @status = status.to_sym @trips = trips end + + + private + + def self.from_csv(record) + return new( + id: record[:id], + name: record[:name], + vin: record[:vin], + status: record[:status] + ) + end end end \ No newline at end of file From 1352de6c45d929a997f998e3ef64ade5215e7532 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:09:51 -0700 Subject: [PATCH 13/37] add driver_id, driver attributes --- lib/trip.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/trip.rb b/lib/trip.rb index c8852ae3e..87674ce4b 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -5,10 +5,12 @@ 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:, @@ -18,6 +20,17 @@ def initialize( ) 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 @@ -70,6 +83,7 @@ def duration def self.from_csv(record) return self.new( id: record[:id], + driver_id: record[:driver_id], passenger_id: record[:passenger_id], start_time: Time.parse(record[:start_time]), end_time: Time.parse(record[:end_time]), From cd1ec45a9e26088c0a62041acef434d8b94ac3cc Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:10:13 -0700 Subject: [PATCH 14/37] added driver load all --- lib/trip_dispatcher.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 5130849f8..f9845cd0a 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -3,6 +3,7 @@ require_relative 'passenger' require_relative 'trip' +require_relative 'driver' module RideShare class TripDispatcher @@ -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 From 7abe50acdc02c270e7f7bf0838f7dc16d3914fe3 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:10:47 -0700 Subject: [PATCH 15/37] correct errors by including driver_id --- test/passenger_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index d9dd6341c..03f539a15 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -46,6 +46,7 @@ ) trip = RideShare::Trip.new( id: 8, + driver_id: 1, passenger: @passenger, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), @@ -78,6 +79,7 @@ ) trip = RideShare::Trip.new( id: 8, + driver_id: 2, passenger: @passenger, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), @@ -95,6 +97,7 @@ it "returns accurate net expenditure" do trip = RideShare::Trip.new( id: 8, + driver_id: 3, passenger: @passenger, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), @@ -126,6 +129,7 @@ ) trip = RideShare::Trip.new( id: 8, + driver_id: 3, passenger: @passenger, start_time: Time.new(2016, 8, 8, 12), end_time: Time.new(2016, 8, 8, 12, 10, 30), From 84fd603c353acb2be8d32993a87a652449a97754 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:11:20 -0700 Subject: [PATCH 16/37] correct errors by including driver_id --- test/trip_dispatch_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 21f4457b7..f152b77fa 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -79,7 +79,7 @@ def build_test_dispatcher end # TODO: un-skip for Wave 2 - xdescribe "drivers" do + describe "drivers" do describe "find_driver method" do before do @dispatcher = build_test_dispatcher From f8a799998756ec1e91d7e6392fc27439bd66936d Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:12:06 -0700 Subject: [PATCH 17/37] correct errors by including driver_id --- test/trip_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/trip_test.rb b/test/trip_test.rb index 46e0361fc..9197e180f 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -7,6 +7,7 @@ end_time = start_time + 25 * 60 # 25 minutes @trip_data = { id: 8, + driver_id: 3, passenger: RideShare::Passenger.new( id: 1, name: "Ada", @@ -60,6 +61,7 @@ end_time = start_time + 10 * 60 # 10 minutes @trip_data = { id: 8, + driver_id: 4, passenger: RideShare::Passenger.new( id: 1, name: "Ada", From 8d4de99a5279ebe1f636b372722433aa575f9511 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 17:33:41 -0700 Subject: [PATCH 18/37] created add_trip method --- lib/driver.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/driver.rb b/lib/driver.rb index fe83f58c9..640abd960 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -19,6 +19,10 @@ def initialize(id:, name:, vin:, status: :AVAILABLE, trips: []) @trips = trips end + def add_trip(trip) + @trips << trip + end + private From 3c4dd8e70a637531a11828303e293468e58353c2 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 17:34:30 -0700 Subject: [PATCH 19/37] updated connect to include driver parameter --- lib/trip.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/trip.rb b/lib/trip.rb index 87674ce4b..a0d426d58 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -2,6 +2,7 @@ require 'time' require_relative 'csv_record' +require_relative 'driver' module RideShare class Trip < CsvRecord @@ -68,9 +69,11 @@ def inspect "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 From ba3864fae547d52ef12dbe761cdbe9874487dc2e Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 17:35:34 -0700 Subject: [PATCH 20/37] created find_driver method and updated connect_trips to include driver --- lib/trip_dispatcher.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index f9845cd0a..e0d6d7b6c 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -21,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)} \ @@ -34,7 +39,8 @@ def inspect 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 From bb9ad3527886e6556221dec4e918dfa09d614a2d Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 17:35:55 -0700 Subject: [PATCH 21/37] unskipped driver test --- test/trip_test.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/trip_test.rb b/test/trip_test.rb index 9197e180f..8b5de48e1 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -7,7 +7,12 @@ end_time = start_time + 25 * 60 # 25 minutes @trip_data = { id: 8, - driver_id: 3, + driver: RideShare::Driver.new( + id: 31, + name: "Mackenzie", + vin: '12345678901234567', + status: :AVAILABLE + ), passenger: RideShare::Passenger.new( id: 1, name: "Ada", @@ -30,7 +35,6 @@ end it "stores an instance of driver" do - skip # Unskip after wave 2 expect(@trip.driver).must_be_kind_of RideShare::Driver end From 3b692edb3cdd9ffda1d4d6c494a9f5232cea7e79 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Wed, 30 Sep 2020 17:49:50 -0700 Subject: [PATCH 22/37] added method average_rating and passed tests --- lib/driver.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index 640abd960..8ef2e8266 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -23,6 +23,21 @@ 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.sum.to_f / all_ratings.length + return average + end + + end + + # def total_revenue do + # + # end private @@ -35,4 +50,4 @@ def self.from_csv(record) ) end end -end \ No newline at end of file +end From 882cb1f124eea87a746e3357f526a9e0ca5f15c5 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 18:10:45 -0700 Subject: [PATCH 23/37] created total_revenue method --- lib/driver.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 8ef2e8266..8148f696d 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -35,9 +35,17 @@ def average_rating end - # def total_revenue do - # - # end + def total_revenue + all_cost = @trips.map do |trip| + if trip.cost >= 1.65 + (trip.cost - 1.65) * 0.8 + else + 0 + end + end + + all_cost.nil? ? 0 : all_cost.sum + end private From 7348d6661e922951d628160279bcc17139af755d Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 18:12:09 -0700 Subject: [PATCH 24/37] updated to ternary operator --- lib/driver.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 8148f696d..ad2fdd3e6 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -36,13 +36,7 @@ def average_rating end def total_revenue - all_cost = @trips.map do |trip| - if trip.cost >= 1.65 - (trip.cost - 1.65) * 0.8 - else - 0 - end - end + all_cost = @trips.map { |trip| trip.cost >= 1.65 ? (trip.cost - 1.65) * 0.8 : 0} all_cost.nil? ? 0 : all_cost.sum end From 515cf105eaf3f3580f78dd7a1c0debb182d6f30e Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 18:12:29 -0700 Subject: [PATCH 25/37] updated inspect to include driver --- lib/trip.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/trip.rb b/lib/trip.rb index a0d426d58..690ec162e 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -62,6 +62,7 @@ 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} " + From 2f05c141e16420dfd26e14078ad59129127c051d Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 30 Sep 2020 18:12:59 -0700 Subject: [PATCH 26/37] created 2 tests for total_revenue method --- test/driver_test.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/driver_test.rb b/test/driver_test.rb index 77bdaa9de..d38f29cbb 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -132,5 +132,39 @@ 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 end end From 42fde6c60d995fb4b62bdaa5dec6b897afc198c9 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 14:05:16 -0700 Subject: [PATCH 27/37] created make_unavailable method --- lib/driver.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index ad2fdd3e6..fa89dab5d 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -36,11 +36,15 @@ def average_rating end def total_revenue - all_cost = @trips.map { |trip| trip.cost >= 1.65 ? (trip.cost - 1.65) * 0.8 : 0} + all_cost = @trips.map { |trip| trip.cost >= 1.65 ? (trip.cost - 1.65) * 0.8 : 0 } all_cost.nil? ? 0 : all_cost.sum end + def make_unavailable + @status = :UNAVAILABLE + end + private def self.from_csv(record) From 0d66bc8546a927a5de7ef7c96496d8dd98c39c96 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 14:05:54 -0700 Subject: [PATCH 28/37] modified initial values to nil, modified exception validations to accommodate the nils --- lib/trip.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 690ec162e..3bce092c9 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -15,9 +15,9 @@ def initialize( passenger: nil, passenger_id: nil, start_time:, - end_time:, + end_time: nil, cost: nil, - rating: + rating: nil ) super(id) @@ -48,12 +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 - if end_time < start_time - raise ArgumentError.new("End time cannot be before start time.") + unless end_time == nil + if end_time < start_time + raise ArgumentError.new("End time cannot be before start time.") + end end end From f861266dced81ddd5905ac68e668f4115be728d2 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 14:06:18 -0700 Subject: [PATCH 29/37] created request_trip method and two helper methods --- lib/trip_dispatcher.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index e0d6d7b6c..15f9119ad 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -34,6 +34,39 @@ 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) + + 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 From ab1c91f8e78f4a90f6bac133901819a8c10a8adf Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 14:07:20 -0700 Subject: [PATCH 30/37] created test to see if we can return Trip for request_trip --- test/trip_dispatch_test.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index f152b77fa..0f2e3e1b2 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -78,7 +78,6 @@ def build_test_dispatcher end end - # TODO: un-skip for Wave 2 describe "drivers" do describe "find_driver method" do before do @@ -121,5 +120,15 @@ def build_test_dispatcher end end end + + describe "request trip" do + before do + @dispatcher = build_test_dispatcher + end + it "returns an instance of Trip" do + request = @dispatcher.request_trip(3) + expect(request).must_be_instance_of RideShare::Trip + end + end end end From 6e18cc65dc5a9e68116806e745abc364eb94a421 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Thu, 1 Oct 2020 15:23:05 -0700 Subject: [PATCH 31/37] added raise argument errror for no available drivers --- lib/trip_dispatcher.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 15f9119ad..2af3e73d0 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -52,6 +52,10 @@ 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, From 185527bd67fc9e1fb83b194e08a8c638d673fdb4 Mon Sep 17 00:00:00 2001 From: Mackenzie Date: Thu, 1 Oct 2020 15:23:43 -0700 Subject: [PATCH 32/37] added tests for updating driver and passenger trip lists, selected driver available, and when no avaialble drivers --- test/trip_dispatch_test.rb | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 0f2e3e1b2..f2aac7cac 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -129,6 +129,45 @@ def build_test_dispatcher request = @dispatcher.request_trip(3) expect(request).must_be_instance_of RideShare::Trip end + + it "updates passenger trip list" do + passenger_trip_initial = @dispatcher.passengers.first.trips.length + + @dispatcher.request_trip(1) + passenger_trip_final = @dispatcher.passengers.first.trips.length + + expect(passenger_trip_final).must_equal passenger_trip_initial + 1 + end + + it "updates driver trip list" do + driver_trip_initial = @dispatcher.available_driver.first.trips.length + + new_trip = @dispatcher.request_trip(1) + driver_trip_final = new_trip.driver.trips.length + + expect(driver_trip_final).must_equal driver_trip_initial + 1 + end + + it "uses an available driver and makes unavailable" do + driver_before_request = @dispatcher.available_driver.first + + expect(driver_before_request.status).must_equal :AVAILABLE + + new_trip = @dispatcher.request_trip(4) + driver_after_request = new_trip.driver + + expect(driver_after_request.id).must_equal driver_before_request.id + expect(driver_after_request.status).must_equal :UNAVAILABLE + end + + it "will raise Argument Error when no drivers available" do + @dispatcher.request_trip(4) + @dispatcher.request_trip(4) + + expect { @dispatcher.request_trip(4) }.must_raise ArgumentError + + end + end end end From 81e6934d92a0f76664f67c1f8e52f5ac972423d2 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 16:06:05 -0700 Subject: [PATCH 33/37] refactored average_rating and total_revenue to account for trips in progress --- lib/driver.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index fa89dab5d..b50ebd303 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -29,16 +29,25 @@ def average_rating if all_ratings.length == 0 return 0 else - average = all_ratings.sum.to_f / all_ratings.length + average = all_ratings.compact.sum.to_f / all_ratings.compact.length return average end end def total_revenue - all_cost = @trips.map { |trip| trip.cost >= 1.65 ? (trip.cost - 1.65) * 0.8 : 0 } + 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 - all_cost.nil? ? 0 : all_cost.sum + return all_cost.compact.sum end def make_unavailable From 1dff25bbd29fdb72232f66e1eff7437a2c2e2a2f Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 16:07:02 -0700 Subject: [PATCH 34/37] refactored net_expenditures and total_time_spent to account for trips in progress --- lib/passenger.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/passenger.rb b/lib/passenger.rb index d9c0d7bc5..2541a6a32 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -18,15 +18,13 @@ def add_trip(trip) end def net_expenditures - all_cost = @trips.map { |trip| trip.cost } - - all_cost.nil? ? 0 : all_cost.sum + all_cost = @trips.map { |trip| trip.cost} + return all_cost.compact.sum end def total_time_spent all_times = @trips.map {|trip| trip.duration} - - all_times.nil? ? 0 : all_times.sum + return all_times.compact.sum end From c7a88bc44754967e56c2dd135d78944f18515b9f Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 16:07:43 -0700 Subject: [PATCH 35/37] refactored duration method to account for end_time == nil --- lib/trip.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 3bce092c9..c9a681e83 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -82,8 +82,7 @@ def connect(passenger, driver) end def duration - trip_duration = @end_time - @start_time - return trip_duration + @end_time == nil ? nil : (@end_time - @start_time) end private From a7dffc016340ecbbc6e49785bf0bcd83e918b175 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 16:10:04 -0700 Subject: [PATCH 36/37] created test to account for trips in progress --- test/driver_test.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/driver_test.rb b/test/driver_test.rb index d38f29cbb..6d0517f06 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -128,6 +128,19 @@ 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 @@ -166,5 +179,19 @@ ) 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 From 7050dae5503ab5760e4fec3c8faefeb3890dca75 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 1 Oct 2020 16:12:41 -0700 Subject: [PATCH 37/37] created tests to account for trips in progress --- test/passenger_test.rb | 66 ++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 03f539a15..faf6e5c39 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -108,12 +108,26 @@ expect(@passenger.net_expenditures).must_equal 63.5 end + it "ignores any trips in progress" do + trip = RideShare::Trip.new( + id: 8, + driver_id: 3, + passenger: @passenger, + start_time: Time.new(2016, 8, 8), + end_time: nil, + cost: nil, + rating: nil + ) + @passenger.add_trip(trip) + expect(@passenger.net_expenditures).must_equal 24.5 + end + it "returns 0 for nil trips" do @passenger = RideShare::Passenger.new( - id: 12, - name: "Merl Glover IV", - phone_number: "1-602-620-2330 x3723", - trips: [] + id: 12, + name: "Merl Glover IV", + phone_number: "1-602-620-2330 x3723", + trips: [] ) expect(@passenger.net_expenditures).must_equal 0 end @@ -122,19 +136,19 @@ describe "total_time_spent" do before do @passenger = RideShare::Passenger.new( - id: 9, - name: "Merl Glover III", - phone_number: "1-602-620-2330 x3723", - trips: [] + id: 9, + name: "Merl Glover III", + phone_number: "1-602-620-2330 x3723", + trips: [] ) trip = RideShare::Trip.new( - id: 8, - driver_id: 3, - passenger: @passenger, - start_time: Time.new(2016, 8, 8, 12), - end_time: Time.new(2016, 8, 8, 12, 10, 30), - cost: 24.5, - rating: 5 + id: 8, + driver_id: 3, + passenger: @passenger, + start_time: Time.new(2016, 8, 8, 12), + end_time: Time.new(2016, 8, 8, 12, 10, 30), + cost: 24.5, + rating: 5 ) @passenger.add_trip(trip) end @@ -149,13 +163,27 @@ it "returns 0 for nil trips" do @passenger = RideShare::Passenger.new( - id: 12, - name: "Merl Glover IV", - phone_number: "1-602-620-2330 x3723", - trips: [] + id: 12, + name: "Merl Glover IV", + phone_number: "1-602-620-2330 x3723", + trips: [] ) expect(@passenger.total_time_spent).must_equal 0 end + + it "ignores trips in progress" do + trip = RideShare::Trip.new( + id: 11, + driver_id: 9, + passenger: @passenger, + start_time: Time.new(2016, 8, 8, 12), + end_time: nil, + cost: nil, + rating: nil + ) + @passenger.add_trip(trip) + expect(@passenger.total_time_spent).must_equal 630 + end end end