public static void SearchFligths(string origin, string destination, FlightsCollection flightsData) { string[] providers = { "Provider1.txt", "Provider2.txt", "Provider3.txt" }; char delimiter; string providerPath; for (int i = 0; i < providers.Length; i++) { providerPath = GetProviderPath(providers[i]); delimiter = GetDelimiter(providers[i]); try { StreamReader reader = new StreamReader(providerPath); string recordIn = reader.ReadLine(); while (recordIn != null) { AddFlights(recordIn, delimiter, origin, destination, flightsData); recordIn = reader.ReadLine(); } } catch (Exception e) { Console.WriteLine("the file could not be read"); Console.WriteLine(e.Message); } } }
static void Main(string[] args) { FlightsCollection flightsData; int indexOrigin; int indexDestination; string origin; string destination; string inputText = ""; int cityLength = 3; int space = 3; SearchFlightsMethods.DisplayMenu(ref inputText); while (inputText != "-1") { flightsData = new FlightsCollection(); indexOrigin = inputText.IndexOf("-o") + space; indexDestination = inputText.IndexOf("-d") + space; try { origin = inputText.Substring(indexOrigin, cityLength).ToUpper(); if (origin.Contains('-') || origin.Trim() == "") { throw new Exception(); } destination = inputText.Substring(indexDestination, cityLength).ToUpper(); } catch (Exception e) { Console.WriteLine("\nPlease, enter origin and destination.\n"); SearchFlightsMethods.DisplayMenu(ref inputText); continue; } SearchFlightsMethods.SearchFligths(origin, destination, flightsData); var searchedFlights = flightsData.flightsCollection .OrderBy(flight => flight.Price) .ThenBy(flight => Convert.ToDateTime(flight.DepartureTime)); if (searchedFlights.Any()) { Console.WriteLine(); foreach (var flight in searchedFlights) { Console.WriteLine($"{flight.Origin}-- > {flight.Destination}({flight.DepartureTime}-- >{flight.DestinationTime}) - ${flight.Price}"); } Console.WriteLine("\n\n"); } else { Console.WriteLine($"\nNo Flights Found for {origin} --> {destination}\n"); } SearchFlightsMethods.DisplayMenu(ref inputText); } }
public static void AddFlights(string recordIn, char delimiter, string origin, string destination, FlightsCollection flightsData) { string[] fields; DateTime prvDepartureTime; DateTime prvDestinationTime; Decimal prvPrice; fields = recordIn.Split(delimiter); string prvOrigin = fields[0]; DateTime.TryParse(fields[1].Replace('-', '/'), out prvDepartureTime); string prvDestination = fields[2]; DateTime.TryParse(fields[3].Replace('-', '/'), out prvDestinationTime); Decimal.TryParse(fields[4].Substring(1), out prvPrice); if (prvOrigin == origin && prvDestination == destination) { Flights aFlight = new Flights { Origin = prvOrigin, DepartureTime = prvDepartureTime, Destination = prvDestination, DestinationTime = prvDestinationTime, Price = prvPrice }; if (!flightsData.flightsCollection.Contains(aFlight)) { flightsData.flightsCollection.Add(aFlight); } } }