Example #1
0
        public (FlightModel Flight, TimeSpan FlightTime) FindBestFlight(DepartureDays dayOfDeparture, TimeSpan timeOfDeparture)
        {
            var minTime           = TimeSpan.MaxValue;
            var flightWithMinTime = new FlightModel();


            foreach (var flight in Flights)
            {
                var timeUntilEndOfFlight = TimeUntilEndOfFlight(flight, dayOfDeparture, timeOfDeparture);
                if (timeUntilEndOfFlight < minTime)
                {
                    minTime           = timeUntilEndOfFlight;
                    flightWithMinTime = flight;
                }
            }
            return(flightWithMinTime, minTime);
        }
Example #2
0
        private TimeSpan TimeUntilEndOfFlight(FlightModel flight, DepartureDays dayOfDeparture, TimeSpan timeOfDeparture)
        {
            int           daysUntilFlight  = default;
            DepartureDays nearestFlightDay = dayOfDeparture;

            if (timeOfDeparture >= flight.DepartureTime)
            {
                GoToNextDay(ref nearestFlightDay, ref daysUntilFlight);
            }

            while (!flight.DeparturesDays.Contains(nearestFlightDay))
            {
                GoToNextDay(ref nearestFlightDay, ref daysUntilFlight);
            }

            return(new TimeSpan(daysUntilFlight, 0, 0, 0) + (flight.DepartureTime - timeOfDeparture) + flight.TakeTimeInAir());
        }