Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Water: Leah & Sophia #26

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/oo-ride-share.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

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

def initialize(id:, name:, vin:, status: :AVAILABLE, trips: [])
super(id)

@name = name
@vin = vin
raise ArgumentError.new("Invalid VIN") if (vin.length) != 17

Choose a reason for hiding this comment

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

It's helpful for debugging to include the bad argument in your variable:

Suggested change
raise ArgumentError.new("Invalid VIN") if (vin.length) != 17
raise ArgumentError.new("Invalid VIN: #{vin}") if (vin.length) != 17


@status = status
raise ArgumentError.new("Invalid status") if ![:AVAILABLE, :UNAVAILABLE].include?(@status)

@trips = trips

end

def accept_new_trip(trip)
add_trip(trip)
@status = :UNAVAILABLE
end
Comment on lines +22 to +25

Choose a reason for hiding this comment

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

You might want to verify that the driver is available before accepting a trip.


def add_trip(trip)
@trips << trip
end

def average_rating
ratings_array = @trips.reject { |trip| trip.rating == nil }.map(&:rating)

Choose a reason for hiding this comment

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

Great use of enumerable methods!

return 0 if (@trips) == [] || @trips == nil
return (ratings_array.sum/ratings_array.length).to_f
end
Comment on lines +31 to +35

Choose a reason for hiding this comment

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

You should move this check up. If @trips is nil you will get an error when you call @trips.reject:

Suggested change
def average_rating
ratings_array = @trips.reject { |trip| trip.rating == nil }.map(&:rating)
return 0 if (@trips) == [] || @trips == nil
return (ratings_array.sum/ratings_array.length).to_f
end
def average_rating
return 0 if (@trips) == [] || @trips == nil
ratings_array = @trips.reject { |trip| trip.rating == nil }.map(&:rating)
return (ratings_array.sum/ratings_array.length).to_f
end


def total_revenue
return 0 if (@trips) == [] || @trips == nil
trip_cost = @trips.map(&:cost)
return 0 if trip_cost.sum < 1.65
driver_revenue = (trip_cost.sum - 1.65) * 0.80
return driver_revenue.to_f
end

private

def self.from_csv(record)
return new(
id: record[:id],
name: record[:name],
vin: record[:vin],
status: record[:status].to_sym
)
end
end
end
26 changes: 26 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative 'passenger'
require_relative 'trip'
require_relative 'csv_record'
require_relative 'driver'

# passenger1 = Passenger.new(2, "Merten Noles", 8347387434, [1,20,54,2018-12-27 02:39:05 -0800,2018-12-27 03:38:08 -0800,10,4])
#
# passenger1_expenditures = passenger1.net_expenditures
#
# pp passenger1_expenditures
#
# arr = [5,6,1]
#
# arr_sum = arr.reduce(:+)
#
# pp arr_sum
#
# @trips =
#
# def net_expenditures
# array_trip_costs = @trips.map(&:cost)
# total_expenditure = array_trip_costs.reduce(:+)
# return total_expenditure
# end

driver_instance = Driver.new(1,"Paul Klee","WBS76FYD47DJF7206",AVAILABLE)
10 changes: 10 additions & 0 deletions lib/passenger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'csv_record'
require_relative 'trip'

module RideShare
class Passenger < CsvRecord
Expand All @@ -16,6 +17,15 @@ def add_trip(trip)
@trips << trip
end

def net_expenditures
total_expenditure = @trips.sum(&:cost)
return total_expenditure
Comment on lines +21 to +22

Choose a reason for hiding this comment

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

You can directly return @trips.sum(&:cost):

Suggested change
total_expenditure = @trips.sum(&:cost)
return total_expenditure
return @trips.sum(&:cost)

(This saves you from having to come up with a variable name.)

end

def total_time_spent
return @trips.sum(&:duration_trip)
end

private

def self.from_csv(record)
Expand Down
37 changes: 29 additions & 8 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
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:,
end_time: nil,
cost: nil,
rating:
rating: nil,
driver_id: nil,
driver: nil
)
super(id)

Expand All @@ -32,10 +35,18 @@ def initialize(
@end_time = end_time
@cost = cost
@rating = rating
@driver_id = driver_id
@driver = driver

if @rating > 5 || @rating < 1

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

Choose a reason for hiding this comment

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

Nice job including the bad argument here!

end

if @end_time != nil && (@end_time < @start_time)
raise ArgumentError.new("Invalid Time")
end

end

def inspect
Expand All @@ -50,21 +61,31 @@ def inspect
"rating=#{rating}>"
end

def connect(passenger)
def connect(passenger, driver)
@passenger = passenger
passenger.add_trip(self)
@driver = driver
driver.add_trip(self)
end

def duration_trip
duration = @end_time - @start_time
return duration
end


private

def self.from_csv(record)
return self.new(
id: record[:id],
passenger_id: record[:passenger_id],
start_time: record[:start_time],
end_time: record[:end_time],
start_time: Time.parse(record[:start_time]),
end_time: Time.parse(record[:end_time]),
cost: record[:cost],
rating: record[:rating]
rating: record[:rating],
driver_id: record[:driver_id],
driver: record[:driver]
)
end
end
Expand Down
45 changes: 44 additions & 1 deletion lib/trip_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require_relative 'passenger'
require_relative 'trip'
require_relative 'driver'

module RideShare
class TripDispatcher
Expand All @@ -11,6 +12,7 @@ class TripDispatcher
def initialize(directory: './support')
@passengers = Passenger.load_all(directory: directory)
@trips = Trip.load_all(directory: directory)
@drivers = Driver.load_all(directory: directory)
connect_trips
end

Expand All @@ -19,6 +21,45 @@ def find_passenger(id)
return @passengers.find { |passenger| passenger.id == id }
end

def find_driver(id)
Driver.validate_id(id)
return @drivers.find { |driver| driver.id == id }
end

def driver_assigned
driver = @drivers.find { |driver| driver.status == :AVAILABLE }
return driver
end

def request_trip(passenger_id)
driver = driver_assigned
return nil if driver == nil

start_time = Time.now
end_time = nil
passenger = find_passenger(passenger_id)
trip_data = {
id: @trips.length + 1,
passenger: passenger,
start_time: start_time,
end_time: end_time,
cost: nil,
rating: nil,
driver_id: driver.id,
driver: driver
}

new_trip_instance = RideShare::Trip.new(trip_data)
Comment on lines +41 to +52

Choose a reason for hiding this comment

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

You got a warning for this. To fix it directly use keyword arguments instead of passing in a hash:

Suggested change
trip_data = {
id: @trips.length + 1,
passenger: passenger,
start_time: start_time,
end_time: end_time,
cost: nil,
rating: nil,
driver_id: driver.id,
driver: driver
}
new_trip_instance = RideShare::Trip.new(trip_data)
new_trip_instance = RideShare::Trip.new(
id: @trips.length + 1,
passenger: passenger,
start_time: start_time,
end_time: end_time,
cost: nil,
rating: nil,
driver_id: driver.id,
driver: driver
)



driver.accept_new_trip(new_trip_instance)
passenger.add_trip(new_trip_instance)
@trips << new_trip_instance

return new_trip_instance

end

def inspect
# Make puts output more useful
return "#<#{self.class.name}:0x#{object_id.to_s(16)} \
Expand All @@ -30,9 +71,11 @@ def inspect
private

def connect_trips

@trips.each do |trip|
passenger = find_passenger(trip.passenger_id)
trip.connect(passenger)
driver = find_driver(trip.driver_id)
trip.connect(passenger, driver)
end

return trips
Expand Down
Loading