// Finds the worst airports to fly from and fly to static void FindWorstAirports() { List<Airport> airports = new List<Airport>(); StreamReader file = new StreamReader(AirlinePerformanceFileName); file.ReadLine(); // Skip the first line string line; while ((line = file.ReadLine()) != null) { string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 7) continue; // Create and fill up a unique airport list with airport objects // Set the total delay and flight count properties for each object Airport departureAirport = new Airport(); Airport arrivalAirport = new Airport(); departureAirport.AirportName = parts[1]; arrivalAirport.AirportName = parts[2]; if (!airports.Exists(a => a.AirportName == departureAirport.AirportName)) airports.Add(departureAirport); if (!airports.Exists(a => a.AirportName == arrivalAirport.AirportName)) airports.Add(arrivalAirport); airports.Find(x => x.AirportName == departureAirport.AirportName).TotalDepartureDelay += int.Parse(parts[3]); airports.Find(x => x.AirportName == departureAirport.AirportName).DepartureFlightCount++; airports.Find(x => x.AirportName == arrivalAirport.AirportName).TotalArrivalDelay += int.Parse(parts[4]); airports.Find(x => x.AirportName == arrivalAirport.AirportName).ArrivalFlightCount++; } file.Close(); List<Airport> worstAirportToFlyFrom = airports.OrderByDescending(a => a.AverageDepartureDelay).ToList(); List<Airport> worstAirportToFlyTo = airports.OrderByDescending(a => a.AverageArrivalDelay).ToList(); Console.WriteLine("The worst airport to fly from is {0} with an average departure delay of {1}.", worstAirportToFlyFrom.First().AirportName, worstAirportToFlyFrom.First().AverageDepartureDelay); Console.WriteLine("The worst airport to fly to is {0} with an average arrival delay of {1}.", worstAirportToFlyTo.First().AirportName, worstAirportToFlyTo.First().AverageArrivalDelay); }
// Finds the worst airports to fly from and fly to static void FindWorstAirports() { StreamReader myFile = new StreamReader("airline-on-time-performance-sep2014-us.csv"); bool first = true; string line = ""; List<Airport> airports = new List<Airport>(); while ((line = myFile.ReadLine()) != null) { if (first) { first = false; continue; } string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 7) continue; // Create and fill up a unique airport list with airport objects // Set the total delay and flight count properties for each object Airport departureAirport = new Airport(); Airport arrivalAirport = new Airport(); departureAirport.AirportName = parts[1]; arrivalAirport.AirportName = parts[2]; if (!airports.Exists(a => a.AirportName == departureAirport.AirportName)) airports.Add(departureAirport); if (!airports.Exists(a => a.AirportName == arrivalAirport.AirportName)) airports.Add(arrivalAirport); airports.Find(x => x.AirportName == departureAirport.AirportName).totalDepartureDelay += int.Parse(parts[3]); airports.Find(x => x.AirportName == departureAirport.AirportName).departureFlightCount++; airports.Find(x => x.AirportName == arrivalAirport.AirportName).totalArrivalDelay += int.Parse(parts[4]); airports.Find(x => x.AirportName == arrivalAirport.AirportName).arrivalFlightCount++; } myFile.Close(); foreach (Airport item in airports) { item.calcAvgDepatureDelay(); item.calcAvgArrivalDelay(); } Airport worstAirportToFlyFrom = new Airport(); Airport worstAirportToFlyTo = new Airport(); worstAirportToFlyFrom = airports.MaxBy(a => a.avgDepartureDelay); worstAirportToFlyTo = airports.MaxBy(a => a.avgArrivalDelay); Console.WriteLine("The worst airport to fly from is {0} with an average departure delay of {1}.", worstAirportToFlyFrom.AirportName, worstAirportToFlyFrom.avgDepartureDelay); Console.WriteLine("The worst airport to fly to is {0} with an average arrival delay of {1}.", worstAirportToFlyTo.AirportName, worstAirportToFlyTo.avgArrivalDelay); }