public SearchResultConnection FindDirectConnectionWitMaxWaitingTime(Repository repository, SoughtConnection soughtConnection, TimeOfArrival maxWaitingTime) { List<BusStop> busStops = repository.BusStops; if (busStops == null) { return null; } TypeOfDayRecognizer dayRecognizer = new TypeOfDayRecognizer(); List<string> dayTypes = dayRecognizer.RecognizeTypeOfDay(soughtConnection.DateAndTime); TracksGiverForSpecifiedDayType tracksGiverForSpecfiedDayType = new TracksGiverForSpecifiedDayType(); BusStop startBusStop = null; BusStop endBusStop = null; FindStartAndEndBusStop(ref startBusStop, ref endBusStop, soughtConnection.StartBusStop, soughtConnection.EndBusStop, busStops); List<Line> linesPlyingThroughBothBusStops = GiveLinesPlyingThroughTwoBusStops(startBusStop, endBusStop); List<LineForSpecifiedDayType> allTracksFromStartToEndBusStopInSpecifiedDayType = tracksGiverForSpecfiedDayType.GiveLinesForSpecifiedDayType(linesPlyingThroughBothBusStops, dayTypes); SearchResultConnection result = GiveDirectConnectionWitMaxWaitingTime(allTracksFromStartToEndBusStopInSpecifiedDayType, soughtConnection, maxWaitingTime); if (result != null) { return result; } DateTime newDateForSoughtConnection = new DateTime(); if (soughtConnection.IsDeparture) { DateTime dayAfterDaySpecifiedByUser = soughtConnection.DateAndTime.AddDays(1); newDateForSoughtConnection = new DateTime(dayAfterDaySpecifiedByUser.Year, dayAfterDaySpecifiedByUser.Month, dayAfterDaySpecifiedByUser.Day, 0, 0, 0); } else { DateTime dayBeforeDaySpecifiedByUser = soughtConnection.DateAndTime.AddDays(-1); newDateForSoughtConnection = new DateTime(dayBeforeDaySpecifiedByUser.Year, dayBeforeDaySpecifiedByUser.Month, dayBeforeDaySpecifiedByUser.Day, 23, 59, 0); } soughtConnection = new SoughtConnection(startBusStop.BusStopName, endBusStop.BusStopName, newDateForSoughtConnection, soughtConnection.IsDeparture); dayTypes = dayRecognizer.RecognizeTypeOfDay(newDateForSoughtConnection); allTracksFromStartToEndBusStopInSpecifiedDayType = tracksGiverForSpecfiedDayType.GiveLinesForSpecifiedDayType(linesPlyingThroughBothBusStops, dayTypes); result = GiveDirectConnectionWitMaxWaitingTime(allTracksFromStartToEndBusStopInSpecifiedDayType, soughtConnection, maxWaitingTime); return result; }
/// <summary> /// Recognizes the neighbour bus stops in specified direction. /// </summary> /// <param name="busStopName">Name of the bus stop. For this bus stop function will search neighbours.</param> /// <param name="repository">The repository.</param> /// <param name="date">The date for searching (in some days one bus stop is next to another, in another - no).</param> /// <param name="direction">The direction. /// For 'Next' value, function will search only bus stops which are next to the 'busStopName' in the tracks. /// For 'Previous' value, function will search only bus stops which are before the 'busStopName' in the tracks.</param> /// <returns>List of the names of neighbour bus stops</returns> public List<string> RecognizeNeighbourBusStopsInSpecifiedDirection(string busStopName, Repository repository, DateTime date, Direction direction) { BusStop busStop = FindBusStop(busStopName, repository.BusStops); List<string> dayTypes = new TypeOfDayRecognizer().RecognizeTypeOfDay(date); List<LineForSpecifiedDayType> specifiedTracksForLines = new TracksGiverForSpecifiedDayType().GiveLinesForSpecifiedDayType(busStop.LinesPlyingThroughBusStop, dayTypes); if (specifiedTracksForLines == null) { return null; } List<string> result = new List<string>(); foreach (LineForSpecifiedDayType specifiedTracksInLine in specifiedTracksForLines) { // simplified - check only first Track for (int j = 0; j < 4 && j < specifiedTracksInLine.TracksForSpecifiedDayType.Count; j++) { Track firstTrack = specifiedTracksInLine.TracksForSpecifiedDayType[j]; int index = -1; bool wasFoundBusStop = false; for (int i = 0; i < firstTrack.TimeOfArrivalOnBusStops.Count; i++) { if (firstTrack.TimeOfArrivalOnBusStops.ElementAt(i).Key == busStopName) { index = i + (int)direction; wasFoundBusStop = true; break; } } if (wasFoundBusStop) { if (index > -1 && index < firstTrack.TimeOfArrivalOnBusStops.Count) { result.Add(firstTrack.TimeOfArrivalOnBusStops.ElementAt(index).Key); } break; } } // in another way for (int j = specifiedTracksInLine.TracksForSpecifiedDayType.Count - 1; j > specifiedTracksInLine.TracksForSpecifiedDayType.Count - 5 && j > 3; j--) { Track lastTrackTrack = specifiedTracksInLine.TracksForSpecifiedDayType[j]; int index = -1; bool wasFoundBusStop = false; for (int i = 0; i < lastTrackTrack.TimeOfArrivalOnBusStops.Count; i++) { if (lastTrackTrack.TimeOfArrivalOnBusStops.ElementAt(i).Key == busStopName) { index = i + (int)direction; wasFoundBusStop = true; break; } } if (wasFoundBusStop) { if (index > -1 && index < lastTrackTrack.TimeOfArrivalOnBusStops.Count) { if (result.Count == 0 || !(result.Last().Equals(lastTrackTrack.TimeOfArrivalOnBusStops.ElementAt(index).Key))) { result.Add(lastTrackTrack.TimeOfArrivalOnBusStops.ElementAt(index).Key); } } break; } } } return result; }