Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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.5'

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.5p114

BUNDLED WITH
1.17.2
5 changes: 2 additions & 3 deletions lib/csv_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'time'
require 'csv'

module RideShare
Expand All @@ -8,9 +9,7 @@ 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)

Expand Down
93 changes: 93 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require_relative 'csv_record'

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

def initialize(id:, name:, vin:, status: :AVAILABLE, trips: nil)
super(id)
@name = name
@vin = vin
@status = status
@trips = trips || []

if vin.length != 17
raise ArgumentError.new("VIN length has to be 17")
end

driver_status = [:AVAILABLE, :UNAVAILABLE]
if !driver_status.include?(status)
raise ArgumentError.new("Driver status invalid.")
end

end

def add_trip(trip)
@trips << trip
end

def ongoing_trips
ongoing_trip = []
self.trips.each do |trip|
if trip.end_time == nil
ongoing_trip << trip
end
end

return ongoing_trip
end

def average_rating
rating = 0
trips.each do |trip|
if trip.rating == nil
rating += 0
else
rating += trip.rating
end
end

if trips.length == 0
return 0
end

return rating.to_f/trips.length
end

def total_revenue
earnings = 0.0

trips.each do |trip|
if trip.cost <= 1.65
earnings += 0
else
earnings += (trip.cost * 0.80)
end
end

return earnings.to_f.round(2)
end

def status_to_unavailable
self.status = :UNAVAILABLE
end

def get_new_trip(trip)
self.add_trip(trip)
self.status_to_unavailable
end

private

def self.from_csv(record)
return new(
id: record[:id],
name: record[:name],
vin: record[:vin],
status: record[:status].to_sym,
trips: record[:trips]
)
end
end
end
29 changes: 28 additions & 1 deletion lib/passenger.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'time'

require_relative 'csv_record'

module RideShare
Expand All @@ -9,13 +11,38 @@ def initialize(id:, name:, phone_number:, trips: nil)

@name = name
@phone_number = phone_number
@trips = trips || []
@trips = trips || []
end

def add_trip(trip)
@trips << trip
end

def net_expenditures
total_money = 0
@trips.each do |trip|
if trip == nil
return 0
else
total_money += trip.cost
end
end
return total_money
end


def total_time_spent
if @trips.empty? == true
return 0
else
time_duration = (@trips).map do |trip|
trip.time_difference
end

return time_duration.sum
end
end

private

def self.from_csv(record)
Expand Down
72 changes: 53 additions & 19 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,101 @@
require 'csv'
require 'time'

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_id, :driver

def initialize(
id:,
passenger: nil,
passenger_id: nil,
start_time:,
end_time:,
start_time: Time.now,
end_time: nil,
cost: nil,
rating:
rating: nil,
driver_id: nil,
driver: nil
)
super(id)
@start_time = start_time
@end_time = end_time
@cost = cost
@rating = rating

if passenger
@passenger = passenger
@passenger_id = passenger.id

elsif passenger_id
@passenger_id = passenger_id

else
raise ArgumentError, 'Passenger or passenger_id is required'
end

@start_time = start_time
@end_time = end_time
@cost = cost
@rating = rating
if driver
@driver = driver
@driver_id = driver.id
elsif driver_id
@driver_id = driver_id
else
raise ArgumentError, 'Driver or driver_id is required'
end

if @rating > 5 || @rating < 1
if start_time.class != Time
raise ArgumentError.new("Value is not a Time object.")
end

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

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

def time_difference
if @start_time == nil
raise ArgumentError.new("Start time cannot be nil.")
end

return @end_time - @start_time
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}>"
end

def connect(passenger)
def connect_passenger(passenger)
@passenger = passenger
passenger.add_trip(self)
end

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

private

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