From 64d1b3d1408b0dfd1ca1513aed69a68bff9dfe0e Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Mon, 24 Feb 2020 16:46:15 -0800 Subject: [PATCH 01/21] Attempted Wave 1.1: Upgrading Times --- Gemfile | 2 +- Gemfile.lock | 34 ++++++++++++++++++++++++++++++++++ lib/trip.rb | 21 +++++++++++++++++---- lib/trip_dispatcher.rb | 1 + test/trip_test.rb | 2 ++ 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile index 63ffa5acd..2ed0f1e67 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'http://rubygems.org' -ruby '2.5.5' +ruby '2.6.3' gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 000000000..68b0850f8 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,34 @@ +GEM + remote: http://rubygems.org/ + specs: + ansi (1.5.0) + awesome_print (1.8.0) + builder (3.2.4) + csv (3.1.2) + minitest (5.14.0) + minitest-reporters (1.4.2) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + minitest-skip (0.0.1) + minitest (~> 5.0) + rake (13.0.1) + ruby-progressbar (1.10.1) + +PLATFORMS + ruby + +DEPENDENCIES + awesome_print + csv + minitest + minitest-reporters + minitest-skip + rake + +RUBY VERSION + ruby 2.6.3p62 + +BUNDLED WITH + 2.1.4 diff --git a/lib/trip.rb b/lib/trip.rb index 87ce496e9..5d73598c3 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,4 +1,5 @@ require 'csv' +require 'time' require_relative 'csv_record' @@ -6,6 +7,7 @@ module RideShare class Trip < CsvRecord attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating + # Add a check in Trip#initialize that raises an ArgumentError if the end time is before the start time, and a corresponding TEST def initialize( id:, passenger: nil, @@ -28,8 +30,15 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end - @start_time = start_time - @end_time = end_time + + if !(@end_time.to_i < @start_time.to_i) + @start_time = start_time + @end_time = end_time + else + raise ArgumentError, 'Those are invalid times' + end + + @cost = cost @rating = rating @@ -53,12 +62,16 @@ def connect(passenger) private + # Spend some time reading the docs for Time - you might be particularly interested in Time.parse + # Wave 1: Turn start_time and end_time into Time instances before passing them to Trip#initialize 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: record[:start_time], + start_time: Time.parse(record[:start_time]), + # end_time: record[:end_time], + end_time: Time.parse(record[:end_time]), cost: record[:cost], rating: record[:rating] ) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 5130849f8..802ef7bed 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -1,5 +1,6 @@ require 'csv' require 'time' +# require 'pry' require_relative 'passenger' require_relative 'trip' diff --git a/test/trip_test.rb b/test/trip_test.rb index 2063e28a7..75903e069 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -43,3 +43,5 @@ end end end + +# Add a check in Trip#initialize that raises an ArgumentError if the end time is before the start time, and a corresponding TEST \ No newline at end of file From e67c4cba40196bf4acec1c148eb12cb8156cc3b4 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Mon, 24 Feb 2020 19:42:55 -0800 Subject: [PATCH 02/21] Corrected error in trip.rb's ArgumentError for when end_time is not later than start_time --- lib/trip.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 5d73598c3..9e062a26d 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -31,7 +31,7 @@ def initialize( end - if !(@end_time.to_i < @start_time.to_i) + if end_time > start_time @start_time = start_time @end_time = end_time else @@ -66,15 +66,15 @@ def connect(passenger) # Wave 1: Turn start_time and end_time into Time instances before passing them to Trip#initialize def self.from_csv(record) return self.new( - id: record[:id], - passenger_id: record[:passenger_id], - # start_time: record[:start_time], - start_time: Time.parse(record[:start_time]), - # end_time: record[:end_time], - end_time: Time.parse(record[:end_time]), - cost: record[:cost], - rating: record[:rating] - ) + id: record[:id], + passenger_id: record[:passenger_id], + # start_time: record[:start_time], + start_time: Time.parse(record[:start_time]), + # end_time: record[:end_time], + end_time: Time.parse(record[:end_time]), + cost: record[:cost], + rating: record[:rating] + ) end end end From 18f60150ba6e2dc02aa96823edbb98f844393af3 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 25 Feb 2020 14:54:29 -0800 Subject: [PATCH 03/21] Added and passed test for raising ArgumentError when trip end time is earlier than start time --- lib/csv_record.rb | 1 + lib/passenger.rb | 4 ++++ lib/trip.rb | 9 ++++----- test/trip_test.rb | 24 +++++++++++++++++++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/csv_record.rb b/lib/csv_record.rb index 96319ab87..f2c2d617a 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -30,6 +30,7 @@ def self.validate_id(id) private + # Template method - doesn't know what to parse def self.from_csv(record) raise NotImplementedError, 'Implement me in a child class!' end diff --git a/lib/passenger.rb b/lib/passenger.rb index afd13d73b..f7895df38 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -25,5 +25,9 @@ def self.from_csv(record) phone_number: record[:phone_num] ) end + + # Wave 1: Add an instance method, net_expenditures, to Passenger that will return the total amount of money that passenger has spent on their trips + # Wave 1: Add an instance method, total_time_spent to Passenger that will return the total amount of time that passenger has spent on their trips + # Each of these methods must have tests. What happens if the passenger has no trips? end end diff --git a/lib/trip.rb b/lib/trip.rb index 9e062a26d..98dde4782 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -7,7 +7,6 @@ module RideShare class Trip < CsvRecord attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating - # Add a check in Trip#initialize that raises an ArgumentError if the end time is before the start time, and a corresponding TEST def initialize( id:, passenger: nil, @@ -30,7 +29,7 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end - + # Raises an ArgumentError if the end time is before the start time if end_time > start_time @start_time = start_time @end_time = end_time @@ -62,19 +61,19 @@ def connect(passenger) private - # Spend some time reading the docs for Time - you might be particularly interested in Time.parse # Wave 1: Turn start_time and end_time into Time instances before passing them to Trip#initialize + # Trip.from_csv overrides CsvRecord.from_csv def self.from_csv(record) return self.new( id: record[:id], passenger_id: record[:passenger_id], - # start_time: record[:start_time], start_time: Time.parse(record[:start_time]), - # end_time: record[:end_time], end_time: Time.parse(record[:end_time]), cost: record[:cost], rating: record[:rating] ) end + + # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds, and a corresponding test end end diff --git a/test/trip_test.rb b/test/trip_test.rb index 75903e069..93bd201ff 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -41,7 +41,25 @@ end.must_raise ArgumentError end end - end -end -# Add a check in Trip#initialize that raises an ArgumentError if the end time is before the start time, and a corresponding TEST \ No newline at end of file + # Test for ArgumentError if the end time is before the start time + it "raises an error if end time is before start time" do + start_time = Time.now + end_time = start_time - 25 * 60 # earlier than start_time + @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{RideShare::Trip.new(@trip_data)}.must_raise ArgumentError + end + + end +end \ No newline at end of file From 373936e88400ad0048e04c0ad9862652895d3307 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 25 Feb 2020 15:53:24 -0800 Subject: [PATCH 04/21] Added method to calculate tripduration in seconds, and wrote and passed corresponding test --- lib/trip.rb | 8 ++++++-- test/trip_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 98dde4782..2136c82a8 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -46,6 +46,12 @@ def initialize( end end + # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds, and a corresponding test + def calculate_duration + duration = @end_time - @start_time + return duration + end + def inspect # Prevent infinite loop when puts-ing a Trip # trip contains a passenger contains a trip contains a passenger... @@ -73,7 +79,5 @@ def self.from_csv(record) rating: record[:rating] ) end - - # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds, and a corresponding test end end diff --git a/test/trip_test.rb b/test/trip_test.rb index 93bd201ff..26556622c 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -1,4 +1,5 @@ require_relative 'test_helper' +require 'time' describe "Trip class" do describe "initialize" do @@ -62,4 +63,28 @@ end end + # Wave 1: Test for Trip class instance method to calculate the duration of the trip in seconds + describe "calculate duration" do + it "finds trip duration in seconds" do + start_time = Time.parse('2018-12-27 02:00:00 -0800') + end_time = Time.parse('2018-12-27 02:01:00 -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 + } + + @trip = RideShare::Trip.new(@trip_data) + + expect(@trip.calculate_duration).must_equal 60 + end + end end \ No newline at end of file From d25b549fae317ed5ce14ddb8b4ce1d24fb2d574e Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 25 Feb 2020 16:08:51 -0800 Subject: [PATCH 05/21] Added methods to passenger to find net expenditures and total time spent --- lib/passenger.rb | 18 ++++++++++++++++++ lib/trip.rb | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/passenger.rb b/lib/passenger.rb index f7895df38..853782ad5 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -16,6 +16,24 @@ def add_trip(trip) @trips << trip end + # Wave 1: method to return the total amount of money that passenger has spent on their trips + tests + def net_expenditures + total_cost = 0 + @trips.each do |trip| + total_cost += trip.cost + end + return total_cost + end + + # Wave 1: method to return the total amount of time that passenger has spent on their trips + tests + def total_time_spent + total_time = 0 + @trips.each do |trip| + total_time += trip.calculate_duration + end + return total_time + end + private def self.from_csv(record) diff --git a/lib/trip.rb b/lib/trip.rb index 2136c82a8..ede901d69 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -46,7 +46,7 @@ def initialize( end end - # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds, and a corresponding test + # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds def calculate_duration duration = @end_time - @start_time return duration From 9cb6a2f24d1c5f8bcd3bc1fc81a320b6f8e0458d Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 25 Feb 2020 21:12:08 -0800 Subject: [PATCH 06/21] Added test for Passenger net_expenditures method --- test/passenger_test.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index eb3a631df..b419c9143 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -68,7 +68,25 @@ end end + # Wave 1: tests for the net_expenditures method describe "net_expenditures" do - # You add tests for the net_expenditures method + it "returns correct total amount spent" do + # set up data + test_trips = [] + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: '2018-10-31 01:02:22 -0700', end_time: '2018-10-31 01:48:15 -0700', cost: 6, rating: 2) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: '2018-12-27 01:57:48 -0800', end_time: '2018-12-27 02:42:05 -0800', cost: 9, rating: 1) + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) + expect(passenger.net_expenditures).must_equal 15 + end + + it "shows when $0 spent" do + # set up data + test_trips = [] + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: '2018-10-31 01:02:22 -0700', end_time: '2018-10-31 01:48:15 -0700', cost: 0, rating: 2) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: '2018-12-27 01:57:48 -0800', end_time: '2018-12-27 02:42:05 -0800', cost: 0, rating: 1) + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) + expect(passenger.net_expenditures).must_equal 0 + end end + end From 2533a5ee9d8af2b23bf09b4aecc545c24b039bf4 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 25 Feb 2020 21:32:45 -0800 Subject: [PATCH 07/21] Added test for Passenger total_time_spent method --- lib/passenger.rb | 4 ---- test/passenger_test.rb | 26 ++++++++++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/passenger.rb b/lib/passenger.rb index 853782ad5..7ec05d556 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -43,9 +43,5 @@ def self.from_csv(record) phone_number: record[:phone_num] ) end - - # Wave 1: Add an instance method, net_expenditures, to Passenger that will return the total amount of money that passenger has spent on their trips - # Wave 1: Add an instance method, total_time_spent to Passenger that will return the total amount of time that passenger has spent on their trips - # Each of these methods must have tests. What happens if the passenger has no trips? end end diff --git a/test/passenger_test.rb b/test/passenger_test.rb index b419c9143..a6d12c71b 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -1,4 +1,5 @@ require_relative 'test_helper' +require 'time' describe "Passenger class" do @@ -71,22 +72,35 @@ # Wave 1: tests for the net_expenditures method describe "net_expenditures" do it "returns correct total amount spent" do - # set up data test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: '2018-10-31 01:02:22 -0700', end_time: '2018-10-31 01:48:15 -0700', cost: 6, rating: 2) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: '2018-12-27 01:57:48 -0800', end_time: '2018-12-27 02:42:05 -0800', cost: 9, rating: 1) + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1) passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) expect(passenger.net_expenditures).must_equal 15 end it "shows when $0 spent" do - # set up data test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: '2018-10-31 01:02:22 -0700', end_time: '2018-10-31 01:48:15 -0700', cost: 0, rating: 2) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: '2018-12-27 01:57:48 -0800', end_time: '2018-12-27 02:42:05 -0800', cost: 0, rating: 1) + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 0, rating: 2) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 0, rating: 1) passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) expect(passenger.net_expenditures).must_equal 0 end end + # Wave 1: tests for the total_time_spent method + describe "total_time_spent" do + it "returns correct total time spent" do + test_trips = [] + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1) + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) + expected_time = 0 + test_trips.each do |trip| + expected_time += trip.calculate_duration + end + expect(passenger.total_time_spent).must_equal expected_time + end + end + end From 0d7fab172b60359accff314930f084922b2e6ca8 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 25 Feb 2020 21:55:09 -0800 Subject: [PATCH 08/21] Created driver.rb file and began creating the Driver class --- lib/driver.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 lib/driver.rb diff --git a/lib/driver.rb b/lib/driver.rb new file mode 100644 index 000000000..6a3e2d466 --- /dev/null +++ b/lib/driver.rb @@ -0,0 +1,53 @@ +require_relative 'csv_record' + +# Since Driver inherits from CsvRecord, you'll need to implement the from_csv template method. Once you do, Driver.load_all should work (test this in pry). +# Use the provided tests to ensure that a Driver instance can be created successfully and that an ArgumentError is raised for an invalid status. +module RideShare + class Driver < CsvRecord + def initialize(id:, name: vin: status: trips:) + super(id) + @name = name + @vin = vin + @status = status + @trips = trips || [] + end + + def self.from_csv(record) + return self.new( + id: record[:id], + name: record[:name], + vin: record[:vin], + status: record[:status].to_sym + ) + end + + end +end + +# we will need to update the Trip class to include a reference to the trip's driver. +# Add the following attributes to the Trip class: + +# Attribute | Description +# driver_id | The ID of the driver for this trip +# driver | The Driver instance for the trip + +# When a Trip is constructed, either driver_id or driver must be provided. + +# Note: You have changed the method signature of the constructor for Trip. Some of your tests may now be failing. Go fix them! + +# Update the TripDispatcher class as follows: + +# In the constructor, call Driver.load_all and save the result in an instance variable +# Update the Trip#connect method to connect the driver as well as the passenger (you'll want to create add trip on driver first - see below) +# Add a find_driver method that looks up a driver by ID + +# After each Trip has a reference to its Driver and TripDispatcher can load a list of Drivers, add the following functionality to the Driver class: + +# Method | Description | Test Cases +# add_trip | Add a trip to the driver's list of trips | Try adding a trip +# average_rating | What is this driver's average rating? | What if there are no trips? Does it handle floating point division correctly? For example the average of 2 and 3 should be 2.5, not 2. + +# total_revenue | This method calculates that driver's total revenue across all their trips. Each driver gets 80% of the trip cost after a fee of $1.65 per trip is subtracted. +# | What if there are no trips? What if the cost of a trip was less that $1.65? + +# All the new methods above should have tests \ 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' From cff5926755f34965df8990f35e6b2c0f04afe073 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Wed, 26 Feb 2020 16:38:52 -0800 Subject: [PATCH 09/21] Added methods for Driver class and attempted to pass tests --- lib/driver.rb | 54 +++++++++++++++++++++++++++++++----------- lib/passenger.rb | 2 +- lib/trip.rb | 29 +++++++++++++++++++---- lib/trip_dispatcher.rb | 19 ++++++++++++++- test/driver_test.rb | 36 +++++++++++++++++++++++++++- test/passenger_test.rb | 12 +++++----- 6 files changed, 124 insertions(+), 28 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 6a3e2d466..e292b3236 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,37 +1,63 @@ -require_relative 'csv_record' +require_relative "csv_record" # Since Driver inherits from CsvRecord, you'll need to implement the from_csv template method. Once you do, Driver.load_all should work (test this in pry). # Use the provided tests to ensure that a Driver instance can be created successfully and that an ArgumentError is raised for an invalid status. module RideShare class Driver < CsvRecord - def initialize(id:, name: vin: status: trips:) + def initialize(id:, name:, vin:, status:, trips:, total_revenue:) super(id) @name = name @vin = vin @status = status @trips = trips || [] + @total_revenue = total_revenue end - + + def add_trip(trip) + @trips << trip + end + + def average_rating + rating_total = 0 + if @trips.empty? + return 0 + else + @trips.each do |trip| + rating_total += trip.rating + end + return rating_total/@trips.length.to_f.round(2) + end + end + + def total_revenue + otal_revenue = 0 + if @trips.empty? + return 0 + else + @trips.each do |trip| + if trip.cost >= 1.65 + total_revenue += trip.cost * 0.8 - 1.65 + else + total_revenue + end + end + end + return total_revenue + end + def self.from_csv(record) return self.new( id: record[:id], name: record[:name], vin: record[:vin], - status: record[:status].to_sym + status: record[:status].to_sym, + trips: record[:trips], + total_revenue: record[:total_revenue] ) - end - + end end end -# we will need to update the Trip class to include a reference to the trip's driver. -# Add the following attributes to the Trip class: - -# Attribute | Description -# driver_id | The ID of the driver for this trip -# driver | The Driver instance for the trip - -# When a Trip is constructed, either driver_id or driver must be provided. # Note: You have changed the method signature of the constructor for Trip. Some of your tests may now be failing. Go fix them! diff --git a/lib/passenger.rb b/lib/passenger.rb index 7ec05d556..9f5d364e3 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -4,7 +4,7 @@ module RideShare class Passenger < CsvRecord attr_reader :name, :phone_number, :trips - def initialize(id:, name:, phone_number:, trips: nil) + def initialize(id:, name:, phone_number:, trips: nil ) super(id) @name = name diff --git a/lib/trip.rb b/lib/trip.rb index ede901d69..c8174fa20 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,11 +1,11 @@ -require 'csv' -require 'time' +require "csv" +require "time" -require_relative 'csv_record' +require_relative "csv_record" module RideShare class Trip < CsvRecord - attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating + attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating, :driver, :driver_id def initialize( id:, @@ -14,7 +14,9 @@ def initialize( start_time:, end_time:, cost: nil, - rating: + rating:, + driver: nil, + driver_id: nil ) super(id) @@ -29,6 +31,16 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end + # Wave 2: When a Trip is constructed, either driver_id or driver must be provided. + 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 + # Raises an ArgumentError if the end time is before the start time if end_time > start_time @start_time = start_time @@ -65,6 +77,12 @@ def connect(passenger) passenger.add_trip(self) end + def connect2(driver) + @driver = driver + driver.add_trip(self) + end + + private # Wave 1: Turn start_time and end_time into Time instances before passing them to Trip#initialize @@ -72,6 +90,7 @@ def connect(passenger) 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]), diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 802ef7bed..0d0e43cde 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -4,14 +4,17 @@ require_relative 'passenger' require_relative 'trip' +require_relative 'driver' module RideShare class TripDispatcher - attr_reader :drivers, :passengers, :trips + attr_reader :drivers, :passengers, :trips, :drivers def initialize(directory: './support') @passengers = Passenger.load_all(directory: directory) @trips = Trip.load_all(directory: directory) + # Wave 2: load Drivers + @drivers = Driver.load_all(directory: directory) connect_trips end @@ -20,6 +23,12 @@ def find_passenger(id) return @passengers.find { |passenger| passenger.id == id } end + # find driver + 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)} \ @@ -33,10 +42,18 @@ def inspect def connect_trips @trips.each do |trip| passenger = find_passenger(trip.passenger_id) + driver = find_driver(trip.driver_id) trip.connect(passenger) + trip.connect2(driver) end return trips end end end + +# Loading Drivers +# Update the TripDispatcher class as follows: + +# Update the Trip#connect method to connect the driver as well as the passenger (you'll want to create add trip on driver first - see below) +# Add a find_driver method that looks up a driver by ID \ No newline at end of file diff --git a/test/driver_test.rb b/test/driver_test.rb index 4e6076ec2..e164d12b2 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -131,6 +131,40 @@ end describe "total_revenue" do - # You add tests for the total_revenue method + it "returns the correct total revenue" do + + 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), + rating: 5, + total_revenue: 5 + ) + @driver.add_trip(trip) + + 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, + total_revenue: 3 + ) + @driver.add_trip(trip2) + end + expect(@driver.total_revenue).must_be_equal_to 8 + end end + + +#do not delete, this is for the class end diff --git a/test/passenger_test.rb b/test/passenger_test.rb index a6d12c71b..2e52b9a12 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -73,16 +73,16 @@ describe "net_expenditures" do it "returns correct total amount spent" do test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1) + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, driver: 'Da Vinci', driver_id: 28) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, driver: 'Munch', driver_id: 25) passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) expect(passenger.net_expenditures).must_equal 15 end it "shows when $0 spent" do test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 0, rating: 2) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 0, rating: 1) + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 0, rating: 2, driver: 'Da Vinci', driver_id: 28) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 0, rating: 1, driver: 'Munch', driver_id: 25) passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) expect(passenger.net_expenditures).must_equal 0 end @@ -92,8 +92,8 @@ describe "total_time_spent" do it "returns correct total time spent" do test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1) + test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, driver: 'Da Vinci', driver_id: 28) + test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, driver: 'Munch', driver_id: 25) passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) expected_time = 0 test_trips.each do |trip| From 95df3cfec53b273f389d46827ea82cab52abcfbd Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Wed, 26 Feb 2020 23:19:45 -0800 Subject: [PATCH 10/21] Worked on passing tests for Wave 1 with Driver class added --- lib/driver.rb | 12 ++++---- test/driver_test.rb | 65 ++++++++++++++++++++-------------------- test/passenger_test.rb | 68 ++++++++++++++++++++++-------------------- test/trip_test.rb | 3 +- 4 files changed, 77 insertions(+), 71 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index e292b3236..cca536bb2 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -4,13 +4,13 @@ # Use the provided tests to ensure that a Driver instance can be created successfully and that an ArgumentError is raised for an invalid status. module RideShare class Driver < CsvRecord - def initialize(id:, name:, vin:, status:, trips:, total_revenue:) + attr_reader :name, :vin, :status, :trips + def initialize(id:, name:, vin:, status: nil, trips: nil) super(id) @name = name @vin = vin @status = status @trips = trips || [] - @total_revenue = total_revenue end def add_trip(trip) @@ -30,7 +30,7 @@ def average_rating end def total_revenue - otal_revenue = 0 + total_revenue = 0 if @trips.empty? return 0 else @@ -50,9 +50,9 @@ def self.from_csv(record) id: record[:id], name: record[:name], vin: record[:vin], - status: record[:status].to_sym, - trips: record[:trips], - total_revenue: record[:total_revenue] + status: record[:status].to_sym + # trips: record[:trips], + # total_revenue: record[:total_revenue] ) end end diff --git a/test/driver_test.rb b/test/driver_test.rb index e164d12b2..81146ed03 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -131,39 +131,40 @@ end describe "total_revenue" do - it "returns the correct total revenue" do - - 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), - rating: 5, - total_revenue: 5 - ) - @driver.add_trip(trip) - - 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, - total_revenue: 3 - ) - @driver.add_trip(trip2) - end - expect(@driver.total_revenue).must_be_equal_to 8 + 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), + rating: 5, + total_revenue: 5 + ) + @driver.add_trip(trip) + + 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, + total_revenue: 3 + ) + @driver.add_trip(trip2) + end + + it "returns the correct total revenue" do + expect(@driver.total_revenue).must_equal 8 + end + end - end #do not delete, this is for the class diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 2e52b9a12..c235a33f4 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -48,6 +48,7 @@ trip = RideShare::Trip.new( id: 8, passenger: @passenger, + driver_id: 9, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), rating: 5 @@ -70,37 +71,40 @@ end # Wave 1: tests for the net_expenditures method - describe "net_expenditures" do - it "returns correct total amount spent" do - test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, driver: 'Da Vinci', driver_id: 28) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, driver: 'Munch', driver_id: 25) - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) - expect(passenger.net_expenditures).must_equal 15 - end - - it "shows when $0 spent" do - test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 0, rating: 2, driver: 'Da Vinci', driver_id: 28) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 0, rating: 1, driver: 'Munch', driver_id: 25) - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) - expect(passenger.net_expenditures).must_equal 0 - end - end - - # Wave 1: tests for the total_time_spent method - describe "total_time_spent" do - it "returns correct total time spent" do - test_trips = [] - test_trips << RideShare::Trip.new(id: 395, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, driver: 'Da Vinci', driver_id: 28) - test_trips << RideShare::Trip.new(id: 441, passenger: nil, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, driver: 'Munch', driver_id: 25) - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) - expected_time = 0 - test_trips.each do |trip| - expected_time += trip.calculate_duration - end - expect(passenger.total_time_spent).must_equal expected_time - end - end + # describe "net_expenditures" do + # it "returns correct total amount spent" do + # test_trips = [] + # driver = RideShare::Driver.new(id: 1, name: 'Da Vinci', vin: 'RFWNJWGU3Y8SD2VP0') + # test_trips << RideShare::Trip.new(id: 395, driver: driver, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, status: :AVAILABLE, trips: test_trips, total_revenue: 5) + # test_trips << RideShare::Trip.new(id: 441, driver: driver, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, status: :AVAILABLE, trips: test_trips, total_revenue: 3) + # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) + # expect(passenger.net_expenditures).must_equal 15 + # end + + # it "shows when $0 spent" do + # test_trips = [] + # driver = RideShare::Driver.new(id: 1, name: 'Da Vinci', vin: 'RFWNJWGU3Y8SD2VP0') + # test_trips << RideShare::Trip.new(id: 395, driver: driver, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 0, rating: 2, status: :AVAILABLE, trips: test_trips, total_revenue: 0) + # test_trips << RideShare::Trip.new(id: 441, driver: driver, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 0, rating: 1, status: :AVAILABLE, trips: test_trips, total_revenue: 0) + # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) + # expect(passenger.net_expenditures).must_equal 0 + # end + # end + + # # Wave 1: tests for the total_time_spent method + # describe "total_time_spent" do + # it "returns correct total time spent" do + # test_trips = [] + # driver = RideShare::Driver.new(id: 1, name: 'Da Vinci', vin: 'RFWNJWGU3Y8SD2VP0') + # test_trips << RideShare::Trip.new(id: 395, driver: driver, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, status: :AVAILABLE, trips: test_trips, total_revenue: 5) + # test_trips << RideShare::Trip.new(id: 441, driver: driver, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, status: :AVAILABLE, trips: test_trips, total_revenue: 5) + # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) + # expected_time = 0 + # test_trips.each do |trip| + # expected_time += trip.calculate_duration + # end + # expect(passenger.total_time_spent).must_equal expected_time + # end + # end end diff --git a/test/trip_test.rb b/test/trip_test.rb index 26556622c..38a02d6c6 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -16,7 +16,8 @@ start_time: start_time, end_time: end_time, cost: 23.45, - rating: 3 + rating: 3, + driver: RideShare::Driver.new(id: 20, name: "Renoir", vin: "SAR73WZ23J2SEJGJS", status: :UNAVAILABLE) } @trip = RideShare::Trip.new(@trip_data) end From 6f9f63f5addace05042335dab547660026d50041 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Thu, 27 Feb 2020 06:57:37 -0800 Subject: [PATCH 11/21] Re-ordered code in Driver's initialize method to be in more logical and legible order --- lib/driver.rb | 4 ++++ lib/trip.rb | 34 +++++++++++++++++----------------- test/trip_test.rb | 16 ++++++++++++++-- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index cca536bb2..170e862b0 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -8,7 +8,9 @@ class Driver < CsvRecord def initialize(id:, name:, vin:, status: nil, trips: nil) super(id) @name = name + # Add ArgumentError @vin = vin + # Add ArgumentError @status = status @trips = trips || [] end @@ -44,6 +46,8 @@ def total_revenue end return total_revenue end + + private def self.from_csv(record) return self.new( diff --git a/lib/trip.rb b/lib/trip.rb index c8174fa20..cda7e181d 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -31,16 +31,6 @@ def initialize( raise ArgumentError, 'Passenger or passenger_id is required' end - # Wave 2: When a Trip is constructed, either driver_id or driver must be provided. - 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 - # Raises an ArgumentError if the end time is before the start time if end_time > start_time @start_time = start_time @@ -48,7 +38,6 @@ def initialize( else raise ArgumentError, 'Those are invalid times' end - @cost = cost @rating = rating @@ -56,13 +45,18 @@ def initialize( if @rating > 5 || @rating < 1 raise ArgumentError.new("Invalid rating #{@rating}") end - end - # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds - def calculate_duration - duration = @end_time - @start_time - return duration - end + # Wave 2: When a Trip is constructed, either driver_id or driver must be provided. + 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 + + end # end initialize def inspect # Prevent infinite loop when puts-ing a Trip @@ -82,6 +76,12 @@ def connect2(driver) driver.add_trip(self) end + # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds + def calculate_duration + duration = @end_time - @start_time + return duration + end + private diff --git a/test/trip_test.rb b/test/trip_test.rb index 38a02d6c6..4f4ec24d0 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -58,7 +58,13 @@ start_time: start_time, end_time: end_time, cost: 23.45, - rating: 3 + rating: 3, + driver: RideShare::Driver.new( + id: 1, + name: 'Da Vinci', + vin: 'RFWNJWGU3Y8SD2VP0', + status: :AVAILABLE + ) } expect{RideShare::Trip.new(@trip_data)}.must_raise ArgumentError end @@ -80,7 +86,13 @@ start_time: start_time, end_time: end_time, cost: 23.45, - rating: 3 + rating: 3, + driver: RideShare::Driver.new( + id: 1, + name: 'Da Vinci', + vin: 'RFWNJWGU3Y8SD2VP0', + status: :AVAILABLE + ) } @trip = RideShare::Trip.new(@trip_data) From 3e77aa18717af482d0b68de6fc07fdef22ba8ed8 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Thu, 27 Feb 2020 07:13:14 -0800 Subject: [PATCH 12/21] Started re-factoring Wave 1 tests --- test/passenger_test.rb | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index c235a33f4..9b806a27b 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -71,15 +71,23 @@ end # Wave 1: tests for the net_expenditures method - # describe "net_expenditures" do - # it "returns correct total amount spent" do - # test_trips = [] - # driver = RideShare::Driver.new(id: 1, name: 'Da Vinci', vin: 'RFWNJWGU3Y8SD2VP0') - # test_trips << RideShare::Trip.new(id: 395, driver: driver, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, status: :AVAILABLE, trips: test_trips, total_revenue: 5) - # test_trips << RideShare::Trip.new(id: 441, driver: driver, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, status: :AVAILABLE, trips: test_trips, total_revenue: 3) - # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) - # expect(passenger.net_expenditures).must_equal 15 - # end + describe "net_expenditures" do + it "returns correct total amount spent" do + all_trips = RideShare::Trip.load_all(directory: './support') + passenger_trips = [] + passenger_cost = 0 + + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: passenger_trips) + + all_trips.each do |trip| + if trip.passenger_id == 1 + passenger_cost += trip.cost + passenger_trips << trip + end + end + + expect(passenger.net_expenditures).must_equal 15 + end # it "shows when $0 spent" do # test_trips = [] @@ -89,7 +97,7 @@ # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) # expect(passenger.net_expenditures).must_equal 0 # end - # end + end # # Wave 1: tests for the total_time_spent method # describe "total_time_spent" do From 0c497daf7d69045c395e7bc67d0b04a1a0287034 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Thu, 27 Feb 2020 14:04:51 -0800 Subject: [PATCH 13/21] added tests to passenger_test.rb --- test/passenger_test.rb | 61 +++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 9b806a27b..782a81879 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -74,45 +74,38 @@ describe "net_expenditures" do it "returns correct total amount spent" do all_trips = RideShare::Trip.load_all(directory: './support') - passenger_trips = [] - passenger_cost = 0 - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: passenger_trips) - - all_trips.each do |trip| - if trip.passenger_id == 1 - passenger_cost += trip.cost - passenger_trips << trip - end - end + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381') + passenger.add_trip(all_trips[0]) + passenger.add_trip(all_trips[1]) - expect(passenger.net_expenditures).must_equal 15 + expect(passenger.net_expenditures).must_equal 28 end + end - # it "shows when $0 spent" do - # test_trips = [] - # driver = RideShare::Driver.new(id: 1, name: 'Da Vinci', vin: 'RFWNJWGU3Y8SD2VP0') - # test_trips << RideShare::Trip.new(id: 395, driver: driver, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 0, rating: 2, status: :AVAILABLE, trips: test_trips, total_revenue: 0) - # test_trips << RideShare::Trip.new(id: 441, driver: driver, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 0, rating: 1, status: :AVAILABLE, trips: test_trips, total_revenue: 0) - # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) - # expect(passenger.net_expenditures).must_equal 0 - # end + # Wave 1: tests for the total_time_spent method + describe "total_time_spent" do + it "returns correct total time spent" do + all_trips = RideShare::Trip.load_all(directory: './support') + + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381') + passenger.add_trip(all_trips[0]) + passenger.add_trip(all_trips[1]) + + expect(passenger.total_time_spent).must_equal 5523.0 + end end - # # Wave 1: tests for the total_time_spent method - # describe "total_time_spent" do - # it "returns correct total time spent" do - # test_trips = [] - # driver = RideShare::Driver.new(id: 1, name: 'Da Vinci', vin: 'RFWNJWGU3Y8SD2VP0') - # test_trips << RideShare::Trip.new(id: 395, driver: driver, passenger_id: 1, start_time: Time.parse('2018-10-31 01:02:22 -0700'), end_time: Time.parse('2018-10-31 01:48:15 -0700'), cost: 6, rating: 2, status: :AVAILABLE, trips: test_trips, total_revenue: 5) - # test_trips << RideShare::Trip.new(id: 441, driver: driver, passenger_id: 1, start_time: Time.parse('2018-12-27 01:57:48 -0800'), end_time: Time.parse('2018-12-27 02:42:05 -0800'), cost: 9, rating: 1, status: :AVAILABLE, trips: test_trips, total_revenue: 5) - # passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381', trips: test_trips) - # expected_time = 0 - # test_trips.each do |trip| - # expected_time += trip.calculate_duration - # end - # expect(passenger.total_time_spent).must_equal expected_time - # end - # end + describe "add_trip" do + it "returns adding a trip" do + all_trips = RideShare::Trip.load_all(directory: './support') + + passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381') + passenger.add_trip(all_trips[0]) + passenger.add_trip(all_trips[1]) + + expect(passenger.trips.length).must_equal 2 + end + end end From 4824c6bb5e7ac7facdde5773027e6b0838cad1cf Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Thu, 27 Feb 2020 14:50:10 -0800 Subject: [PATCH 14/21] Got Wave 2 tests passing --- lib/driver.rb | 11 +++++--- lib/passenger.rb | 2 +- lib/trip.rb | 1 - lib/trip_dispatcher.rb | 8 ++++++ test/driver_test.rb | 52 ++++++++++++++++++++------------------ test/trip_dispatch_test.rb | 2 +- 6 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 170e862b0..9383b9898 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -5,16 +5,19 @@ module RideShare class Driver < CsvRecord attr_reader :name, :vin, :status, :trips - def initialize(id:, name:, vin:, status: nil, trips: nil) + def initialize(id:, name:, vin: nil, status: :AVAILABLE, trips: nil) super(id) @name = name - # Add ArgumentError + + if vin.length != 17 + raise ArgumentError, "vin must be 17 characters long" + end @vin = vin - # Add ArgumentError + @status = status @trips = trips || [] end - + def add_trip(trip) @trips << trip end diff --git a/lib/passenger.rb b/lib/passenger.rb index 9f5d364e3..dfa706b39 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -4,7 +4,7 @@ module RideShare class Passenger < CsvRecord attr_reader :name, :phone_number, :trips - def initialize(id:, name:, phone_number:, trips: nil ) + def initialize(id:, name:, phone_number:, trips: [] ) super(id) @name = name diff --git a/lib/trip.rb b/lib/trip.rb index cda7e181d..31571c644 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -82,7 +82,6 @@ def calculate_duration return duration end - private # Wave 1: Turn start_time and end_time into Time instances before passing them to Trip#initialize diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 0d0e43cde..12e5b94de 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -37,6 +37,14 @@ def inspect #{passengers.count} passengers>" end + + + + + + + + private def connect_trips diff --git a/test/driver_test.rb b/test/driver_test.rb index 81146ed03..4ce43bbe4 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( @@ -90,7 +90,7 @@ driver: @driver, passenger_id: 3, start_time: Time.new(2016, 8, 8), - end_time: Time.new(2016, 8, 8), + end_time: Time.new(2016, 9, 9), rating: 5 ) @driver.add_trip(trip) @@ -132,39 +132,43 @@ describe "total_revenue" do before do + all_trips = RideShare::Trip.load_all(directory: './support') + @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), - rating: 5, - total_revenue: 5 - ) - @driver.add_trip(trip) - - 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, - total_revenue: 3 - ) - @driver.add_trip(trip2) - end + # 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), + # rating: 5, + # total_revenue: 5 + # ) + # @driver.add_trip(trip) + + # 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, + # total_revenue: 3 + # ) + # @driver.add_trip(trip2) + driver.add_trip(all_trips[0]) + driver.add_trip(all_trips[1]) it "returns the correct total revenue" do expect(@driver.total_revenue).must_equal 8 end end + end #do not delete, this is for the class 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 21221c4741caa15c2b33280e350f072f5bf03488 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Thu, 27 Feb 2020 15:30:54 -0800 Subject: [PATCH 15/21] Added request_trip method for Wave 3 --- lib/trip_dispatcher.rb | 33 +++++++++++++++++++++------------ test/trip_dispatch_test.rb | 2 +- test/trip_test.rb | 1 - 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 12e5b94de..8155b4bd1 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -15,7 +15,6 @@ def initialize(directory: './support') @trips = Trip.load_all(directory: directory) # Wave 2: load Drivers @drivers = Driver.load_all(directory: directory) - connect_trips end def find_passenger(id) @@ -35,16 +34,32 @@ def inspect #{trips.count} trips, \ #{drivers.count} drivers, \ #{passengers.count} passengers>" + end end + # Wave 3 + # creates a trip and assigns an available driver + def request_trip(passenger_id) + if driver.each do |driver| + if driver.status == :AVAILABLE + return driver + end + end - + Trip.new( + id: driver.id, + passenger: @passenger_id, # function + passenger_id: @passenger_id, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil, + driver: driver, + driver_id: driver.id + ) + end - - - - private def connect_trips @@ -59,9 +74,3 @@ def connect_trips end end end - -# Loading Drivers -# Update the TripDispatcher class as follows: - -# Update the Trip#connect method to connect the driver as well as the passenger (you'll want to create add trip on driver first - see below) -# Add a find_driver method that looks up a driver by ID \ No newline at end of file diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index f152b77fa..60344f936 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 diff --git a/test/trip_test.rb b/test/trip_test.rb index 4f4ec24d0..b6c2351d4 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -31,7 +31,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 00bbef08a9131735832bab05d1ffb07f91d5b589 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Thu, 27 Feb 2020 16:55:58 -0800 Subject: [PATCH 16/21] fixed indentation and syntax in TripDispatcher --- lib/trip_dispatcher.rb | 77 ++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 8155b4bd1..7380ec491 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -1,16 +1,16 @@ -require 'csv' -require 'time' +require "csv" +require "time" # require 'pry' -require_relative 'passenger' -require_relative 'trip' -require_relative 'driver' +require_relative "passenger" +require_relative "trip" +require_relative "driver" module RideShare class TripDispatcher attr_reader :drivers, :passengers, :trips, :drivers - def initialize(directory: './support') + def initialize(directory: "./support") @passengers = Passenger.load_all(directory: directory) @trips = Trip.load_all(directory: directory) # Wave 2: load Drivers @@ -34,43 +34,48 @@ def inspect #{trips.count} trips, \ #{drivers.count} drivers, \ #{passengers.count} passengers>" - end end + end - # Wave 3 - # creates a trip and assigns an available driver - def request_trip(passenger_id) - if driver.each do |driver| - if driver.status == :AVAILABLE - return driver - end + # Wave 3 + # creates a trip and assigns an available driver + def request_trip(passenger_id) + @drivers.each do |driver| + if driver.status == :AVAILABLE + driver = dispatch_driver # make this loop end faster end - - Trip.new( - id: driver.id, - passenger: @passenger_id, # function - passenger_id: @passenger_id, - start_time: Time.now, - end_time: nil, - cost: nil, - rating: nil, - driver: driver, - driver_id: driver.id - ) + # return dispatch_driver end - - private + dispatch_driver.status = :UNAVAILABLE - def connect_trips - @trips.each do |trip| - passenger = find_passenger(trip.passenger_id) - driver = find_driver(trip.driver_id) - trip.connect(passenger) - trip.connect2(driver) - end + trip = Trip.new( + id: dispatch_driver.id, + passenger: find_passenger(passenger_id), + passenger_id: passenger_id, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil, + driver: dispatch_driver, + driver_id: dispatch_driver.id, + ) + + dispatch_driver.add_trip(trip) + trip.passenger.add_trip(trip) + @trips << trip + return trip + end + + private - return trips + def connect_trips + @trips.each do |trip| + passenger = find_passenger(trip.passenger_id) + driver = find_driver(trip.driver_id) + trip.connect(passenger) + trip.connect2(driver) end + return trips end end From ffdc29100bbd6047ff2cb2a09b9a54e34c271931 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Fri, 28 Feb 2020 11:05:31 -0800 Subject: [PATCH 17/21] Refactored request_trip method --- lib/driver.rb | 117 +++++++++++++++++++------------------ lib/trip.rb | 65 +++++++++++---------- lib/trip_dispatcher.rb | 98 +++++++++++++++++++------------ test/trip_dispatch_test.rb | 34 +++++++++-- 4 files changed, 182 insertions(+), 132 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 9383b9898..b0abb1823 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -3,68 +3,69 @@ # Since Driver inherits from CsvRecord, you'll need to implement the from_csv template method. Once you do, Driver.load_all should work (test this in pry). # Use the provided tests to ensure that a Driver instance can be created successfully and that an ArgumentError is raised for an invalid status. module RideShare - class Driver < CsvRecord - attr_reader :name, :vin, :status, :trips - def initialize(id:, name:, vin: nil, status: :AVAILABLE, trips: nil) - super(id) - @name = name - - if vin.length != 17 - raise ArgumentError, "vin must be 17 characters long" - end - @vin = vin - - @status = status - @trips = trips || [] - end + class Driver < CsvRecord + attr_reader :name, :vin, :trips + attr_accessor :status - def add_trip(trip) - @trips << trip - end - - def average_rating - rating_total = 0 - if @trips.empty? - return 0 - else - @trips.each do |trip| - rating_total += trip.rating - end - return rating_total/@trips.length.to_f.round(2) - end - end - - def total_revenue - total_revenue = 0 - if @trips.empty? - return 0 - else - @trips.each do |trip| - if trip.cost >= 1.65 - total_revenue += trip.cost * 0.8 - 1.65 - else - total_revenue - end - end - end - return total_revenue + def initialize(id:, name:, vin: nil, status: :AVAILABLE, trips: nil) + super(id) + @name = name + + if vin.length != 17 + raise ArgumentError, "vin must be 17 characters long" + end + @vin = vin + + @status = status + @trips = trips || [] + end + + def add_trip(trip) + @trips << trip + end + + def average_rating + rating_total = 0 + if @trips.empty? + return 0 + else + @trips.each do |trip| + rating_total += trip.rating end + return rating_total / @trips.length.to_f.round(2) + end + end - private - - def self.from_csv(record) - return self.new( - id: record[:id], - name: record[:name], - vin: record[:vin], - status: record[:status].to_sym - # trips: record[:trips], - # total_revenue: record[:total_revenue] - ) + def total_revenue + total_revenue = 0 + if @trips.empty? + return 0 + else + @trips.each do |trip| + if trip.cost >= 1.65 + total_revenue += trip.cost * 0.8 - 1.65 + else + total_revenue + end end + end + return total_revenue end -end + private + + def self.from_csv(record) + return self.new( + id: record[:id], + name: record[:name], + vin: record[:vin], + status: record[:status].to_sym, + # trips: record[:trips], + # total_revenue: record[:total_revenue] + ) + end + end +end # Note: You have changed the method signature of the constructor for Trip. Some of your tests may now be failing. Go fix them! @@ -80,7 +81,7 @@ def self.from_csv(record) # add_trip | Add a trip to the driver's list of trips | Try adding a trip # average_rating | What is this driver's average rating? | What if there are no trips? Does it handle floating point division correctly? For example the average of 2 and 3 should be 2.5, not 2. -# total_revenue | This method calculates that driver's total revenue across all their trips. Each driver gets 80% of the trip cost after a fee of $1.65 per trip is subtracted. +# total_revenue | This method calculates that driver's total revenue across all their trips. Each driver gets 80% of the trip cost after a fee of $1.65 per trip is subtracted. # | What if there are no trips? What if the cost of a trip was less that $1.65? -# All the new methods above should have tests \ No newline at end of file +# All the new methods above should have tests diff --git a/lib/trip.rb b/lib/trip.rb index 31571c644..9fb99fb01 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -8,42 +8,44 @@ class Trip < CsvRecord attr_reader :id, :passenger, :passenger_id, :start_time, :end_time, :cost, :rating, :driver, :driver_id def initialize( - id:, - passenger: nil, - passenger_id: nil, - start_time:, - end_time:, - cost: nil, - rating:, - driver: nil, - driver_id: nil - ) + id:, + passenger: nil, + passenger_id: nil, + start_time:, + end_time:, + cost: nil, + rating:, + driver: nil, + driver_id: nil + ) super(id) if passenger @passenger = passenger @passenger_id = passenger.id - elsif passenger_id @passenger_id = passenger_id - else - raise ArgumentError, 'Passenger or passenger_id is required' + raise ArgumentError, "Passenger or passenger_id is required" end # Raises an ArgumentError if the end time is before the start time - if end_time > start_time - @start_time = start_time - @end_time = end_time - else - raise ArgumentError, 'Those are invalid times' + if end_time != nil + if end_time > start_time + @start_time = start_time + @end_time = end_time + else + raise ArgumentError, "Those are invalid times" + end end @cost = cost @rating = rating - if @rating > 5 || @rating < 1 - raise ArgumentError.new("Invalid rating #{@rating}") + if rating != nil + if @rating > 5 || @rating < 1 + raise ArgumentError.new("Invalid rating #{@rating}") + end end # Wave 2: When a Trip is constructed, either driver_id or driver must be provided. @@ -53,10 +55,9 @@ def initialize( elsif driver_id @driver_id = driver_id else - raise ArgumentError, 'Driver or driver_id is required' + raise ArgumentError, "Driver or driver_id is required" end - - end # end initialize + end # end initialize def inspect # Prevent infinite loop when puts-ing a Trip @@ -71,7 +72,7 @@ def connect(passenger) passenger.add_trip(self) end - def connect2(driver) + def connect_driver(driver) @driver = driver driver.add_trip(self) end @@ -88,14 +89,14 @@ def calculate_duration # Trip.from_csv overrides CsvRecord.from_csv 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]), - cost: record[:cost], - rating: record[:rating] - ) + 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]), + cost: record[:cost], + rating: record[:rating], + ) end end end diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 7380ec491..abddec2ef 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -15,6 +15,7 @@ def initialize(directory: "./support") @trips = Trip.load_all(directory: directory) # Wave 2: load Drivers @drivers = Driver.load_all(directory: directory) + connect_trips end def find_passenger(id) @@ -35,47 +36,72 @@ def inspect #{drivers.count} drivers, \ #{passengers.count} passengers>" end - end - # Wave 3 - # creates a trip and assigns an available driver - def request_trip(passenger_id) - @drivers.each do |driver| - if driver.status == :AVAILABLE - driver = dispatch_driver # make this loop end faster + # Wave 3 + # creates a trip and assigns an available driver + def request_trip(passenger_id) + available_drivers = [] + + @drivers.each do |driver| + if driver.status == :AVAILABLE + available_drivers << driver + end end - # return dispatch_driver - end - dispatch_driver.status = :UNAVAILABLE - - trip = Trip.new( - id: dispatch_driver.id, - passenger: find_passenger(passenger_id), - passenger_id: passenger_id, - start_time: Time.now, - end_time: nil, - cost: nil, - rating: nil, - driver: dispatch_driver, - driver_id: dispatch_driver.id, - ) - - dispatch_driver.add_trip(trip) - trip.passenger.add_trip(trip) - @trips << trip - return trip - end + if available_drivers.length == 0 + raise ArgumentError, "No available drivers" + end + + dispatch_driver = available_drivers[0] + + dispatch_driver.status = :UNAVAILABLE + + trip = Trip.new( + id: @trips.last.id + 1, + passenger: find_passenger(passenger_id), + passenger_id: passenger_id, + start_time: Time.now, + end_time: nil, + cost: nil, + rating: nil, + driver: dispatch_driver, + driver_id: dispatch_driver.id, + ) + + dispatch_driver.add_trip(trip) + trip.passenger.add_trip(trip) - private + @trips << trip + trip.connect(trip.passenger) + trip.connect_driver(dispatch_driver) - def connect_trips - @trips.each do |trip| - passenger = find_passenger(trip.passenger_id) - driver = find_driver(trip.driver_id) - trip.connect(passenger) - trip.connect2(driver) + return trip + end + + private + + def connect_trips + @trips.each do |trip| + passenger = find_passenger(trip.passenger_id) + driver = find_driver(trip.driver_id) + trip.connect(passenger) + trip.connect_driver(driver) + end + return trips end - return trips end end + +# Wave 3 TripDispatcher tests: + +# Was the trip created properly? +# Were the trip lists for the driver and passenger updated? +# Was the driver who was selected AVAILABLE? +# What happens if you try to request a trip when there are no AVAILABLE drivers? + +# Interaction with Waves 1 & 2 +# One thing you may notice is that this change breaks your code from previous waves, possibly in subtle ways. We've added a new kind of trip, an in-progress trip, that is missing some of the values you need to compute those numbers. + +# Your code from waves 1 & 2 should ignore any in-progress trips. That is to say, any trip where the end time is nil should not be included in your totals. + +# You should also add explicit tests for this new situation. For example, what happens if you attempt to calculate the total money spent for a Passenger with an in-progress trip, or the average rating of a Driver with an in-progress trip? diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 60344f936..40e3f4687 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -1,12 +1,12 @@ -require_relative 'test_helper' +require_relative "test_helper" -TEST_DATA_DIRECTORY = 'test/test_data' +TEST_DATA_DIRECTORY = "test/test_data" describe "TripDispatcher class" do def build_test_dispatcher return RideShare::TripDispatcher.new( - directory: TEST_DATA_DIRECTORY - ) + directory: TEST_DATA_DIRECTORY, + ) end describe "Initializer" do @@ -28,7 +28,7 @@ def build_test_dispatcher it "loads the development data by default" do # Count lines in the file, subtract 1 for headers - trip_count = %x{wc -l 'support/trips.csv'}.split(' ').first.to_i - 1 + trip_count = %x{wc -l 'support/trips.csv'}.split(" ").first.to_i - 1 dispatcher = RideShare::TripDispatcher.new @@ -43,7 +43,7 @@ def build_test_dispatcher end it "throws an argument error for a bad ID" do - expect{ @dispatcher.find_passenger(0) }.must_raise ArgumentError + expect { @dispatcher.find_passenger(0) }.must_raise ArgumentError end it "finds a passenger instance" do @@ -78,6 +78,14 @@ def build_test_dispatcher end end + # Wave 3: tests for request_trip method + describe "request_trip" do + it "returns instance of trip" do + td = RideShare::TripDispatcher.new + expect(td.request_trip(1)).must_be_kind_of RideShare::Trip + end + end + # TODO: un-skip for Wave 2 describe "drivers" do describe "find_driver method" do @@ -123,3 +131,17 @@ def build_test_dispatcher end end end + +# Wave 3 TripDispatcher tests: + +# Was the trip created properly? +# Were the trip lists for the driver and passenger updated? +# Was the driver who was selected AVAILABLE? +# What happens if you try to request a trip when there are no AVAILABLE drivers? + +# Interaction with Waves 1 & 2 +# One thing you may notice is that this change breaks your code from previous waves, possibly in subtle ways. We've added a new kind of trip, an in-progress trip, that is missing some of the values you need to compute those numbers. + +# Your code from waves 1 & 2 should ignore any in-progress trips. That is to say, any trip where the end time is nil should not be included in your totals. + +# You should also add explicit tests for this new situation. For example, what happens if you attempt to calculate the total money spent for a Passenger with an in-progress trip, or the average rating of a Driver with an in-progress trip? From 0846868b0219e60202e95ea13d1ce45ab50cf416 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Fri, 28 Feb 2020 12:12:03 -0800 Subject: [PATCH 18/21] added a test for request_trip --- test/trip_dispatch_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index 40e3f4687..a425365ae 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -84,6 +84,22 @@ def build_test_dispatcher td = RideShare::TripDispatcher.new expect(td.request_trip(1)).must_be_kind_of RideShare::Trip end + # Were the trip lists for the driver and passenger updated? + it "Adds trip to passenger's trips" do + dispatcher = build_test_dispatcher + + passenger = dispatcher.find_passenger(1) + + dispatcher.request_trip(1) + + expect(passenger.trips.length).must_equal 3 + + # trip = added_trip + # passenger.trips.last.id == trip.id + end + + it "Adds trip to driver's trips" do + end end # TODO: un-skip for Wave 2 From c5109dce3b978a1a185b0c9d422c99d870a4263b Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Fri, 28 Feb 2020 14:58:00 -0800 Subject: [PATCH 19/21] Refactored request_trip method --- lib/trip_dispatcher.rb | 25 +++++++++++++++++-------- test/trip_dispatch_test.rb | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index abddec2ef..0a5af855b 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -37,9 +37,7 @@ def inspect #{passengers.count} passengers>" end - # Wave 3 - # creates a trip and assigns an available driver - def request_trip(passenger_id) + def find_available_drivers available_drivers = [] @drivers.each do |driver| @@ -47,14 +45,24 @@ def request_trip(passenger_id) available_drivers << driver end end + return available_drivers + end - if available_drivers.length == 0 - raise ArgumentError, "No available drivers" - end + # Wave 3 + # creates a trip and assigns an available driver + def request_trip(passenger_id) + # available_drivers = [] - dispatch_driver = available_drivers[0] + # @drivers.each do |driver| + # if driver.status == :AVAILABLE + # available_drivers << driver + # end + # end - dispatch_driver.status = :UNAVAILABLE + # if available_drivers.length == 0 + # raise ArgumentError, "No available drivers" + # end + dispatch_driver = find_available_drivers[0] trip = Trip.new( id: @trips.last.id + 1, @@ -69,6 +77,7 @@ def request_trip(passenger_id) ) dispatch_driver.add_trip(trip) + dispatch_driver.status = :UNAVAILABLE trip.passenger.add_trip(trip) @trips << trip diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index a425365ae..df43e3ff1 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -99,6 +99,26 @@ def build_test_dispatcher end it "Adds trip to driver's trips" do + dispatcher = build_test_dispatcher + passenger = dispatcher.find_passenger(1) + dispatcher.request_trip(1) + + # get the passenger's last trip + passengers_last_trip = passenger.trips[-1] + last_trips_driver = passengers_last_trip.driver_id + driver = dispatcher.find_driver(last_trips_driver) + + expect(driver.trips.length).must_equal 5 + end + + it "Was the driver available" do + # count how many available driver + # request trip + # expect one less available driver + end + + it "Raises ArgumentError for unavailable driver" do + # Give dispatcher only unavailable drivers to raise ArgumentError - Set all driver to unavailable? Give one unavailable driver in drivers? end end From c9068b4ec286dfa6be5744942312adf9b4eceabb Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Fri, 28 Feb 2020 16:48:01 -0800 Subject: [PATCH 20/21] Passed tests for Wave 3 --- lib/trip_dispatcher.rb | 20 +++++++++++--------- test/trip_dispatch_test.rb | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 0a5af855b..58f9c783f 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -59,10 +59,12 @@ def request_trip(passenger_id) # end # end - # if available_drivers.length == 0 - # raise ArgumentError, "No available drivers" - # end - dispatch_driver = find_available_drivers[0] + + dispatch_driver = find_available_drivers + + if dispatch_driver.length == 0 + raise ArgumentError.new("No available drivers") + end trip = Trip.new( id: @trips.last.id + 1, @@ -72,17 +74,17 @@ def request_trip(passenger_id) end_time: nil, cost: nil, rating: nil, - driver: dispatch_driver, - driver_id: dispatch_driver.id, + driver: dispatch_driver[0], + driver_id: dispatch_driver[0].id, ) - dispatch_driver.add_trip(trip) - dispatch_driver.status = :UNAVAILABLE + dispatch_driver[0].add_trip(trip) + dispatch_driver[0].status = :UNAVAILABLE trip.passenger.add_trip(trip) @trips << trip trip.connect(trip.passenger) - trip.connect_driver(dispatch_driver) + trip.connect_driver(dispatch_driver[0]) return trip end diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index df43e3ff1..befe31bc6 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -112,14 +112,30 @@ def build_test_dispatcher end it "Was the driver available" do + dispatcher = build_test_dispatcher + drivers = dispatcher.find_available_drivers + passenger = dispatcher.find_passenger(1) + dispatcher.request_trip(1) + + passengers_last_trip = passenger.trips[-1] + last_trips_driver = passengers_last_trip.driver_id + driver = dispatcher.find_driver(last_trips_driver) + expect(drivers.include?(last_trips_driver)).must_equal false # count how many available driver # request trip # expect one less available driver + + expect(driver.status).must_equal :UNAVAILABLE end it "Raises ArgumentError for unavailable driver" do - # Give dispatcher only unavailable drivers to raise ArgumentError - Set all driver to unavailable? Give one unavailable driver in drivers? + dispatcher = build_test_dispatcher + dispatcher.drivers.each do |driver| + driver.status = :UNAVAILABLE + end + expect {dispatcher.request_trip(1) }.must_raise ArgumentError end + # Give dispatcher only unavailable drivers to raise ArgumentError - Set all driver to unavailable? Give one unavailable driver in drivers? end # TODO: un-skip for Wave 2 From 01844274e31b7b526cc78da66456488dd52a6146 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Sat, 29 Feb 2020 14:42:58 -0800 Subject: [PATCH 21/21] cleaned up extra spaces and comments --- lib/csv_record.rb | 26 +++++++++--------- lib/driver.rb | 25 ------------------ lib/passenger.rb | 13 +++++---- lib/trip.rb | 7 ++--- lib/trip_dispatcher.rb | 34 +----------------------- test/driver_test.rb | 54 +++++++++----------------------------- test/passenger_test.rb | 36 +++++++++---------------- test/trip_dispatch_test.rb | 34 +----------------------- test/trip_test.rb | 48 ++++++++++++++++----------------- 9 files changed, 68 insertions(+), 209 deletions(-) diff --git a/lib/csv_record.rb b/lib/csv_record.rb index f2c2d617a..8f86b6969 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -1,4 +1,4 @@ -require 'csv' +require "csv" module RideShare class CsvRecord @@ -8,31 +8,30 @@ def initialize(id) self.class.validate_id(id) @id = id end - + # Takes either full_path or directory and optional file_name # Default file name matches class name def self.load_all(full_path: nil, directory: nil, file_name: nil) full_path ||= build_path(directory, file_name) - return CSV.read( - full_path, - headers: true, - header_converters: :symbol, - converters: :numeric - ).map { |record| from_csv(record) } + full_path, + headers: true, + header_converters: :symbol, + converters: :numeric, + ).map { |record| from_csv(record) } end def self.validate_id(id) if id.nil? || id <= 0 - raise ArgumentError, 'ID cannot be blank or less than one.' + raise ArgumentError, "ID cannot be blank or less than one." end end private - - # Template method - doesn't know what to parse + + # Template method def self.from_csv(record) - raise NotImplementedError, 'Implement me in a child class!' + raise NotImplementedError, "Implement me in a child class!" end def self.build_path(directory, file_name) @@ -41,10 +40,9 @@ def self.build_path(directory, file_name) end unless file_name - class_name = self.to_s.split('::').last + class_name = self.to_s.split("::").last file_name = "#{class_name.downcase}s.csv" end - return "#{directory}/#{file_name}" end end diff --git a/lib/driver.rb b/lib/driver.rb index b0abb1823..948e6ecf5 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,7 +1,5 @@ require_relative "csv_record" -# Since Driver inherits from CsvRecord, you'll need to implement the from_csv template method. Once you do, Driver.load_all should work (test this in pry). -# Use the provided tests to ensure that a Driver instance can be created successfully and that an ArgumentError is raised for an invalid status. module RideShare class Driver < CsvRecord attr_reader :name, :vin, :trips @@ -10,12 +8,10 @@ class Driver < CsvRecord def initialize(id:, name:, vin: nil, status: :AVAILABLE, trips: nil) super(id) @name = name - if vin.length != 17 raise ArgumentError, "vin must be 17 characters long" end @vin = vin - @status = status @trips = trips || [] end @@ -60,28 +56,7 @@ def self.from_csv(record) name: record[:name], vin: record[:vin], status: record[:status].to_sym, - # trips: record[:trips], - # total_revenue: record[:total_revenue] ) end end end - -# Note: You have changed the method signature of the constructor for Trip. Some of your tests may now be failing. Go fix them! - -# Update the TripDispatcher class as follows: - -# In the constructor, call Driver.load_all and save the result in an instance variable -# Update the Trip#connect method to connect the driver as well as the passenger (you'll want to create add trip on driver first - see below) -# Add a find_driver method that looks up a driver by ID - -# After each Trip has a reference to its Driver and TripDispatcher can load a list of Drivers, add the following functionality to the Driver class: - -# Method | Description | Test Cases -# add_trip | Add a trip to the driver's list of trips | Try adding a trip -# average_rating | What is this driver's average rating? | What if there are no trips? Does it handle floating point division correctly? For example the average of 2 and 3 should be 2.5, not 2. - -# total_revenue | This method calculates that driver's total revenue across all their trips. Each driver gets 80% of the trip cost after a fee of $1.65 per trip is subtracted. -# | What if there are no trips? What if the cost of a trip was less that $1.65? - -# All the new methods above should have tests diff --git a/lib/passenger.rb b/lib/passenger.rb index dfa706b39..195879158 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -1,12 +1,11 @@ -require_relative 'csv_record' +require_relative "csv_record" module RideShare class Passenger < CsvRecord attr_reader :name, :phone_number, :trips - def initialize(id:, name:, phone_number:, trips: [] ) + def initialize(id:, name:, phone_number:, trips: []) super(id) - @name = name @phone_number = phone_number @trips = trips || [] @@ -38,10 +37,10 @@ def total_time_spent def self.from_csv(record) return new( - id: record[:id], - name: record[:name], - phone_number: record[:phone_num] - ) + id: record[:id], + name: record[:name], + phone_number: record[:phone_num], + ) end end end diff --git a/lib/trip.rb b/lib/trip.rb index 9fb99fb01..3175aa07e 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -57,11 +57,10 @@ def initialize( else raise ArgumentError, "Driver or driver_id is required" end - end # end initialize + end def inspect # Prevent infinite loop when puts-ing a Trip - # trip contains a passenger contains a trip contains a passenger... "#<#{self.class.name}:0x#{self.object_id.to_s(16)} " + "ID=#{id.inspect} " + "PassengerID=#{passenger&.id.inspect}>" @@ -77,7 +76,7 @@ def connect_driver(driver) driver.add_trip(self) end - # Wave 1: Add an instance method to the Trip class to calculate the duration of the trip in seconds + # Wave 1: Instance method to calculate the duration of the trip in seconds def calculate_duration duration = @end_time - @start_time return duration @@ -85,8 +84,6 @@ def calculate_duration private - # Wave 1: Turn start_time and end_time into Time instances before passing them to Trip#initialize - # Trip.from_csv overrides CsvRecord.from_csv def self.from_csv(record) return self.new( id: record[:id], diff --git a/lib/trip_dispatcher.rb b/lib/trip_dispatcher.rb index 58f9c783f..f67e3785d 100644 --- a/lib/trip_dispatcher.rb +++ b/lib/trip_dispatcher.rb @@ -1,6 +1,5 @@ require "csv" require "time" -# require 'pry' require_relative "passenger" require_relative "trip" @@ -13,7 +12,6 @@ class TripDispatcher def initialize(directory: "./support") @passengers = Passenger.load_all(directory: directory) @trips = Trip.load_all(directory: directory) - # Wave 2: load Drivers @drivers = Driver.load_all(directory: directory) connect_trips end @@ -39,7 +37,6 @@ def inspect def find_available_drivers available_drivers = [] - @drivers.each do |driver| if driver.status == :AVAILABLE available_drivers << driver @@ -48,24 +45,12 @@ def find_available_drivers return available_drivers end - # Wave 3 - # creates a trip and assigns an available driver + # Wave 3: creates a trip and assigns an available driver def request_trip(passenger_id) - # available_drivers = [] - - # @drivers.each do |driver| - # if driver.status == :AVAILABLE - # available_drivers << driver - # end - # end - - dispatch_driver = find_available_drivers - if dispatch_driver.length == 0 raise ArgumentError.new("No available drivers") end - trip = Trip.new( id: @trips.last.id + 1, passenger: find_passenger(passenger_id), @@ -77,15 +62,12 @@ def request_trip(passenger_id) driver: dispatch_driver[0], driver_id: dispatch_driver[0].id, ) - dispatch_driver[0].add_trip(trip) dispatch_driver[0].status = :UNAVAILABLE trip.passenger.add_trip(trip) - @trips << trip trip.connect(trip.passenger) trip.connect_driver(dispatch_driver[0]) - return trip end @@ -102,17 +84,3 @@ def connect_trips end end end - -# Wave 3 TripDispatcher tests: - -# Was the trip created properly? -# Were the trip lists for the driver and passenger updated? -# Was the driver who was selected AVAILABLE? -# What happens if you try to request a trip when there are no AVAILABLE drivers? - -# Interaction with Waves 1 & 2 -# One thing you may notice is that this change breaks your code from previous waves, possibly in subtle ways. We've added a new kind of trip, an in-progress trip, that is missing some of the values you need to compute those numbers. - -# Your code from waves 1 & 2 should ignore any in-progress trips. That is to say, any trip where the end time is nil should not be included in your totals. - -# You should also add explicit tests for this new situation. For example, what happens if you attempt to calculate the total money spent for a Passenger with an in-progress trip, or the average rating of a Driver with an in-progress trip? diff --git a/test/driver_test.rb b/test/driver_test.rb index 4ce43bbe4..183e3d2b2 100644 --- a/test/driver_test.rb +++ b/test/driver_test.rb @@ -1,4 +1,4 @@ -require_relative 'test_helper' +require_relative "test_helper" describe "Driver class" do describe "Driver instantiation" do @@ -7,7 +7,7 @@ id: 54, name: "Test Driver", vin: "12345678901234567", - status: :AVAILABLE + status: :AVAILABLE, ) end @@ -37,7 +37,6 @@ [:id, :name, :vin, :status, :trips].each do |prop| expect(@driver).must_respond_to prop end - expect(@driver.id).must_be_kind_of Integer expect(@driver.name).must_be_kind_of String expect(@driver.vin).must_be_kind_of String @@ -50,12 +49,12 @@ pass = RideShare::Passenger.new( id: 1, name: "Test Passenger", - phone_number: "412-432-7640" + phone_number: "412-432-7640", ) @driver = RideShare::Driver.new( id: 3, name: "Test Driver", - vin: "12345678912345678" + vin: "12345678912345678", ) @trip = RideShare::Trip.new( id: 8, @@ -63,16 +62,14 @@ passenger: pass, start_time: Time.new(2016, 8, 8), end_time: Time.new(2018, 8, 9), - rating: 5 + rating: 5, ) end it "adds the trip" do expect(@driver.trips).wont_include @trip previous = @driver.trips.length - @driver.add_trip(@trip) - expect(@driver.trips).must_include @trip expect(@driver.trips.length).must_equal previous + 1 end @@ -83,7 +80,7 @@ @driver = RideShare::Driver.new( id: 54, name: "Rogers Bartell IV", - vin: "1C9EVBRM0YBC564DZ" + vin: "1C9EVBRM0YBC564DZ", ) trip = RideShare::Trip.new( id: 8, @@ -91,7 +88,7 @@ passenger_id: 3, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 9, 9), - rating: 5 + rating: 5, ) @driver.add_trip(trip) end @@ -110,7 +107,7 @@ driver = RideShare::Driver.new( id: 54, name: "Rogers Bartell IV", - vin: "1C9EVBRM0YBC564DZ" + vin: "1C9EVBRM0YBC564DZ", ) expect(driver.average_rating).must_equal 0 end @@ -122,7 +119,7 @@ passenger_id: 3, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), - rating: 1 + rating: 1, ) @driver.add_trip(trip2) @@ -132,44 +129,17 @@ describe "total_revenue" do before do - all_trips = RideShare::Trip.load_all(directory: './support') - + all_trips = RideShare::Trip.load_all(directory: "./support") @driver = RideShare::Driver.new( id: 54, name: "Rogers Bartell IV", - vin: "1C9EVBRM0YBC564DZ" + 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), - # rating: 5, - # total_revenue: 5 - # ) - # @driver.add_trip(trip) - - # 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, - # total_revenue: 3 - # ) - # @driver.add_trip(trip2) driver.add_trip(all_trips[0]) driver.add_trip(all_trips[1]) - it "returns the correct total revenue" do - expect(@driver.total_revenue).must_equal 8 + expect(@driver.total_revenue).must_equal 8 end - end end - - -#do not delete, this is for the class end diff --git a/test/passenger_test.rb b/test/passenger_test.rb index 782a81879..48ba71327 100644 --- a/test/passenger_test.rb +++ b/test/passenger_test.rb @@ -1,8 +1,7 @@ -require_relative 'test_helper' -require 'time' +require_relative "test_helper" +require "time" describe "Passenger class" do - describe "Passenger instantiation" do before do @passenger = RideShare::Passenger.new(id: 1, name: "Smithy", phone_number: "353-533-5334") @@ -27,7 +26,6 @@ [:id, :name, :phone_number, :trips].each do |prop| expect(@passenger).must_respond_to prop end - expect(@passenger.id).must_be_kind_of Integer expect(@passenger.name).must_be_kind_of String expect(@passenger.phone_number).must_be_kind_of String @@ -35,25 +33,22 @@ end end - 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", phone_number: "1-602-620-2330 x3723", - trips: [] - ) + trips: [], + ) trip = RideShare::Trip.new( id: 8, passenger: @passenger, driver_id: 9, start_time: Time.new(2016, 8, 8), end_time: Time.new(2016, 8, 9), - rating: 5 - ) - + rating: 5, + ) @passenger.add_trip(trip) end @@ -73,12 +68,10 @@ # Wave 1: tests for the net_expenditures method describe "net_expenditures" do it "returns correct total amount spent" do - all_trips = RideShare::Trip.load_all(directory: './support') - - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381') + all_trips = RideShare::Trip.load_all(directory: "./support") + passenger = RideShare::Passenger.new(id: 1, name: "Paul Pollich", phone_number: "(358) 263-9381") passenger.add_trip(all_trips[0]) passenger.add_trip(all_trips[1]) - expect(passenger.net_expenditures).must_equal 28 end end @@ -86,26 +79,21 @@ # Wave 1: tests for the total_time_spent method describe "total_time_spent" do it "returns correct total time spent" do - all_trips = RideShare::Trip.load_all(directory: './support') - - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381') + all_trips = RideShare::Trip.load_all(directory: "./support") + passenger = RideShare::Passenger.new(id: 1, name: "Paul Pollich", phone_number: "(358) 263-9381") passenger.add_trip(all_trips[0]) passenger.add_trip(all_trips[1]) - expect(passenger.total_time_spent).must_equal 5523.0 end end describe "add_trip" do it "returns adding a trip" do - all_trips = RideShare::Trip.load_all(directory: './support') - - passenger = RideShare::Passenger.new(id: 1, name: 'Paul Pollich', phone_number: '(358) 263-9381') + all_trips = RideShare::Trip.load_all(directory: "./support") + passenger = RideShare::Passenger.new(id: 1, name: "Paul Pollich", phone_number: "(358) 263-9381") passenger.add_trip(all_trips[0]) passenger.add_trip(all_trips[1]) - expect(passenger.trips.length).must_equal 2 end end - end diff --git a/test/trip_dispatch_test.rb b/test/trip_dispatch_test.rb index befe31bc6..fc1c18a07 100644 --- a/test/trip_dispatch_test.rb +++ b/test/trip_dispatch_test.rb @@ -20,7 +20,6 @@ def build_test_dispatcher [:trips, :passengers].each do |prop| expect(dispatcher).must_respond_to prop end - expect(dispatcher.trips).must_be_kind_of Array expect(dispatcher.passengers).must_be_kind_of Array expect(dispatcher.drivers).must_be_kind_of Array @@ -29,9 +28,7 @@ def build_test_dispatcher it "loads the development data by default" do # Count lines in the file, subtract 1 for headers trip_count = %x{wc -l 'support/trips.csv'}.split(" ").first.to_i - 1 - dispatcher = RideShare::TripDispatcher.new - expect(dispatcher.trips.length).must_equal trip_count end end @@ -60,7 +57,6 @@ def build_test_dispatcher it "accurately loads passenger information into passengers array" do first_passenger = @dispatcher.passengers.first last_passenger = @dispatcher.passengers.last - expect(first_passenger.name).must_equal "Passenger 1" expect(first_passenger.id).must_equal 1 expect(last_passenger.name).must_equal "Passenger 8" @@ -87,15 +83,9 @@ def build_test_dispatcher # Were the trip lists for the driver and passenger updated? it "Adds trip to passenger's trips" do dispatcher = build_test_dispatcher - passenger = dispatcher.find_passenger(1) - dispatcher.request_trip(1) - expect(passenger.trips.length).must_equal 3 - - # trip = added_trip - # passenger.trips.last.id == trip.id end it "Adds trip to driver's trips" do @@ -107,7 +97,6 @@ def build_test_dispatcher passengers_last_trip = passenger.trips[-1] last_trips_driver = passengers_last_trip.driver_id driver = dispatcher.find_driver(last_trips_driver) - expect(driver.trips.length).must_equal 5 end @@ -121,10 +110,6 @@ def build_test_dispatcher last_trips_driver = passengers_last_trip.driver_id driver = dispatcher.find_driver(last_trips_driver) expect(drivers.include?(last_trips_driver)).must_equal false - # count how many available driver - # request trip - # expect one less available driver - expect(driver.status).must_equal :UNAVAILABLE end @@ -133,12 +118,10 @@ def build_test_dispatcher dispatcher.drivers.each do |driver| driver.status = :UNAVAILABLE end - expect {dispatcher.request_trip(1) }.must_raise ArgumentError + expect { dispatcher.request_trip(1) }.must_raise ArgumentError end - # Give dispatcher only unavailable drivers to raise ArgumentError - Set all driver to unavailable? Give one unavailable driver in drivers? end - # TODO: un-skip for Wave 2 describe "drivers" do describe "find_driver method" do before do @@ -163,7 +146,6 @@ def build_test_dispatcher it "accurately loads driver information into drivers array" do first_driver = @dispatcher.drivers.first last_driver = @dispatcher.drivers.last - expect(first_driver.name).must_equal "Driver 1 (unavailable)" expect(first_driver.id).must_equal 1 expect(first_driver.status).must_equal :UNAVAILABLE @@ -183,17 +165,3 @@ def build_test_dispatcher end end end - -# Wave 3 TripDispatcher tests: - -# Was the trip created properly? -# Were the trip lists for the driver and passenger updated? -# Was the driver who was selected AVAILABLE? -# What happens if you try to request a trip when there are no AVAILABLE drivers? - -# Interaction with Waves 1 & 2 -# One thing you may notice is that this change breaks your code from previous waves, possibly in subtle ways. We've added a new kind of trip, an in-progress trip, that is missing some of the values you need to compute those numbers. - -# Your code from waves 1 & 2 should ignore any in-progress trips. That is to say, any trip where the end time is nil should not be included in your totals. - -# You should also add explicit tests for this new situation. For example, what happens if you attempt to calculate the total money spent for a Passenger with an in-progress trip, or the average rating of a Driver with an in-progress trip? diff --git a/test/trip_test.rb b/test/trip_test.rb index b6c2351d4..6b111e46a 100644 --- a/test/trip_test.rb +++ b/test/trip_test.rb @@ -1,5 +1,5 @@ -require_relative 'test_helper' -require 'time' +require_relative "test_helper" +require "time" describe "Trip class" do describe "initialize" do @@ -11,13 +11,13 @@ passenger: RideShare::Passenger.new( id: 1, name: "Ada", - phone_number: "412-432-7640" + phone_number: "412-432-7640", ), start_time: start_time, end_time: end_time, cost: 23.45, rating: 3, - driver: RideShare::Driver.new(id: 20, name: "Renoir", vin: "SAR73WZ23J2SEJGJS", status: :UNAVAILABLE) + driver: RideShare::Driver.new(id: 20, name: "Renoir", vin: "SAR73WZ23J2SEJGJS", status: :UNAVAILABLE), } @trip = RideShare::Trip.new(@trip_data) end @@ -45,58 +45,54 @@ # Test for ArgumentError if the end time is before the start time it "raises an error if end time is before start time" do - start_time = Time.now - end_time = start_time - 25 * 60 # earlier than start_time + start_time = Time.now + end_time = start_time - 25 * 60 # earlier than start_time @trip_data = { id: 8, passenger: RideShare::Passenger.new( id: 1, name: "Ada", - phone_number: "412-432-7640" + phone_number: "412-432-7640", ), start_time: start_time, end_time: end_time, cost: 23.45, rating: 3, driver: RideShare::Driver.new( - id: 1, - name: 'Da Vinci', - vin: 'RFWNJWGU3Y8SD2VP0', - status: :AVAILABLE - ) + id: 1, + name: "Da Vinci", + vin: "RFWNJWGU3Y8SD2VP0", + status: :AVAILABLE, + ), } - expect{RideShare::Trip.new(@trip_data)}.must_raise ArgumentError + expect { RideShare::Trip.new(@trip_data) }.must_raise ArgumentError end - end # Wave 1: Test for Trip class instance method to calculate the duration of the trip in seconds describe "calculate duration" do it "finds trip duration in seconds" do - start_time = Time.parse('2018-12-27 02:00:00 -0800') - end_time = Time.parse('2018-12-27 02:01:00 -0800') - + start_time = Time.parse("2018-12-27 02:00:00 -0800") + end_time = Time.parse("2018-12-27 02:01:00 -0800") @trip_data = { id: 8, passenger: RideShare::Passenger.new( id: 1, name: "Ada", - phone_number: "412-432-7640" + phone_number: "412-432-7640", ), start_time: start_time, end_time: end_time, cost: 23.45, rating: 3, driver: RideShare::Driver.new( - id: 1, - name: 'Da Vinci', - vin: 'RFWNJWGU3Y8SD2VP0', - status: :AVAILABLE - ) + id: 1, + name: "Da Vinci", + vin: "RFWNJWGU3Y8SD2VP0", + status: :AVAILABLE, + ), } - @trip = RideShare::Trip.new(@trip_data) - expect(@trip.calculate_duration).must_equal 60 end end -end \ No newline at end of file +end