From 7702b11e1d2c412f34412f0112863377b8c5487f Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Tue, 29 Sep 2020 15:12:28 -0700 Subject: [PATCH 01/10] wave 1 --- lib/passenger.rb | 11 +++++++ lib/trip.rb | 14 +++++++-- test/passenger_test.rb | 70 +++++++++++++++++++++++++++++++++++++++++- test/trip_test.rb | 25 +++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) diff --git a/lib/passenger.rb b/lib/passenger.rb index 12ec25982..4f4e900f1 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -16,6 +16,17 @@ def add_trip(trip) @trips << trip end + def net_expenditures + total = @trips.sum { |trip| trip.cost } + return total + end + + def total_time_spent + total = @trips.sum { |trip| trip.duration } + return total + end + + private def self.from_csv(record) diff --git a/lib/trip.rb b/lib/trip.rb index f59464dae..b93336e25 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,4 +1,5 @@ require 'csv' +require 'time' require_relative 'csv_record' @@ -30,6 +31,8 @@ def initialize( @start_time = start_time @end_time = end_time + + raise ArgumentError.new("Start time should occur before the end time") if @start_time > @end_time @cost = cost @rating = rating @@ -38,6 +41,11 @@ def initialize( end end + def duration + duration = @end_time - @start_time + return duration.to_i + end + def inspect # Prevent infinite loop when puts-ing a Trip # trip contains a passenger contains a trip contains a passenger... @@ -46,7 +54,7 @@ def inspect "passenger_id=#{passenger&.id.inspect} " + "start_time=#{start_time} " + "end_time=#{end_time} " + - "cost=#{cost} " + + "cost=#{cost} " + "rating=#{rating}>" end @@ -61,8 +69,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/passenger_test.rb b/test/passenger_test.rb index eb3a631df..36f484c59 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -49,6 +49,7 @@ passenger: @passenger, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), + cost: 10.5, rating: 5 ) @@ -69,6 +70,73 @@ end describe "net_expenditures" do - # You add tests for the net_expenditures method + before do + # TODO: you'll need to add a driver at some point here. + @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: 10.5, + rating: 5 + ) + trip_2 = RideShare::Trip.new( + id: 9, + passenger: @passenger, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + cost: 9, + rating: 5 + ) + + @passenger.add_trip(trip) + @passenger.add_trip(trip_2) + end + + it "can calculate the total spending for each passenger" do + expect(@passenger.net_expenditures).must_equal 19.5 + end end + + describe "total_time_spent" do + before do + # TODO: you'll need to add a driver at some point here. + @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.parse("2018-12-27 01:39:05 -0800"), + end_time: Time.parse("2018-12-27 02:39:05 -0800"), + cost: 10.5, + rating: 5 + ) + trip_2 = RideShare::Trip.new( + id: 9, + passenger: @passenger, + start_time: Time.parse("2018-12-27 01:39:05 -0800"), + end_time: Time.parse("2018-12-27 01:59:05 -0800"), + cost: 9, + rating: 5 + ) + + @passenger.add_trip(trip) + @passenger.add_trip(trip_2) + end + + it "can calculate the total spending for each passenger" do + expect(@passenger.total_time_spent).must_equal 4800 + end + end + end diff --git a/test/trip_test.rb b/test/trip_test.rb index 2063e28a7..4a621e8e1 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -41,5 +41,30 @@ end.must_raise ArgumentError end end + + it 'raises an error for end time before the start time' do + start_time = Time.parse("2018-12-27 02:39:05 -0800") + end_time = Time.parse("2018-12-27 01:39:05 -0800") + @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 + } + + expect do + RideShare::Trip.new(@trip_data) + end.must_raise ArgumentError + end + + it "returns a trip duration" do + expect(@trip.duration).must_equal 1500 + end end end From dc6b57c68a88d768fc5d5cc66e0e6485b9ad8b0a Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Tue, 29 Sep 2020 16:46:40 -0700 Subject: [PATCH 02/10] wave 2git statusgit status --- .floo | 3 ++ .flooignore | 6 ++++ lib/driver.rb | 59 ++++++++++++++++++++++++++++++++++++++++ lib/trip.rb | 22 +++++++++++++-- lib/trip_dispatcher.rb | 13 +++++++-- test/driver_test.rb | 33 ++++++++++++++++++++-- test/passenger_test.rb | 26 ++++++++++++++++++ test/test_data/trips.csv | 2 +- test/test_helper.rb | 2 +- test/trip_test.rb | 12 ++++++++ 10 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 .floo create mode 100644 .flooignore create mode 100644 lib/driver.rb diff --git a/.floo b/.floo new file mode 100644 index 000000000..9b907d2c2 --- /dev/null +++ b/.floo @@ -0,0 +1,3 @@ +{ + "url": "https://floobits.com/SophieMess/oo-ride-share" +} \ No newline at end of file diff --git a/.flooignore b/.flooignore new file mode 100644 index 000000000..ed824d39a --- /dev/null +++ b/.flooignore @@ -0,0 +1,6 @@ +extern +node_modules +tmp +vendor +.idea/workspace.xml +.idea/misc.xml diff --git a/lib/driver.rb b/lib/driver.rb new file mode 100644 index 000000000..18c04f022 --- /dev/null +++ b/lib/driver.rb @@ -0,0 +1,59 @@ +require_relative 'csv_record' + +module RideShare + class Driver < CsvRecord + attr_reader :name, :vin, :status, :trips + + def initialize(id:, name:, vin:, status: :AVAILABLE, trips: []) + super(id) + + @name = name + + raise ArgumentError.new("Invalid VIN: #{ vin }") if vin.length != 17 + @vin = vin + + raise ArgumentError.new("Invalid status: #{ status }") unless [:AVAILABLE, :UNAVAILABLE].include? (status.to_sym) + @status = status.to_sym + + @trips = trips + end + + def add_trip(trip) + @trips << trip + end + + + def average_rating + return 0 if @trips.length == 0 + + total = @trips.sum { |trip| trip.rating } + average = total.to_f / @trips.length + return average + end + + def total_revenue + return 0 if @trips.length == 0 + + total = @trips.sum do |trip| + if trip.cost < 1.65 + fee = 0 + else + fee = 1.65 + end + ((trip.cost - fee) * 0.8) + end + return total.round(2) + 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 diff --git a/lib/trip.rb b/lib/trip.rb index b93336e25..b255ff19a 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -5,12 +5,14 @@ module RideShare class Trip < CsvRecord - attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating + attr_reader :id, :passenger, :passenger_id,:driver, :driver_id, :start_time, :end_time, :cost, :rating def initialize( id:, passenger: nil, passenger_id: nil, + driver: nil, + driver_id: nil, start_time:, end_time:, cost: nil, @@ -29,6 +31,19 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end + + + 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 + @start_time = start_time @end_time = end_time @@ -58,9 +73,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 private @@ -69,6 +86,7 @@ def self.from_csv(record) return self.new( id: record[:id], passenger_id: record[:passenger_id], + driver_id: record[:driver_id], start_time: Time.parse(record[:start_time]), end_time: Time.parse(record[:end_time]), cost: record[:cost], diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 5130849f8..42c0dc391 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -11,6 +11,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 @@ -19,6 +20,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)} \ @@ -32,10 +38,11 @@ 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 + # this was trips as a local variable before + return @trips end end end diff --git a/test/driver_test.rb b/test/driver_test.rb index 4e6076ec2..9a2ea2a42 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( @@ -131,6 +131,35 @@ 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: 1, + rating: 5 + ) + trip_2 = RideShare::Trip.new( + id: 9, + driver: @driver, + passenger_id: 3, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 8), + cost: 10, + rating: 5 + ) + @driver.add_trip(trip) + @driver.add_trip(trip_2) + end + it "can calculate the total revenue for each driver" do + expect(@driver.total_revenue).must_equal 7.48 + end end end diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 36f484c59..6354db67c 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -44,9 +44,17 @@ phone_number: "1-602-620-2330 x3723", trips: [] ) + @driver = RideShare::Driver.new( + id: 2, + name: "Chris", + vin: "1B6CF40K1J3Y74UY2", + status: :AVAILABLE, + trips: [] + ) trip = RideShare::Trip.new( id: 8, passenger: @passenger, + driver: @driver, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), cost: 10.5, @@ -78,9 +86,17 @@ phone_number: "1-602-620-2330 x3723", trips: [] ) + @driver = RideShare::Driver.new( + id: 2, + name: "Chris", + vin: "1B6CF40K1J3Y74UY2", + status: :AVAILABLE, + trips: [] + ) trip = RideShare::Trip.new( id: 8, passenger: @passenger, + driver: @driver, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), cost: 10.5, @@ -89,6 +105,7 @@ trip_2 = RideShare::Trip.new( id: 9, passenger: @passenger, + driver: @driver, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), cost: 9, @@ -113,9 +130,17 @@ phone_number: "1-602-620-2330 x3723", trips: [] ) + @driver = RideShare::Driver.new( + id: 2, + name: "Chris", + vin: "1B6CF40K1J3Y74UY2", + status: :AVAILABLE, + trips: [] + ) trip = RideShare::Trip.new( id: 8, passenger: @passenger, + driver: @driver, start_time: Time.parse("2018-12-27 01:39:05 -0800"), end_time: Time.parse("2018-12-27 02:39:05 -0800"), cost: 10.5, @@ -124,6 +149,7 @@ trip_2 = RideShare::Trip.new( id: 9, passenger: @passenger, + driver: @driver, start_time: Time.parse("2018-12-27 01:39:05 -0800"), end_time: Time.parse("2018-12-27 01:59:05 -0800"), cost: 9, diff --git a/test/test_data/trips.csv b/test/test_data/trips.csv index 636dac122..4e5891e7e 100644 --- a/test/test_data/trips.csv +++ b/test/test_data/trips.csv @@ -3,4 +3,4 @@ id,driver_id,passenger_id,start_time,end_time,cost,rating 2,1,3,2018-05-25 04:39:00 -0700,2018-05-25 04:55:00 -0700,7,3 3,2,4,2018-06-11 22:22:00 -0700,2018-06-11 22:57:00 -0700,15,4 4,2,7,2018-08-12 15:04:00 -0700,2018-08-12 15:14:00 -0700,8,1 -5,2,6,2018-08-05 08:58:00 -0700,2018-08-05 09:30:00 -0700,32,1 +5,2,6,2018-08-05 08:58:00 -0700,2018-08-05 09:30:00 -0700,32,1 \ No newline at end of file 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' diff --git a/test/trip_test.rb b/test/trip_test.rb index 4a621e8e1..a985a08c1 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -12,6 +12,12 @@ name: "Ada", phone_number: "412-432-7640" ), + driver: RideShare::Driver.new( + id: 2, + name: "Chris", + vin: "1B6CF40K1J3Y74UY2", + status: :AVAILABLE + ), start_time: start_time, end_time: end_time, cost: 23.45, @@ -52,6 +58,12 @@ name: "Ada", phone_number: "412-432-7640" ), + driver: RideShare::Driver.new( + id: 2, + name: "Chris", + vin: "1B6CF40K1J3Y74UY2", + status: :AVAILABLE + ), start_time: start_time, end_time: end_time, cost: 23.45, From 44595c21cba8b941cac7efa1f694178cea854b2c Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Wed, 30 Sep 2020 12:05:54 -0700 Subject: [PATCH 03/10] wave 3 --- lib/driver.rb | 4 ++++ lib/trip.rb | 7 ++++--- lib/trip_dispatcher.rb | 12 ++++++++++++ test/trip_dispatch_test.rb | 19 ++++++++++++++++++- test/trip_test.rb | 1 - 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 18c04f022..b0fca8f4e 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -45,6 +45,10 @@ def total_revenue return total.round(2) end + def trip_in_progress + @status = :UNAVAILABLE + end + private def self.from_csv(record) diff --git a/lib/trip.rb b/lib/trip.rb index b255ff19a..612f343ae 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -47,13 +47,14 @@ def initialize( @start_time = start_time @end_time = end_time - raise ArgumentError.new("Start time should occur before the end time") if @start_time > @end_time + raise ArgumentError.new("Start time should occur before the end time") if @start_time > @end_time unless @end_time.nil? @cost = cost @rating = rating - if @rating > 5 || @rating < 1 - raise ArgumentError.new("Invalid rating #{@rating}") + unless @rating.nil? + raise ArgumentError.new("Invalid rating #{@rating}") if @rating > 5 || @rating < 1 end + end def duration diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 42c0dc391..e9077b533 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -25,6 +25,18 @@ def find_driver(id) return @drivers.find { |driver| driver.id == id } end + def request_trip(passenger_id) + passenger = find_passenger(passenger_id) + # do we need "find_driver" to validate driver_id? + driver = @drivers.find { |driver| driver.status == :AVAILABLE } + trip = Trip.new(id: 601, passenger: passenger, passenger_id: passenger_id, driver: driver, driver_id: driver.id, start_time: Time.now, end_time: nil, cost: nil, rating: nil) + + passenger.add_trip(trip) + driver.add_trip(trip) + driver.trip_in_progress + return trip + end + def inspect # Make puts output more useful return "#<#{self.class.name}:0x#{object_id.to_s(16)} \ diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 21f4457b7..0a12aa187 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 @@ -122,4 +122,21 @@ def build_test_dispatcher end end end + + describe "request a new trip" do + before do + @dispatcher = build_test_dispatcher + end + + passenger_id = 6 + it "works for request_trip" do + expect(@dispatcher.request_trip(passenger_id)).must_be_kind_of RideShare::Trip + end + + it "finds the first available driver and changes status to :UNAVAILABLE" do + expect(@dispatcher.request_trip(passenger_id).driver_id).must_equal 2 + expect(@dispatcher.request_trip(passenger_id).driver.status).must_equal :UNAVAILABLE + end + + end end diff --git a/test/trip_test.rb b/test/trip_test.rb index a985a08c1..69dea44cd 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -35,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 780b302bb9bc40b830e4c542a1d596c1b12bf933 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Wed, 30 Sep 2020 16:40:03 -0700 Subject: [PATCH 04/10] changed trip id creation on line 32 and pushed trip into @trips list for trip_dispatcher on line 33. wrote a list of tests to be written. --- lib/trip_dispatcher.rb | 4 ++-- test/trip_dispatch_test.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index e9077b533..bd21e5d5f 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -29,8 +29,8 @@ def request_trip(passenger_id) passenger = find_passenger(passenger_id) # do we need "find_driver" to validate driver_id? driver = @drivers.find { |driver| driver.status == :AVAILABLE } - trip = Trip.new(id: 601, passenger: passenger, passenger_id: passenger_id, driver: driver, driver_id: driver.id, start_time: Time.now, end_time: nil, cost: nil, rating: nil) - + trip = Trip.new(id: @trips.last.id + 1, passenger: passenger, passenger_id: passenger_id, driver: driver, driver_id: driver.id, start_time: Time.now, end_time: nil, cost: nil, rating: nil) + @trips << trip passenger.add_trip(trip) driver.add_trip(trip) driver.trip_in_progress diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 0a12aa187..c163a8c37 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -140,3 +140,21 @@ def build_test_dispatcher end end + +# TESTS NEEDED FOR WAVE 3 # + +# - Finds the right passenger for passenger_id, and adds trip to passenger trip list +# - Finds the first available driver, and adds trip to driver's trip list +# - Ensure returned trip has the right parameters +# - @trips list should increase by 1 whenever request_trip is (SEE LINE 33) +# - trip ID should be incrementally higher than last trip id (SEE LINE 32) +# - If request trip twice, both new trips should have unique IDs +# +# - Ignore average rating, duration and revenue calculation on trips in-progress +# +# - Edge case -- what happens if all drivers are unavailable? +# should raise ArgumentError for nil driver +# - Edge case -- what happens if there are NO DRIVERS? +# should raise ArgumentError for nil driver +# - Edge case -- invalid passenger ID +# raise ArgumentError for nil passenger \ No newline at end of file From 43c9cb6fb23cea9e98fe6e5356e19d1bda0df613 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Thu, 1 Oct 2020 12:40:22 -0700 Subject: [PATCH 05/10] wave 3 updates and edge cases added --- lib/driver.rb | 31 ++++++++++++------ lib/passenger.rb | 6 ++-- lib/trip.rb | 16 ++++----- lib/trip_dispatcher.rb | 3 ++ test/driver_test.rb | 31 +++++++++++++++++- test/passenger_test.rb | 32 +++++++++++++++++- test/trip_dispatch_test.rb | 66 +++++++++++++++++++++++++++++--------- 7 files changed, 148 insertions(+), 37 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index b0fca8f4e..6500b8d0a 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -24,23 +24,34 @@ def add_trip(trip) def average_rating - return 0 if @trips.length == 0 + length = @trips.length + return 0 if length == 0 + + total = 0 - total = @trips.sum { |trip| trip.rating } - average = total.to_f / @trips.length - return average + @trips.each do |trip| + total += trip.rating unless trip.rating.nil? + if trip.rating.nil? + length -= 1 + end + end + average = total.to_f / length + return average.round(1) end def total_revenue return 0 if @trips.length == 0 - total = @trips.sum do |trip| - if trip.cost < 1.65 - fee = 0 - else - fee = 1.65 + total = 0 + + @trips.each do |trip| + if trip.cost.nil? + next + elsif trip.cost <= 1.65 + total += (trip.cost * 0.8) + elsif + total += ((trip.cost - 1.65) * 0.8) end - ((trip.cost - fee) * 0.8) end return total.round(2) end diff --git a/lib/passenger.rb b/lib/passenger.rb index 4f4e900f1..40d9e2d61 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -17,7 +17,10 @@ def add_trip(trip) end def net_expenditures - total = @trips.sum { |trip| trip.cost } + total = 0 + @trips.each do |trip| + total += trip.cost unless trip.cost.nil? + end return total end @@ -26,7 +29,6 @@ def total_time_spent return total end - private def self.from_csv(record) diff --git a/lib/trip.rb b/lib/trip.rb index 612f343ae..70ed4ff27 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -31,8 +31,6 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end - - if driver @driver = driver @driver_id = driver.id @@ -46,19 +44,21 @@ def initialize( @start_time = start_time @end_time = end_time - raise ArgumentError.new("Start time should occur before the end time") if @start_time > @end_time unless @end_time.nil? + @cost = cost - @rating = rating - unless @rating.nil? - raise ArgumentError.new("Invalid rating #{@rating}") if @rating > 5 || @rating < 1 - end + @rating = rating + raise ArgumentError.new("Invalid rating #{@rating}") if @rating > 5 || @rating < 1 unless @rating.nil? end def duration - duration = @end_time - @start_time + if @end_time.nil? + duration = 0 + else + duration = @end_time - @start_time + end return duration.to_i end diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index bd21e5d5f..dc3aabb6d 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -29,6 +29,9 @@ def request_trip(passenger_id) passenger = find_passenger(passenger_id) # do we need "find_driver" to validate driver_id? driver = @drivers.find { |driver| driver.status == :AVAILABLE } + + raise ArgumentError.new("No available drivers") if driver.nil? + trip = Trip.new(id: @trips.last.id + 1, passenger: passenger, passenger_id: passenger_id, driver: driver, driver_id: driver.id, start_time: Time.now, end_time: nil, cost: nil, rating: nil) @trips << trip passenger.add_trip(trip) diff --git a/test/driver_test.rb b/test/driver_test.rb index 9a2ea2a42..e6d334051 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -115,7 +115,7 @@ expect(driver.average_rating).must_equal 0 end - it "correctly calculates the average rating" do + it "correctly calculates the average rating for a driver with an in-progress trip" do trip2 = RideShare::Trip.new( id: 8, driver: @driver, @@ -127,6 +127,20 @@ @driver.add_trip(trip2) expect(@driver.average_rating).must_be_close_to (5.0 + 1.0) / 2.0, 0.01 + + # add in-progress riding with rating = nil + trip3 = RideShare::Trip.new( + id: 9, + driver: @driver, + passenger_id: 6, + start_time: Time.now, + end_time: nil, + rating: nil, + cost: nil + ) + @driver.add_trip(trip3) + + expect(@driver.average_rating).must_be_close_to (5.0 + 1.0) / 2.0, 0.01 end end @@ -161,5 +175,20 @@ it "can calculate the total revenue for each driver" do expect(@driver.total_revenue).must_equal 7.48 end + + it "can calculate the total revenue for a driver with a trip in-progress" do + trip3 = RideShare::Trip.new( + id: 10, + driver: @driver, + passenger_id: 6, + start_time: Time.now, + end_time: nil, + rating: nil, + cost: nil + ) + @driver.add_trip(trip3) + + expect(@driver.total_revenue).must_equal 7.48 + end end end diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 6354db67c..e87b08598 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -119,6 +119,21 @@ it "can calculate the total spending for each passenger" do expect(@passenger.net_expenditures).must_equal 19.5 end + + it "can calculate total spending for a passenger with a trip in-progress" do + trip_3 = RideShare::Trip.new( + id: 10, + passenger: @passenger, + driver: @driver, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil + ) + @passenger.add_trip(trip_3) + expect(@passenger.net_expenditures).must_equal 19.5 + end + end describe "total_time_spent" do @@ -160,9 +175,24 @@ @passenger.add_trip(trip_2) end - it "can calculate the total spending for each passenger" do + it "can calculate the total time spent for each passenger" do expect(@passenger.total_time_spent).must_equal 4800 end + + it "can calculate total time spent for a passenger with a trip in-progress" do + trip_3 = RideShare::Trip.new( + id: 10, + passenger: @passenger, + driver: @driver, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil + ) + @passenger.add_trip(trip_3) + expect(@passenger.total_time_spent).must_equal 4800 + end + end end diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index c163a8c37..0c0cd4cb1 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -138,23 +138,59 @@ def build_test_dispatcher expect(@dispatcher.request_trip(passenger_id).driver.status).must_equal :UNAVAILABLE end + it "checks that requested trip has the right parameters" do + + passenger_id = 6 + trip = @dispatcher.request_trip(passenger_id) + + expect(trip.passenger_id).must_equal 6 + expect(trip.id).must_equal 6 + expect(trip.end_time).must_be_nil + expect(trip.cost).must_be_nil + expect(trip.rating).must_be_nil + + end + + it "checks that trip id increments by 1" do + + # passenger_id = 1 + # trip = @dispatcher.request_trip(passenger_id) + # + # expect(trip.passenger_id).must_equal 1 + # expect(trip.id).must_equal 6 + # + # passenger_id = 2 + # trip = @dispatcher.request_trip(passenger_id) + # + # expect(trip.passenger_id).must_equal 2 + # expect(trip.id).must_equal 7 + + + array = [6, 7] + trip_id = 5 + + array.each do |pass| + trip = @dispatcher.request_trip(pass) + expect(trip.id).must_equal trip_id += 1 + end + + end + + it "raises an error for no driver available" do + array = [6, 7, 3] + + expect{ + array.each do |pass| + @dispatcher.request_trip(pass) + end}.must_raise ArgumentError + + end + end end # TESTS NEEDED FOR WAVE 3 # -# - Finds the right passenger for passenger_id, and adds trip to passenger trip list -# - Finds the first available driver, and adds trip to driver's trip list -# - Ensure returned trip has the right parameters -# - @trips list should increase by 1 whenever request_trip is (SEE LINE 33) -# - trip ID should be incrementally higher than last trip id (SEE LINE 32) -# - If request trip twice, both new trips should have unique IDs -# -# - Ignore average rating, duration and revenue calculation on trips in-progress -# -# - Edge case -- what happens if all drivers are unavailable? -# should raise ArgumentError for nil driver -# - Edge case -- what happens if there are NO DRIVERS? -# should raise ArgumentError for nil driver -# - Edge case -- invalid passenger ID -# raise ArgumentError for nil passenger \ No newline at end of file +# - adds trip to passenger trip list +# - and adds trip to driver's trip list + From 29f5854cb3727165781321925ca51e195f6d7bc2 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Thu, 1 Oct 2020 14:12:22 -0700 Subject: [PATCH 06/10] wave 3 tests passing --- test/driver_test.rb | 15 +++++++++++-- test/trip_dispatch_test.rb | 46 ++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/test/driver_test.rb b/test/driver_test.rb index e6d334051..c65100cb2 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -115,7 +115,7 @@ expect(driver.average_rating).must_equal 0 end - it "correctly calculates the average rating for a driver with an in-progress trip" do + it "correctly calculates the average rating for a driver" do trip2 = RideShare::Trip.new( id: 8, driver: @driver, @@ -127,8 +127,17 @@ @driver.add_trip(trip2) expect(@driver.average_rating).must_be_close_to (5.0 + 1.0) / 2.0, 0.01 + end - # add in-progress riding with rating = nil + it "correctly calculates the average rating for a driver with an in-progress trip" do + trip2 = RideShare::Trip.new( + id: 8, + driver: @driver, + passenger_id: 3, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + rating: 1 + ) trip3 = RideShare::Trip.new( id: 9, driver: @driver, @@ -138,6 +147,8 @@ rating: nil, cost: nil ) + + @driver.add_trip(trip2) @driver.add_trip(trip3) expect(@driver.average_rating).must_be_close_to (5.0 + 1.0) / 2.0, 0.01 diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 0c0cd4cb1..1451e8c14 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -139,7 +139,6 @@ def build_test_dispatcher end it "checks that requested trip has the right parameters" do - passenger_id = 6 trip = @dispatcher.request_trip(passenger_id) @@ -148,24 +147,9 @@ def build_test_dispatcher expect(trip.end_time).must_be_nil expect(trip.cost).must_be_nil expect(trip.rating).must_be_nil - end it "checks that trip id increments by 1" do - - # passenger_id = 1 - # trip = @dispatcher.request_trip(passenger_id) - # - # expect(trip.passenger_id).must_equal 1 - # expect(trip.id).must_equal 6 - # - # passenger_id = 2 - # trip = @dispatcher.request_trip(passenger_id) - # - # expect(trip.passenger_id).must_equal 2 - # expect(trip.id).must_equal 7 - - array = [6, 7] trip_id = 5 @@ -173,7 +157,6 @@ def build_test_dispatcher trip = @dispatcher.request_trip(pass) expect(trip.id).must_equal trip_id += 1 end - end it "raises an error for no driver available" do @@ -183,14 +166,39 @@ def build_test_dispatcher array.each do |pass| @dispatcher.request_trip(pass) end}.must_raise ArgumentError + end + + it "checks a trip is added to passenger trips" do + passenger_id = 6 + trip = @dispatcher + expect(trip.find_passenger(passenger_id).trips.count).must_equal 1 + + # after request a trip + trip.request_trip(passenger_id) + expect(trip.find_passenger(passenger_id).trips.count).must_equal 2 end + it "checks a trip is added to driver trips" do + + passenger_id = 6 + driver_id = 2 + + trip = @dispatcher + + # trip = @dispatcher.request_trip(passenger_id) + expect(trip.find_driver(driver_id).trips.count).must_equal 3 + + # after request a trip + trip.request_trip(passenger_id) + expect(trip.find_driver(driver_id).trips.count).must_equal 4 + + end end end # TESTS NEEDED FOR WAVE 3 # -# - adds trip to passenger trip list -# - and adds trip to driver's trip list +# - checks if adds trip to passenger trip list +# - checks if and adds trip to driver's trip list From e79cc2c3f23436878649b821c11236d1bfe73370 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Thu, 1 Oct 2020 14:45:42 -0700 Subject: [PATCH 07/10] wave 4 plus tests --- lib/trip_dispatcher.rb | 10 ++++++++-- test/trip_dispatch_test.rb | 29 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index dc3aabb6d..5fee9ab8d 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -28,9 +28,15 @@ def find_driver(id) def request_trip(passenger_id) passenger = find_passenger(passenger_id) # do we need "find_driver" to validate driver_id? - driver = @drivers.find { |driver| driver.status == :AVAILABLE } + drivers_available = @drivers.select { |driver| driver.status == :AVAILABLE } - raise ArgumentError.new("No available drivers") if driver.nil? + raise ArgumentError.new("No available drivers") if drivers_available.empty? + + if drivers_available.any? { |candidate| candidate.trips.count == 0 } + driver = drivers_available.find { |candidate| candidate.trips.count == 0 } + else + driver = drivers_available.min_by { |candidate| candidate.trips.last.end_time } + end trip = Trip.new(id: @trips.last.id + 1, passenger: passenger, passenger_id: passenger_id, driver: driver, driver_id: driver.id, start_time: Time.now, end_time: nil, cost: nil, rating: nil) @trips << trip diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 1451e8c14..e7cd44d6c 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -127,14 +127,27 @@ def build_test_dispatcher before do @dispatcher = build_test_dispatcher end - passenger_id = 6 + it "works for request_trip" do expect(@dispatcher.request_trip(passenger_id)).must_be_kind_of RideShare::Trip end - it "finds the first available driver and changes status to :UNAVAILABLE" do + xit "finds the first available driver" do expect(@dispatcher.request_trip(passenger_id).driver_id).must_equal 2 + end + + it "(Wave 4) verify selected driver is :AVAILABLE, and change driver status to :UNAVAILABLE when trip is in-progress" do + driver_id = 3 + # before request a trip + expect(@dispatcher.find_driver(driver_id).status).must_equal :AVAILABLE + # after request the trip + @dispatcher.request_trip(passenger_id) + expect(@dispatcher.find_driver(driver_id).status).must_equal :UNAVAILABLE + end + + it "(Wave 4) Intelligent Dispatching: selects the available driver with 0 trips first" do + expect(@dispatcher.request_trip(passenger_id).driver_id).must_equal 3 expect(@dispatcher.request_trip(passenger_id).driver.status).must_equal :UNAVAILABLE end @@ -182,20 +195,26 @@ def build_test_dispatcher it "checks a trip is added to driver trips" do passenger_id = 6 - driver_id = 2 + driver_id = 3 trip = @dispatcher # trip = @dispatcher.request_trip(passenger_id) - expect(trip.find_driver(driver_id).trips.count).must_equal 3 + expect(trip.find_driver(driver_id).trips.count).must_equal 0 # after request a trip trip.request_trip(passenger_id) - expect(trip.find_driver(driver_id).trips.count).must_equal 4 + expect(trip.find_driver(driver_id).trips.count).must_equal 1 end end end +# describe "Intelligent dispatching wave 4" do +# before do +# @dispatcher = build_test_dispatcher +# end +# +# passenger_id = 6 # TESTS NEEDED FOR WAVE 3 # From ec6c774233d808db59778226c32ff1013e4d4bf0 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Thu, 1 Oct 2020 14:51:25 -0700 Subject: [PATCH 08/10] Fancy ternary operation --- lib/trip.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 70ed4ff27..7772214b5 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -31,16 +31,20 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end - if driver - @driver = driver - @driver_id = driver.id + raise ArgumentError, 'Driver or driver_id is required' unless driver || driver_id + @driver = driver + @driver.nil? ? @driver_id = driver_id : @driver_id = driver.id - elsif driver_id - @driver_id = driver_id + # if driver + # @driver = driver + # @driver_id = driver.id - else - raise ArgumentError, 'Driver or driver_id is required' - end + # elsif driver_id + # @driver_id = driver_id + + # else + # raise ArgumentError, 'Driver or driver_id is required' + # end @start_time = start_time @end_time = end_time @@ -68,6 +72,7 @@ def inspect "#<#{self.class.name}:0x#{self.object_id.to_s(16)} " + "id=#{id.inspect} " + "passenger_id=#{passenger&.id.inspect} " + + "driver_id=#{driver&.id.inspect} " + "start_time=#{start_time} " + "end_time=#{end_time} " + "cost=#{cost} " + From 6f1b43ab17a34b2d0510a097d41bb39a245cc4d6 Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Thu, 1 Oct 2020 15:23:21 -0700 Subject: [PATCH 09/10] fixed formatting and removed extra comments --- lib/driver.rb | 1 - lib/trip.rb | 11 ---- lib/trip_dispatcher.rb | 4 +- test/driver_test.rb | 28 ++++----- test/passenger_test.rb | 119 ++++++++++++++++++------------------- test/trip_dispatch_test.rb | 17 +----- 6 files changed, 75 insertions(+), 105 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 6500b8d0a..879a3f166 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -22,7 +22,6 @@ def add_trip(trip) @trips << trip end - def average_rating length = @trips.length return 0 if length == 0 diff --git a/lib/trip.rb b/lib/trip.rb index 7772214b5..dbcc30268 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -35,17 +35,6 @@ def initialize( @driver = driver @driver.nil? ? @driver_id = driver_id : @driver_id = driver.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 - @start_time = start_time @end_time = end_time raise ArgumentError.new("Start time should occur before the end time") if @start_time > @end_time unless @end_time.nil? diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 5fee9ab8d..b5201f993 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -27,7 +27,6 @@ def find_driver(id) def request_trip(passenger_id) passenger = find_passenger(passenger_id) - # do we need "find_driver" to validate driver_id? drivers_available = @drivers.select { |driver| driver.status == :AVAILABLE } raise ArgumentError.new("No available drivers") if drivers_available.empty? @@ -62,8 +61,7 @@ def connect_trips driver = find_driver(trip.driver_id) trip.connect(passenger, driver) end - # this was trips as a local variable before - return @trips + return trips end end end diff --git a/test/driver_test.rb b/test/driver_test.rb index c65100cb2..badbf0770 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -84,7 +84,7 @@ id: 54, name: "Rogers Bartell IV", vin: "1C9EVBRM0YBC564DZ" - ) + ) trip = RideShare::Trip.new( id: 8, driver: @driver, @@ -92,7 +92,7 @@ start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 8), rating: 5 - ) + ) @driver.add_trip(trip) end @@ -111,7 +111,7 @@ id: 54, name: "Rogers Bartell IV", vin: "1C9EVBRM0YBC564DZ" - ) + ) expect(driver.average_rating).must_equal 0 end @@ -123,7 +123,7 @@ start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), rating: 1 - ) + ) @driver.add_trip(trip2) expect(@driver.average_rating).must_be_close_to (5.0 + 1.0) / 2.0, 0.01 @@ -131,13 +131,13 @@ it "correctly calculates the average rating for a driver with an in-progress trip" do trip2 = RideShare::Trip.new( - id: 8, - driver: @driver, - passenger_id: 3, - start_time: Time.new(2016, 8, 8), - end_time: Time.new(2016, 8, 9), - rating: 1 - ) + id: 8, + driver: @driver, + passenger_id: 3, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + rating: 1 + ) trip3 = RideShare::Trip.new( id: 9, driver: @driver, @@ -146,7 +146,7 @@ end_time: nil, rating: nil, cost: nil - ) + ) @driver.add_trip(trip2) @driver.add_trip(trip3) @@ -188,7 +188,7 @@ end it "can calculate the total revenue for a driver with a trip in-progress" do - trip3 = RideShare::Trip.new( + trip_3 = RideShare::Trip.new( id: 10, driver: @driver, passenger_id: 6, @@ -197,7 +197,7 @@ rating: nil, cost: nil ) - @driver.add_trip(trip3) + @driver.add_trip(trip_3) expect(@driver.total_revenue).must_equal 7.48 end diff --git a/test/passenger_test.rb b/test/passenger_test.rb index e87b08598..03e0a5ec2 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -37,7 +37,6 @@ describe "trips property" do before do - # TODO: you'll need to add a driver at some point here. @passenger = RideShare::Passenger.new( id: 9, name: "Merl Glover III", @@ -79,13 +78,12 @@ describe "net_expenditures" do before do - # TODO: you'll need to add a driver at some point here. @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: [] + ) @driver = RideShare::Driver.new( id: 2, name: "Chris", @@ -94,23 +92,23 @@ trips: [] ) trip = RideShare::Trip.new( - id: 8, - passenger: @passenger, - driver: @driver, - start_time: Time.new(2016, 8, 8), - end_time: Time.new(2016, 8, 9), - cost: 10.5, - rating: 5 - ) + id: 8, + passenger: @passenger, + driver: @driver, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + cost: 10.5, + rating: 5 + ) trip_2 = RideShare::Trip.new( - id: 9, - passenger: @passenger, - driver: @driver, - start_time: Time.new(2016, 8, 8), - end_time: Time.new(2016, 8, 9), - cost: 9, - rating: 5 - ) + id: 9, + passenger: @passenger, + driver: @driver, + start_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 8, 9), + cost: 9, + rating: 5 + ) @passenger.add_trip(trip) @passenger.add_trip(trip_2) @@ -122,14 +120,14 @@ it "can calculate total spending for a passenger with a trip in-progress" do trip_3 = RideShare::Trip.new( - id: 10, - passenger: @passenger, - driver: @driver, - start_time: Time.now, - end_time: nil, - cost: nil, - rating: nil - ) + id: 10, + passenger: @passenger, + driver: @driver, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil + ) @passenger.add_trip(trip_3) expect(@passenger.net_expenditures).must_equal 19.5 end @@ -138,13 +136,12 @@ describe "total_time_spent" do before do - # TODO: you'll need to add a driver at some point here. @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: [] + ) @driver = RideShare::Driver.new( id: 2, name: "Chris", @@ -153,23 +150,23 @@ trips: [] ) trip = RideShare::Trip.new( - id: 8, - passenger: @passenger, - driver: @driver, - start_time: Time.parse("2018-12-27 01:39:05 -0800"), - end_time: Time.parse("2018-12-27 02:39:05 -0800"), - cost: 10.5, - rating: 5 - ) + id: 8, + passenger: @passenger, + driver: @driver, + start_time: Time.parse("2018-12-27 01:39:05 -0800"), + end_time: Time.parse("2018-12-27 02:39:05 -0800"), + cost: 10.5, + rating: 5 + ) trip_2 = RideShare::Trip.new( - id: 9, - passenger: @passenger, - driver: @driver, - start_time: Time.parse("2018-12-27 01:39:05 -0800"), - end_time: Time.parse("2018-12-27 01:59:05 -0800"), - cost: 9, - rating: 5 - ) + id: 9, + passenger: @passenger, + driver: @driver, + start_time: Time.parse("2018-12-27 01:39:05 -0800"), + end_time: Time.parse("2018-12-27 01:59:05 -0800"), + cost: 9, + rating: 5 + ) @passenger.add_trip(trip) @passenger.add_trip(trip_2) @@ -181,14 +178,14 @@ it "can calculate total time spent for a passenger with a trip in-progress" do trip_3 = RideShare::Trip.new( - id: 10, - passenger: @passenger, - driver: @driver, - start_time: Time.now, - end_time: nil, - cost: nil, - rating: nil - ) + id: 10, + passenger: @passenger, + driver: @driver, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil + ) @passenger.add_trip(trip_3) expect(@passenger.total_time_spent).must_equal 4800 end diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index e7cd44d6c..7a928f49b 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -23,7 +23,7 @@ def build_test_dispatcher expect(dispatcher.trips).must_be_kind_of Array expect(dispatcher.passengers).must_be_kind_of Array - # expect(dispatcher.drivers).must_be_kind_of Array + expect(dispatcher.drivers).must_be_kind_of Array end it "loads the development data by default" do @@ -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 @@ -199,9 +198,8 @@ def build_test_dispatcher trip = @dispatcher - # trip = @dispatcher.request_trip(passenger_id) + # before request a trip expect(trip.find_driver(driver_id).trips.count).must_equal 0 - # after request a trip trip.request_trip(passenger_id) expect(trip.find_driver(driver_id).trips.count).must_equal 1 @@ -209,15 +207,4 @@ def build_test_dispatcher end end end -# describe "Intelligent dispatching wave 4" do -# before do -# @dispatcher = build_test_dispatcher -# end -# -# passenger_id = 6 - -# TESTS NEEDED FOR WAVE 3 # - -# - checks if adds trip to passenger trip list -# - checks if and adds trip to driver's trip list From 058cd129a0c0b25aef7fe3125bcee2c010c9e88f Mon Sep 17 00:00:00 2001 From: Sophie Messing Date: Mon, 5 Oct 2020 10:46:16 -0700 Subject: [PATCH 10/10] refactored intelligent_dispatch into a method --- lib/trip_dispatcher.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index b5201f993..d6aa94421 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -25,10 +25,8 @@ def find_driver(id) return @drivers.find { |driver| driver.id == id } end - def request_trip(passenger_id) - passenger = find_passenger(passenger_id) + def intelligent_dispatch drivers_available = @drivers.select { |driver| driver.status == :AVAILABLE } - raise ArgumentError.new("No available drivers") if drivers_available.empty? if drivers_available.any? { |candidate| candidate.trips.count == 0 } @@ -36,8 +34,15 @@ def request_trip(passenger_id) else driver = drivers_available.min_by { |candidate| candidate.trips.last.end_time } end + return driver + end + + def request_trip(passenger_id) + passenger = find_passenger(passenger_id) + driver = intelligent_dispatch trip = Trip.new(id: @trips.last.id + 1, passenger: passenger, passenger_id: passenger_id, driver: driver, driver_id: driver.id, start_time: Time.now, end_time: nil, cost: nil, rating: nil) + @trips << trip passenger.add_trip(trip) driver.add_trip(trip)