Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
64d1b3d
Attempted Wave 1.1: Upgrading Times
ydunbar Feb 25, 2020
e67c4cb
Corrected error in trip.rb's ArgumentError for when end_time is not l…
ydunbar Feb 25, 2020
18f6015
Added and passed test for raising ArgumentError when trip end time is…
ydunbar Feb 25, 2020
373936e
Added method to calculate tripduration in seconds, and wrote and pass…
ydunbar Feb 25, 2020
d25b549
Added methods to passenger to find net expenditures and total time spent
ydunbar Feb 26, 2020
9cb6a2f
Added test for Passenger net_expenditures method
ydunbar Feb 26, 2020
2533a5e
Added test for Passenger total_time_spent method
ydunbar Feb 26, 2020
0d7fab1
Created driver.rb file and began creating the Driver class
ydunbar Feb 26, 2020
cff5926
Added methods for Driver class and attempted to pass tests
ydunbar Feb 27, 2020
95df3cf
Worked on passing tests for Wave 1 with Driver class added
ydunbar Feb 27, 2020
6f9f63f
Re-ordered code in Driver's initialize method to be in more logical a…
ydunbar Feb 27, 2020
3e77aa1
Started re-factoring Wave 1 tests
ydunbar Feb 27, 2020
0c497da
added tests to passenger_test.rb
ydunbar Feb 27, 2020
4824c6b
Got Wave 2 tests passing
ydunbar Feb 27, 2020
21221c4
Added request_trip method for Wave 3
ydunbar Feb 27, 2020
00bbef0
fixed indentation and syntax in TripDispatcher
ydunbar Feb 28, 2020
ffdc291
Refactored request_trip method
ydunbar Feb 28, 2020
0846868
added a test for request_trip
ydunbar Feb 28, 2020
c5109dc
Refactored request_trip method
ydunbar Feb 28, 2020
c9068b4
Passed tests for Wave 3
ydunbar Feb 29, 2020
0184427
cleaned up extra spaces and comments
ydunbar Feb 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'http://rubygems.org'

ruby '2.5.5'
ruby '2.6.3'

gem 'rake'

Expand Down
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
25 changes: 12 additions & 13 deletions lib/csv_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'csv'
require "csv"

module RideShare
class CsvRecord
Expand All @@ -8,30 +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
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)
Expand All @@ -40,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
Expand Down
62 changes: 62 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require_relative "csv_record"

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

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)
Comment on lines +28 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a really good opportunity for refactoring to use an Enumerable method, like sum, instead of needing to increment rating_total in an each loop

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
end

private

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
31 changes: 24 additions & 7 deletions lib/passenger.rb
Original file line number Diff line number Diff line change
@@ -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: nil)
def initialize(id:, name:, phone_number:, trips: [])
super(id)

@name = name
@phone_number = phone_number
@trips = trips || []
Expand All @@ -16,14 +15,32 @@ 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)
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
76 changes: 54 additions & 22 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,66 @@
require 'csv'
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:,
passenger: nil,
passenger_id: nil,
start_time:,
end_time:,
cost: nil,
rating:
)
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 != nil
if end_time > start_time
@start_time = start_time
@end_time = end_time
else
raise ArgumentError, "Those are invalid times"
end
end

@start_time = start_time
@end_time = end_time
@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.
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

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}>"
Expand All @@ -51,16 +71,28 @@ def connect(passenger)
passenger.add_trip(self)
end

def connect_driver(driver)
@driver = driver
driver.add_trip(self)
end

# Wave 1: Instance method to calculate the duration of the trip in seconds
def calculate_duration
duration = @end_time - @start_time
return duration
end

private

def self.from_csv(record)
return self.new(
id: record[:id],
driver_id: record[:driver_id],
passenger_id: record[:passenger_id],
start_time: record[:start_time],
end_time: record[:end_time],
start_time: Time.parse(record[:start_time]),
end_time: Time.parse(record[:end_time]),
cost: record[:cost],
rating: record[:rating]
rating: record[:rating],
)
end
end
Expand Down
Loading