using System; using System.Collections.Generic; //using System.Linq; using System.Web; using System.Text; namespace BusScheduler { public class Route { public List cLocation; public string name; public List cTripPattern; public List cTrip; public Route() { cLocation = new List(); cTripPattern = new List(); } public List setcTrip() { cTrip = new List(); foreach (TripPattern tp in cTripPattern) { cTrip.AddRange(tp.setcTrip()); } // storing yesterday's trips. int nTrip = cTrip.Count; int day = 0; while (cTrip.Count < (nTrip + 4)) { day = day + 1; for (int ii = 0; ii < nTrip; ii++) { Trip yTrip = cTrip[ii].Copy(); for (int i = 0; i < yTrip.c1ArrivalTime.Count; i++) { BusScheduler.Time newTime = new BusScheduler.Time(); newTime.setTime(yTrip.c1ArrivalTime[i].total - (day * 24 * 60)); yTrip.c1ArrivalTime[i] = newTime; } cTrip.Add(yTrip); } } return cTrip; } public List cLocationIndex(BusScheduler.Location aLocation) { List cLocationIndex = new List(); List cLocation = this.cLocation; for (int i = 0; i < cLocation.Count; i++) { if (aLocation == cLocation[i]) { cLocationIndex.Add(i); } } if (cLocationIndex.Contains(0) & cLocationIndex.Contains(cLocation.Count - 1)) { cLocationIndex.Remove(0); } return cLocationIndex; } public List cTripNearestTime(BusScheduler.Location aLocation, BusScheduler.Time time) { List cTripNearestTime = new List(); List cLocationIndex = this.cLocationIndex(aLocation); for (int i = 0; i < cTrip.Count; i++) { Trip aTrip = cTrip[i]; if (cLocationIndex.Exists(delegate(int index) { return aTrip.c1ArrivalTime[index].total < time.total; })) { cTripNearestTime.Add(aTrip); } } int j = cLocationIndex[cLocationIndex.Count - 1]; cTripNearestTime.Sort(delegate(Trip t1, Trip t2) { return t1.c1ArrivalTime[j].total.CompareTo(t2.c1ArrivalTime[j].total); }); return cTripNearestTime; } public List startandEndTime(BusScheduler.Location startDest, BusScheduler.Location endDest, BusScheduler.Time aArrivalTime) { List cTripNearestTime = this.cTripNearestTime(endDest, aArrivalTime); int index = cTripNearestTime.Count - 1; Trip bestTrip = cTripNearestTime[index].Copy(); JoinedTrip bestJoinedTrip = new JoinedTrip(); bool found = false; while (!found) { index--; Trip previousTrip = cTripNearestTime[index]; if (!(previousTrip.overlaps(bestTrip))) { found = true; bestJoinedTrip = previousTrip.Append(bestTrip); } } index = cTripNearestTime.Count - 2; Trip secondBestTrip = cTripNearestTime[index].Copy(); JoinedTrip secondBestJoinedTrip = new JoinedTrip(); found = false; while (!found) { index--; Trip previousTrip = cTripNearestTime[index]; if (!(previousTrip.overlaps(secondBestTrip))) { found = true; secondBestJoinedTrip = previousTrip.Append(secondBestTrip); } } List bestStartandEndTime = bestJoinedTrip.startandEndTime(startDest, endDest, aArrivalTime); List secondBestStartEndTime = secondBestJoinedTrip.startandEndTime(startDest, endDest, aArrivalTime); bestStartandEndTime.AddRange(secondBestStartEndTime); return bestStartandEndTime; } public BusScheduler.Location joinStartandEnd(Route startRoute, BusScheduler.Location startDest, BusScheduler.Location endDest) { List csDest = new List(); List ceDest = new List(); csDest.AddRange(startRoute.cLocation); ceDest.AddRange(this.cLocation); List commonDest = new List(); foreach (BusScheduler.Location str in csDest) { if (ceDest.Contains(str) && str.type=="Interchange") { commonDest.Add(str); } } int endIndex = -1; List cEndDest = this.cLocation; for (int i = cEndDest.Count - 1; endIndex < 0; i--) { if (cEndDest[i] == endDest) { endIndex = i; } } int startIndex = -1; for (int i = endIndex - 1; (startIndex < 0) & (i >= 0); i--) { if (cEndDest[i] == endDest) { endIndex = i; } if (commonDest.Contains(cEndDest[i])) { startIndex = i; } } return cEndDest[startIndex]; } public TripPattern addTripPattern(TripPattern aTripPattern) { cTripPattern.Add(aTripPattern); aTripPattern.aRoute = this; return aTripPattern; } public bool ContainsEnd(BusScheduler.Location endDest) { for (int i = 1; i < cLocation.Count; i++) { if (endDest == cLocation[i]) { return true; } } return false; } public bool ContainsStart(BusScheduler.Location startDest) { for (int i = 0; i < cLocation.Count - 1; i++) { if (startDest == cLocation[i]) { return true; } } return false; } public String getLocationIndex(BusScheduler.Location aLocation, BusScheduler.Location bLocation, BusScheduler.Time start, BusScheduler.Time end) { string path = ""; bool foundEnd = false, foundStart=false; int index = 0; List cLocation = this.cLocation; List cTrip = this.cTrip; double tempStart = 0; while (!foundEnd) { for (index = 0; index < cLocation.Count; index++) { if (aLocation == cLocation[index] && foundStart==false) { path = aLocation.latitude + "," + aLocation.longitude; foundStart = true; } else if (aLocation == cLocation[index] && foundStart == true) { tempStart= cTrip[0].c1ArrivalTime[index].total; while (start.total >= tempStart) { if (tempStart == start.total) { path = aLocation.latitude + "," + aLocation.longitude; foundStart = true; tempStart++; } else tempStart += 60; } } else if (bLocation == cLocation[index] && foundStart == true) { path = path + ":" + bLocation.latitude + "," + bLocation.longitude; foundEnd = true; return path; } else if (foundStart == true) { path = path + ":" + cLocation[index].latitude + "," + cLocation[index].longitude; } } index = 1; } return ""; } } }