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

Pull request for Episode 7: Eagle Level #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion lib/calculates_route.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
class CalculatesRoute

@total_miles = 0
@travel_time = 0
@travel_speed = 55

class << self
attr_accessor :total_miles, :travel_time, :travel_speed
end

def self.calculate(points)

remaining_points = points
Expand All @@ -16,7 +24,14 @@ def self.shortest_distance(from, possible)
distances = possible.map do |point|
{point: point, distance: Map.distance_between(from, point)}
end
distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point)
travel_distances = distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}
@total_miles += travel_distances.first[:distance]
@travel_time += travel_distances.first[:distance] / @travel_speed
# distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point)
travel_distances.first.fetch(:point)
end



end

16 changes: 16 additions & 0 deletions lib/sales_person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,20 @@ def schedule_city(city)
def route
CalculatesRoute.calculate(cities)
end

def start_city(fail_count = 0)
Copy link
Member

Choose a reason for hiding this comment

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

nice use of a fail_count and recursion!

if @cities.length > 0
puts "Enter the number of the city where the salesperson will start their journey"
@cities.each_with_index { |city, index| puts "#{index}: #{city}" }
print "> "; city = gets.chomp.to_i
if city >= 0 && city < @cities.length
@cities = ( [@cities[city]] + @cities ).uniq
else
puts "Invalid selection, please try again. #{3 -fail_count} tries remaining"
start_city(fail_count + 1)
end
else
puts "Please schedule cities before selecting a start city"
end
end
end
12 changes: 8 additions & 4 deletions salesperson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@


phil = SalesPerson.new
phil.schedule_city(Place.build("Dallas, TX"))
phil.schedule_city(Place.build("El Paso, TX"))
phil.schedule_city(Place.build("Austin, TX"))
phil.schedule_city(Place.build("Lubbock, TX"))
phil.schedule_city(Place.build("Arlington, VA"))
phil.schedule_city(Place.build("Chantilly, VA"))
phil.schedule_city(Place.build("Silver Spring, MD"))
phil.schedule_city(Place.build("Annapolis, MD"))

phil.start_city

puts Map.distance_between(phil.cities.first, phil.cities.last)

puts phil.route
14 changes: 14 additions & 0 deletions spec/calculates_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@
expected = [dallas, austin, lubbock, el_paso]
CalculatesRoute.calculate(points).should eq(expected)
end

it "should return the total miles in a route" do
predicted_miles = Map.distance_between(dallas, austin) +
Map.distance_between(austin, lubbock) +
Map.distance_between(lubbock, el_paso)
CalculatesRoute.total_miles.should eq(predicted_miles)
end

it "should return the travel time" do
miles_traveled = CalculatesRoute.total_miles
CalculatesRoute.travel_speed = 55
CalculatesRoute.travel_time.should eq(miles_traveled / 55)
end

end