From f88041abf5ce4146ab6e000bde8fabc48d5fe74b Mon Sep 17 00:00:00 2001 From: Ryan Moser Date: Thu, 3 Jan 2013 12:26:38 -0500 Subject: [PATCH 1/2] Panda level complete --- lib/sales_person.rb | 16 ++++++++++++++++ salesperson.rb | 10 ++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index cb481cd..c09c845 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -12,4 +12,20 @@ def schedule_city(city) def route CalculatesRoute.calculate(cities) end + + def start_city(fail_count = 0) + 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 diff --git a/salesperson.rb b/salesperson.rb index 9cafbd7..34aabb8 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -2,9 +2,11 @@ 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 phil.route From 88847c2befc37c84c26c59bbcb93473cdd0a92b9 Mon Sep 17 00:00:00 2001 From: Ryan Moser Date: Mon, 7 Jan 2013 12:42:16 -0500 Subject: [PATCH 2/2] Eagle level complete --- lib/calculates_route.rb | 17 ++++++++++++++++- salesperson.rb | 2 ++ spec/calculates_route_spec.rb | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index ee223f8..648bf0d 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -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 @@ -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 diff --git a/salesperson.rb b/salesperson.rb index 34aabb8..078e114 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -9,4 +9,6 @@ phil.start_city +puts Map.distance_between(phil.cities.first, phil.cities.last) + puts phil.route diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index 302693b..989f7e5 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -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