Created
April 3, 2021 07:57
-
-
Save programisti/2080a8d586d850858c93ec414c616958 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
# rubocop:disable Style/Documentation | |
class FlightManager | |
def add_new_flight(flight_number, origin, destination) | |
@flights ||= [] | |
flight = {flight_number: flight_number, origin: origin, destination: destination} | |
already_added = @flights.include?(flight) | |
@flights.push(flight) unless already_added | |
!already_added | |
end | |
def find_flights_between(origin, destination, direction_sensitive) | |
@flights.select do |flight| | |
if direction_sensitive | |
find_direct_flights(flight, origin, destination) | |
else | |
find_flights(flight, origin, destination) | |
end | |
end | |
end | |
private | |
def find_direct_flights(flight, origin, destination) | |
flight[:origin] == origin && flight[:destination] == destination | |
end | |
def find_flights(flight, origin, destination) | |
flight[:origin] == origin || | |
flight[:destination] == destination || | |
flight[:destination] == origin || | |
flight[:origin] == destination | |
end | |
end | |
# rubocop:enable Style/Documentation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment