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

Melissa_Pushpa_Edges_OO_RideShare #24

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bafad52
Parsed time into string
Aug 28, 2018
36cd4ef
Fix the broken test data - see slack
pushpaagr Aug 28, 2018
fa81cd2
wave 1.2, with argument passing
pushpaagr Aug 29, 2018
8e87c64
new specs data
Aug 29, 2018
80691e2
Merge branch 'master' of https://github.com/melicious-dish/oo-ride-share
Aug 29, 2018
e557f9e
added duration method, added duration test in trip and trip_spec
Aug 29, 2018
17d2d57
For wave 1.2, added net_expenitures method and coorsponding test
Aug 29, 2018
e9ea4c8
Added test fot total_time_spent in user.spec.rb
Aug 30, 2018
73e59b3
added notes to trip_dispatcher.rb
Aug 30, 2018
26d0084
Wave 1 done added total_time_spent method and added test that was suc…
pushpaagr Aug 30, 2018
c462dc7
updated driver_spec code per Chris reccomedations.
pushpaagr Aug 30, 2018
7786a7b
added driver.rb, and passed test for instance of a driver
pushpaagr Aug 30, 2018
f6d990d
working on passing driver vin id
pushpaagr Aug 30, 2018
99d9fc9
got vin test to pass in driver_spec
pushpaagr Aug 30, 2018
b86a02e
set @driven_trips to empty array
Aug 30, 2018
4ee9bfc
Driver attributes and data types set to test requirement
Aug 30, 2018
504ba78
Added driver CSV to trip_dispater.rb
Aug 31, 2018
cd614e3
added driver to drivers array
Aug 31, 2018
f47211e
drivers data to parsed_trip
Aug 31, 2018
55281ff
added driver with all the info, including trip
pushpaagr Aug 31, 2018
f3a0dc1
added driver load_trip and got the tests to pass
pushpaagr Aug 31, 2018
02bbe41
got all tests to pass for load driver, including the weird test
pushpaagr Aug 31, 2018
a1e6481
got add_driven_trip method test to pass
pushpaagr Aug 31, 2018
9ef0cb6
argument error for trip instance passed in driver spec
pushpaagr Aug 31, 2018
98806cb
added average rating, and got tests to pass, also have add_driven tr…
pushpaagr Sep 1, 2018
6d6728e
Added return 0 for an empty arrayif no given trips
Sep 1, 2018
9fbfce8
passed correctly calculates the average rating
Sep 1, 2018
90216ff
calculates the drivers total revenue across all of the trips
Sep 1, 2018
5de24c1
Merge branch 'master' into master
melicious-dish Sep 1, 2018
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
86 changes: 86 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require_relative 'trip'
require 'pry'

module RideShare

class Driver < User

attr_reader :vin, :status, :driven_trips

def initialize(input)

if input[:id].nil? || input[:id] <= 0
raise ArgumentError, 'ID cannot be blank or less than zero.'
end

Choose a reason for hiding this comment

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

Doesn't User already check this? If not it probably should.


if input[:vin] == nil || input[:vin].length != 17 || input[:vin] == " "
# binding.pry
raise ArgumentError, 'Vin inaccurate, must be 17 characters long.'
end


super(input)
# @id = input[:id].to_i
# @name = input[:name].to_s
@vin = input[:vin].to_s
@status = input[:status]
@driven_trips = []

# status_array = [:AVAILABLE, :UNAVAILABLE ]




# binding.pry
# unless @status.include?(status_array)
# raise ArgumentError. "Invalid status, you entered: #{status}"
# end

Choose a reason for hiding this comment

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

Why is this commented out? It does look like line 35 is backward (should be unless status_array.include?(@status).



end

def add_driven_trip(trip)
unless trip.is_a? Trip
raise ArgumentError, "Got a thing that wasn't a trip! (#{trip.class})"
end
@driven_trips << trip

end




def average_rating #sums rating from all drivers trips and returns the average
trip_sum = 0.0
@driven_trips.each do |trip|
trip_sum += trip.rating
end
if @driven_trips == []
return 0
else
return trip_sum / @driven_trips.length
end
end


def total_revenue #calculates the drivers total revenue across all of the trips
sum = 0.0
sum_tax = 1.65
total_rev = 0
@driven_trips.each do |trip|
sum = (trip.cost - sum_tax) * 0.8

Choose a reason for hiding this comment

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

This code would be a little more readable if the two magic numbers (0.8 and 1.65) were stored in constants, maybe something like DRIVER_CUT and FEE.

total_rev += sum
end
return total_rev
end

def net_expenditures
end

Choose a reason for hiding this comment

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

One possible implementation:

def net_expenditures
  return (super - total_revenue).round(2)
end

The key here is taking advantage of code you've already written, both from the parent class using super, and another method in this class.



end



end
22 changes: 18 additions & 4 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
require 'csv'
require 'pry'

module RideShare
class Trip
attr_reader :id, :passenger, :start_time, :end_time, :cost, :rating
attr_reader :id, :driver, :passenger, :start_time, :end_time, :cost, :rating

def initialize(input)
@id = input[:id]
@passenger = input[:passenger]
@id = input[:id] #trip ID
@driver = input[:driver] #driver for the trip
@passenger = input[:passenger] #passenger ID
@start_time = input[:start_time]
@end_time = input[:end_time]
@cost = input[:cost]
@rating = input[:rating]
@rating = input[:rating].to_f

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


unless @end_time > @start_time
raise ArgumentError.new("end time can not be before the start time end time is: #{@end_time} start time is #{@start_time}")
end

end

def duration
# binding.pry
trip_in_seconds = @end_time - @start_time
return trip_in_seconds
end

def inspect
Expand Down
89 changes: 75 additions & 14 deletions lib/trip_dispatcher.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
require 'csv'
require 'time'

require 'pry'
require_relative 'user'
require_relative 'trip'
require_relative 'driver'

module RideShare
class TripDispatcher
attr_reader :drivers, :passengers, :trips

def initialize(user_file = 'support/users.csv',
trip_file = 'support/trips.csv')
trip_file = 'support/trips.csv',
driver_file = 'support/drivers.csv')
@passengers = load_users(user_file)
@drivers = load_drivers(driver_file)
@trips = load_trips(trip_file)
end

Expand All @@ -36,29 +39,87 @@ def load_trips(filename)
header_converters: :symbol)

trip_data.each do |raw_trip|

passenger = find_passenger(raw_trip[:passenger_id].to_i)
driver = find_driver(raw_trip[:driver_id].to_i)


start_time = Time.parse(raw_trip[:start_time])
end_time = Time.parse(raw_trip[:end_time])

# in trips ID = Driver ID
parsed_trip = {
id: raw_trip[:id].to_i,
passenger: passenger,
start_time: raw_trip[:start_time],
end_time: raw_trip[:end_time],
id: raw_trip[:id].to_i, #trip ID
passenger: passenger, #passenger ID
start_time: start_time,
end_time: end_time,
cost: raw_trip[:cost].to_f,
rating: raw_trip[:rating].to_i
rating: raw_trip[:rating].to_i,
driver: driver
}

# adding passenger from user.rb to into trip
trip = Trip.new(parsed_trip)
passenger.add_trip(trip)
driver.add_trip(trip)
driver.add_trip(trip)
trips << trip
end

end
return trips
end

def find_passenger(id)
check_id(id)
return @passengers.find { |passenger| passenger.id == id }
end
def load_drivers(filename)

drivers = []

driver_data = CSV.open(filename, 'r', headers: true,
header_converters: :symbol)

driver_data.each do |raw_driver|

driver_name = find_passenger(raw_driver[:id].to_i).name
driver_phone = find_passenger(raw_driver[:id].to_i).phone_number

parsed_trip = {
id: raw_driver[:id].to_i, #trip ID
vin: raw_driver[:vin],
status: raw_driver[:status].to_sym,
name: driver_name.to_s,
phone: driver_phone

}


drivers << Driver.new(parsed_trip)
# binding.pry
# # find them and replace the `User` object with a `Driver` object. You should also be loading the `driven_tripss` for each `Driver` at this stage.
# # driver = @passengers.find do |user|
# # user == driver
#
end
# # add to drivers array
# # passenger.add_driver(driver)
# binding.pry
#
# drivers << driver
# # end
# end
return drivers

end

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

def find_passenger(id)
check_id(id)
return @passengers.find { |passenger| passenger.id == id }
end




def inspect
return "#<#{self.class.name}:0x#{self.object_id.to_s(16)} \
Expand All @@ -67,7 +128,7 @@ def inspect
#{passengers.count} passengers>"
end

private
# private

def check_id(id)
raise ArgumentError, "ID cannot be blank or less than zero. (got #{id})" if id.nil? || id <= 0
Expand Down
26 changes: 25 additions & 1 deletion lib/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'pry'

module RideShare
class User
attr_reader :id, :name, :phone_number, :trips
Expand All @@ -7,7 +9,7 @@ def initialize(input)
raise ArgumentError, 'ID cannot be blank or less than zero.'
end

@id = input[:id]
@id = input[:id] #user_ID/PassengerID
@name = input[:name]
@phone_number = input[:phone]
@trips = input[:trips].nil? ? [] : input[:trips]
Expand All @@ -16,5 +18,27 @@ def initialize(input)
def add_trip(trip)
@trips << trip
end


# return total amount of money user spent on trips
def net_expenditures
ride_total = 0
@trips.each do |trip|
ride_total += trip[:cost]

Choose a reason for hiding this comment

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

Both these methods will hit an error if you try to run them on a user with an incomplete trip. There are a couple of workarounds for this.

You could explicitly ignore trips with a nil cost:

trips.each do |trip|
  if trip.cost.nil?
    next
  end
  # ... add to the total ...
end

Or even better, you could write a helper method that returns a list of complete trips:

def completed_trips
  return @trips.reject { |t| t.end_time.nil? }
end

def net_expenditures
  completed_trips.each do |trip|
    # ... same logic as before ...
  end
end

Choose a reason for hiding this comment

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

While the above is true, it is less relevant given that you didn't complete wave 3.

end
return ride_total
end

# RideShare::Trip.duration - don't need RideShare? NO, same module

# return total amout of time user has spent on the trips
def total_time_spent
total_time = 0
@trips.each do |trip|
trip_in_seconds = trip[:end_time] - trip[:start_time]
total_time += trip_in_seconds
end
return total_time
end
end
end
40 changes: 28 additions & 12 deletions specs/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'spec_helper'

xdescribe "Driver class" do
describe "Driver class" do

describe "Driver instantiation" do
before do
Expand All @@ -18,28 +18,25 @@
expect{ RideShare::Driver.new(id: 0, name: "George", vin: "33133313331333133")}.must_raise ArgumentError
end


it "throws an argument error with a bad VIN value" do
expect{ RideShare::Driver.new(id: 100, name: "George", vin: "")}.must_raise ArgumentError
expect{ RideShare::Driver.new(id: 100, name: "George", vin: "33133313331333133extranums")}.must_raise ArgumentError
end

it "has a default status of :AVAILABLE" do
expect( (RideShare::Driver.new(id: 100, name: "George", vin: "12345678901234567")).status).must_equal :AVAILABLE
end

it "sets driven trips to an empty array if not provided" do
expect(@driver.driven_trips).must_be_kind_of Array
expect(@driver.driven_trips.length).must_equal 0
end

it "is set up for specific attributes and data types" do
[:id, :name, :vehicle_id, :status, :driven_trips].each do |prop|
[:id, :name, :vin, :status, :driven_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.vehicle_id).must_be_kind_of String
expect(@driver.vin).must_be_kind_of String
expect(@driver.status).must_be_kind_of Symbol
end
end
Expand Down Expand Up @@ -69,7 +66,7 @@
vin: "1C9EVBRM0YBC564DZ")
trip = RideShare::Trip.new(id: 8, driver: @driver, passenger: nil,
start_time: Time.parse("2016-08-08"),
end_time: Time.parse("2016-08-08"), rating: 5)
end_time: Time.parse("2016-08-09"), rating: 5)
@driver.add_driven_trip(trip)
end

Expand Down Expand Up @@ -102,11 +99,30 @@

end

describe "total_revenue" do
# You add tests for the total_revenue method
describe "total_revenue" do
before do
@driver = RideShare::Driver.new(id: 54, name: "Rogers Bartell IV",
vin: "1C9EVBRM0YBC564DZ")
trip1 = RideShare::Trip.new(id: 8, driver: @driver, passenger: nil,
start_time: Time.parse("2016-08-08"),
end_time: Time.parse("2016-08-09"), rating: 5, cost: 10.00)
@driver.add_driven_trip(trip1)
trip2 = RideShare::Trip.new(id: 8, driver: @driver, passenger: nil,
start_time: Time.parse("2016-08-08"),
end_time: Time.parse("2016-08-09"), rating: 5, cost: 20.00)
@driver.add_driven_trip(trip2)

end


it " driver's total revenue across all their trips"do

expect(@driver.total_revenue).must_equal 21.36
end

end

describe "net_expenditures" do
xdescribe "net_expenditures" do
# You add tests for the net_expenditures method
end
end
end
2 changes: 1 addition & 1 deletion specs/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
require_relative '../lib/user'
require_relative '../lib/trip'
require_relative '../lib/trip_dispatcher'
#require_relative '../lib/driver'
require_relative '../lib/driver'
4 changes: 2 additions & 2 deletions specs/test_data/trips_test.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id,driver_id,passenger_id,start_time,end_time,cost,rating
1,2,1,2018-05-25 11:52:40 -0700,2018-05-25 12:25:00 -0700,10,5
2,2,3,2018-07-23 04:39:00 -0700,2018-07-25 04:55:00 -0700,7,3
2,2,3,2018-07-23 04:39:00 -0700,2018-07-23 04:55:00 -0700,7,3
3,5,4,2018-06-11 22:22:00 -0700,2018-06-11 22:57:00 -0700,15,4
4,5,7,2018-08-12 15:04:00 -0700,2018-08-12 15:14:00 -0700,8,1
5,5,6,2018-08-05 08:58:00 -0700,2018-08-05 09:30:00 -0700,32,1
5,5,6,2018-08-05 08:58:00 -0700,2018-08-05 09:30:00 -0700,32,1
Loading