Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor calculator and runner

  • Loading branch information...
commit c367fe37be90fc08e3f8d9082d8534e0e5e42334 1 parent 0872754
@gitlaura authored ryanzverner committed
View
30 lib/best_flights.rb
@@ -0,0 +1,30 @@
+class BestFlights
+ class << self
+ def get_cheapest_flight(all_flight_paths)
+ @lowest_price = nil
+ all_flight_paths.each do |flight|
+ if @lowest_price.nil? || flight.price < @lowest_price
+ @best_flight = flight
+ @lowest_price = flight.price
+ elsif @lowest_price == flight.price
+ @best_flight = get_fastest_flight([@best_flight, flight])
+ end
+ end
+ @best_flight
+ end
+
+ def get_fastest_flight(all_flight_paths)
+ @shortest_length = nil
+ all_flight_paths.each do |flight|
+ @length = flight.arr - flight.dep
+ if @shortest_length.nil? || @length < @shortest_length
+ @best_flight = flight
+ @shortest_length = @length
+ elsif @shortest_length == @length
+ @best_flight = get_cheapest_flight([@best_flight, flight])
+ end
+ end
+ @best_flight
+ end
+ end
+end
View
20 lib/flight_formatter.rb
@@ -0,0 +1,20 @@
+class FlightFormatter
+ def self.get_print_format(flights)
+ @line_to_print = ""
+ flights.each do |flight|
+ departure_hour = (flight.dep / 60).to_s
+ departure_hour = "0#{departure_hour}" if departure_hour.to_i < 10
+ departure_min = (flight.dep % 60).to_s
+ departure_min = "0#{departure_min}" if departure_min.to_i < 10
+ departure_time = "#{departure_hour}:#{departure_min}"
+ arrival_hour = (flight.arr / 60).to_s
+ arrival_hour = "0#{arrival_hour}" if arrival_hour.to_i < 10
+ arrival_min = (flight.arr % 60).to_s
+ arrival_min = "0#{arrival_min}" if arrival_min.to_i < 10
+ arrival_time = "#{arrival_hour}:#{arrival_min}"
+ price = flight.price
+ @line_to_print << "#{departure_time} #{arrival_time} #{price}0\n"
+ end
+ @line_to_print << "\n"
+ end
+end
View
58 lib/flight_route_calculator.rb
@@ -1,5 +1,7 @@
-require_relative 'flight_parser.rb'
-require_relative 'all_paths.rb'
+require_relative("all_paths.rb")
+require_relative("best_flights.rb")
+require_relative("flight_formatter.rb")
+require_relative("flight_parser.rb")
class FlightRouteCalculator
class << self
@@ -7,60 +9,20 @@ def get_cases(file)
FlightParser.parse(file)
end
- def find_best_flights(flight_cases, departure_city, arrival_city)
- flight_cases.each do |flight_case|
- all_flight_paths = AllPaths.create(flight_case, departure_city, arrival_city)
- cheapest_flight = get_cheapest_flight(all_flight_paths)
- fastest_flight = get_fastest_flight(all_flight_paths)
- write_to_file([cheapest_flight, fastest_flight])
- end
+ def find_all_flight_paths(flight_case, departure_city, arrival_city)
+ AllPaths.create(flight_case, departure_city, arrival_city)
end
def get_cheapest_flight(all_flight_paths)
- @lowest_price = nil
- all_flight_paths.each do |flight|
- if @lowest_price.nil? || flight.price < @lowest_price
- @best_flight = flight
- @lowest_price = flight.price
- elsif @lowest_price == flight.price
- @best_flight = get_fastest_flight([@best_flight, flight])
- end
- end
- @best_flight
+ BestFlights.get_cheapest_flight(all_flight_paths)
end
def get_fastest_flight(all_flight_paths)
- @shortest_length = nil
- all_flight_paths.each do |flight|
- @length = flight.arr - flight.dep
- if @shortest_length.nil? || @length < @shortest_length
- @best_flight = flight
- @shortest_length = @length
- elsif @shortest_length == @length
- @best_flight = get_cheapest_flight([@best_flight, flight])
- end
- end
- @best_flight
+ BestFlights.get_fastest_flight(all_flight_paths)
end
- def write_to_file(flights)
- file = File.open("output.txt", "w")
- flights.each do |flight|
- departure_hour = (flight.dep / 60).to_s
- departure_hour = "0#{departure_hour}" if departure_hour.to_i < 10
- departure_min = (flight.dep % 60).to_s
- departure_min = "0#{departure_min}" if departure_min.to_i < 10
- departure_time = "#{departure_hour}:#{departure_min}"
- arrival_hour = (flight.arr / 60).to_s
- arrival_hour = "0#{arrival_hour}" if arrival_hour.to_i < 10
- arrival_min = (flight.arr % 60).to_s
- arrival_min = "0#{arrival_min}" if arrival_min.to_i < 10
- arrival_time = "#{arrival_hour}:#{arrival_min}"
- price = flight.price
- line_to_print = "#{departure_time} #{arrival_time} #{price}0"
- file.write("#{line_to_print}\n\n")
- end
- file
+ def get_print_format(flights)
+ FlightFormatter.get_print_format(flights)
end
end
end
View
20 lib/runner.rb
@@ -0,0 +1,20 @@
+require_relative 'flight_route_calculator.rb'
+
+class Runner
+ class << self
+ def run_flight_route_calculator(flights_to_parse, departure_city, arrival_city)
+ cases = FlightRouteCalculator.get_cases(flights_to_parse)
+ cases.each do |flight_case|
+ flight_paths = FlightRouteCalculator.find_all_flight_paths(flight_case, departure_city, arrival_city)
+ cheapest_flight = FlightRouteCalculator.get_cheapest_flight(flight_paths)
+ fastest_flight = FlightRouteCalculator.get_fastest_flight(flight_paths)
+ line_to_print = FlightRouteCalculator.get_print_format([cheapest_flight, fastest_flight])
+ write_to_file(line_to_print)
+ end
+ end
+
+ def write_to_file(line_to_print, file = File.open("output.txt","w"))
+ file.write(line_to_print)
+ end
+ end
+end
View
18 spec/best_flights_spec.rb
@@ -0,0 +1,18 @@
+require 'best_flights'
+
+describe "Best flights" do
+ let(:flight_one){OpenStruct.new(:from => "A", :to => "B", :dep => 540, :arr => 600, :price => 100.00)}
+ let(:flight_two){OpenStruct.new(:from => "B", :to => "Z", :dep => 690, :arr => 810, :price => 100.00)}
+ let(:flight_three){OpenStruct.new(:from => "A", :to => "Z", :dep => 600, :arr => 720, :price => 300.00)}
+ let(:case_one){[flight_one, flight_two, flight_three]}
+
+ it "gets the cheapest flight" do
+ best_flight = BestFlights.get_cheapest_flight(case_one)
+ expect(best_flight).to eq(flight_one)
+ end
+
+ it "gets the fastest flight" do
+ best_flight = BestFlights.get_fastest_flight(case_one)
+ expect(best_flight).to eq(flight_one)
+ end
+end
View
11 spec/flight_formatter_spec.rb
@@ -0,0 +1,11 @@
+require 'flight_formatter'
+
+describe "Flight formatter" do
+ let(:flight_one){OpenStruct.new(:from => "A", :to => "B", :dep => 540, :arr => 600, :price => 100.00)}
+ let(:flight_two){OpenStruct.new(:from => "B", :to => "Z", :dep => 690, :arr => 810, :price => 100.00)}
+
+ it "creates the correct flight format" do
+ line = FlightFormatter.get_print_format([flight_one, flight_two])
+ expect(line).to include("9:00 10:00 100.00\n")
+ end
+end
View
11 spec/flight_route_calculator_spec.rb
@@ -15,7 +15,7 @@
end
it "finds the best flights for each case" do
- expect(FlightRouteCalculator).to respond_to(:find_best_flights)
+ expect(FlightRouteCalculator).to respond_to(:find_all_flight_paths)
end
it "finds the cheapest flight" do
@@ -28,11 +28,8 @@
expect(best_flight).to eq(flight_one)
end
- it "write to a file" do
- output_file = FlightRouteCalculator.write_to_file([flight_one, flight_two])
- read_file = File.open(output_file).read
- read_file.each_line do |line|
- expect(line.start_with?("09:00 10:00 100.00")).to eq(true)
- end
+ it "formats the flights for print" do
+ line_to_print = FlightRouteCalculator.get_print_format([flight_two, flight_three])
+ expect(line_to_print).to include("10:00 12:00 300.00\n\n")
end
end
View
25 spec/runner_spec.rb
@@ -0,0 +1,25 @@
+require 'runner'
+
+describe "Runner" do
+ let(:file) {"sample-input.txt"}
+ let(:departure_city) {"A"}
+ let(:arrival_city) {"Z"}
+
+ it "gets the cases from the flight parser" do
+ expect(FlightRouteCalculator).to receive(:get_cases) {[1, 1]}
+ expect(FlightRouteCalculator).to receive(:find_all_flight_paths).twice
+ expect(FlightRouteCalculator).to receive(:get_cheapest_flight).twice
+ expect(FlightRouteCalculator).to receive(:get_fastest_flight).twice
+ expect(FlightRouteCalculator).to receive(:get_print_format).twice
+ expect(Runner).to receive(:write_to_file).twice
+ Runner.run_flight_route_calculator(file, departure_city, arrival_city)
+ end
+
+ it "writes to a file" do
+ temp_output = StringIO.new
+ Runner.write_to_file("Hi, I'm Laura", temp_output)
+ temp_output.each_line do |line|
+ expect(line).to eq("Hi, I'm Laura")
+ end
+ end
+end

0 comments on commit c367fe3

Please sign in to comment.
Something went wrong with that request. Please try again.