Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
157 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,93 @@ | ||
require 'ostruct' | ||
|
||
class AllPaths | ||
|
||
def initialize(flights, departure_city, arrival_city) | ||
@flights = flights | ||
@departure_city = departure_city | ||
@arrival_city = arrival_city | ||
@final_paths = [] | ||
end | ||
|
||
def create_paths | ||
from_city_groups = group_flights_by_from_city | ||
from_city_groups[@departure_city.to_sym].each do |flight_from_dep_city| | ||
if flight_from_dep_city.to == @arrival_city | ||
@final_paths << flight_from_dep_city | ||
else | ||
@current_path = create_new_path(flight_from_dep_city) | ||
reset_price(flight_from_dep_city) | ||
reset_arrivals(flight_from_dep_city) | ||
reset_arrival_cities(flight_from_dep_city) | ||
update_current_path(flight_from_dep_city.to, from_city_groups) | ||
end | ||
def initialize(flights, departure_city, arrival_city) | ||
@flights = flights | ||
@departure_city = departure_city | ||
@arrival_city = arrival_city | ||
@final_paths = [] | ||
end | ||
|
||
def create_paths | ||
@departure_city_groups = group_flights_by_departure_city | ||
@departure_city_groups[@departure_city.to_sym].each do |flight_from_dep_city| | ||
if flight_from_dep_city.to == @arrival_city | ||
@final_paths << flight_from_dep_city | ||
else | ||
@current_path = create_new_path(flight_from_dep_city) | ||
@path_attributes = reset_path_attributes(flight_from_dep_city) | ||
update_current_path | ||
end | ||
@final_paths | ||
end | ||
|
||
def group_flights_by_from_city | ||
from_city_groups = {} | ||
@flights.each do |flight| | ||
new_from = flight.from.to_sym | ||
from_city_groups[new_from] = [] if from_city_groups[new_from].nil? | ||
from_city_groups[new_from] << flight | ||
@final_paths | ||
end | ||
|
||
def group_flights_by_departure_city | ||
departure_city_groups = {} | ||
@flights.each do |flight| | ||
new_from = flight.from.to_sym | ||
departure_city_groups[new_from] ||= [] | ||
departure_city_groups[new_from] << flight | ||
end | ||
departure_city_groups | ||
end | ||
|
||
def create_new_path(flight) | ||
OpenStruct.new(:from => @departure_city, :to => nil, :dep => flight.dep, :arr => nil, :price => nil) | ||
end | ||
|
||
def reset_path_attributes(flight) | ||
path_attributes = {} | ||
path_attributes[:prices], path_attributes[:arrival_times], path_attributes[:arrival_cities] = [], [], [] | ||
path_attributes[:prices] << flight.price | ||
path_attributes[:arrival_times] << flight.arr | ||
path_attributes[:arrival_cities] << flight.to | ||
path_attributes | ||
end | ||
|
||
def update_current_path(new_path_level = false) | ||
new_dep_city = @path_attributes[:arrival_cities].last.to_sym | ||
@departure_city_groups[new_dep_city].each do |possible_flight| | ||
if new_path_level == false | ||
remove_attributes_from_last_flight if @path_attributes[:arrival_cities].size > 1 | ||
end | ||
from_city_groups | ||
end | ||
|
||
private | ||
|
||
def create_new_path(flight) | ||
OpenStruct.new(:from => @departure_city, :to => nil, :dep => flight.dep, :arr => nil, :price => nil) | ||
end | ||
|
||
def reset_price(flight) | ||
@prices = [] | ||
@prices << flight.price | ||
end | ||
|
||
def reset_arrivals(flight) | ||
@arrival_times = [] | ||
@arrival_times << flight.arr | ||
end | ||
|
||
def reset_arrival_cities(flight) | ||
@arrival_cities = [] | ||
@arrival_cities << flight.to | ||
end | ||
|
||
def update_current_path(current_from_city, from_city_groups, loop = false) | ||
new_from_city = current_from_city.to_sym | ||
from_city_groups[new_from_city].each do |possible_flight| | ||
if loop == false | ||
reset_to_price_from_city if @prices.size > 1 | ||
reset_arrivals_from_city if @arrival_times.size > 1 | ||
reset_arrival_cities_from_city if @arrival_cities.size > 1 | ||
end | ||
if valid_next_flight?(possible_flight) | ||
#update_path(possible_flight) | ||
update_price(possible_flight) | ||
update_arrivals(possible_flight) | ||
update_arrival_cities(possible_flight) | ||
if possible_flight.to == @arrival_city | ||
add_prices_to_path | ||
add_arrivals_to_path(possible_flight) | ||
add_arrival_cities_to_path(possible_flight) | ||
reset_to_price_from_city | ||
reset_arrivals_from_city | ||
reset_arrival_cities_from_city | ||
add_current_path_to_final_paths(possible_flight) | ||
next | ||
end | ||
update_current_path(@arrival_cities.last, from_city_groups, true) | ||
if valid_next_flight?(possible_flight) | ||
update_path_attributes(possible_flight) | ||
if possible_flight.to == @arrival_city | ||
add_attributes_to_path | ||
add_current_path_to_final_paths | ||
remove_attributes_from_last_flight | ||
next | ||
end | ||
update_current_path(true) | ||
end | ||
end | ||
|
||
def valid_next_flight?(possible_flight) | ||
@arrival_times.last < possible_flight.dep && possible_flight.to > possible_flight.from | ||
end | ||
|
||
def update_path(possible_flight) | ||
@current_path.to = possible_flight.to if possible_flight.to != @arrival_city | ||
end | ||
|
||
def update_price(possible_flight) | ||
@prices << possible_flight.price | ||
end | ||
|
||
def update_arrivals(possible_flight) | ||
@arrival_times << possible_flight.arr | ||
end | ||
|
||
def update_arrival_cities(possible_flight) | ||
@arrival_cities << possible_flight.to | ||
end | ||
|
||
def add_prices_to_path | ||
@current_path.price = @prices.reduce(:+) | ||
end | ||
|
||
def add_arrivals_to_path(possible_flight) | ||
@current_path.arr = @arrival_times.last | ||
end | ||
|
||
def add_arrival_cities_to_path(possible_flight) | ||
@current_path.to = @arrival_cities.last | ||
end | ||
|
||
def reset_to_price_from_city | ||
@prices.pop | ||
end | ||
|
||
def reset_arrivals_from_city | ||
@arrival_times.pop | ||
end | ||
|
||
def reset_arrival_cities_from_city | ||
@arrival_cities.pop | ||
end | ||
|
||
def add_current_path_to_final_paths(possible_flight) | ||
end_path = @current_path.dup | ||
@final_paths << end_path | ||
end | ||
end | ||
|
||
def valid_next_flight?(possible_flight) | ||
@path_attributes[:arrival_times].last < possible_flight.dep && possible_flight.to > possible_flight.from | ||
end | ||
|
||
def update_path_attributes(possible_flight) | ||
@path_attributes[:prices] << possible_flight.price | ||
@path_attributes[:arrival_times] << possible_flight.arr | ||
@path_attributes[:arrival_cities] << possible_flight.to | ||
end | ||
|
||
def add_attributes_to_path | ||
@current_path.price = @path_attributes[:prices].reduce(:+) | ||
@current_path.arr = @path_attributes[:arrival_times].last | ||
@current_path.to = @path_attributes[:arrival_cities].last | ||
end | ||
|
||
def remove_attributes_from_last_flight | ||
@path_attributes[:prices].pop | ||
@path_attributes[:arrival_times].pop | ||
@path_attributes[:arrival_cities].pop | ||
end | ||
|
||
def add_current_path_to_final_paths | ||
end_path = @current_path.dup | ||
@final_paths << end_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
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 | ||
flight_one = OpenStruct.new(:from => "A", :to => "B", :dep => 540, :arr => 600, :price => 100.00) | ||
flight_two = OpenStruct.new(:from => "B", :to => "Z", :dep => 690, :arr => 810, :price => 100.00) | ||
|
||
line = FlightFormatter.get_print_format([flight_one, flight_two]) | ||
|
||
expect(line).to include("9:00 10:00 100.00\n") | ||
end | ||
|
||
it "formats the correct time" do | ||
expect(FlightFormatter.format_time(550)).to eq("09:10") | ||
end | ||
|
||
it "formats the correct price" do | ||
expect(FlightFormatter.format_price(375.2)).to eq("375.20") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c8b3b99
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.