using System; using System.Collections.Generic; //using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Text; using System.IO; using Subgurim.Controles; using AtomicCityBusScheduler; public partial class _Default : System.Web.UI.Page { public TransportSystem aTransportSystem = new TransportSystem(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); public List allRoutes = new List(); public Route aRoute; public TripPattern pattern1 = new TripPattern(); public TripPattern aTripPattern; protected void Page_Load(object sender, EventArgs e) { //read from the file TextReader fileReader = new StreamReader("Bus Schedule2.csv"); string title = fileReader.ReadLine(); title = fileReader.ReadLine(); string[] bits = title.Split(','); aTransportSystem.name = bits[0]; lblTitle.Text = aTransportSystem.name + " Scheduler"; string line = fileReader.ReadLine(); string line2 = fileReader.ReadLine(); string line3 = fileReader.ReadLine(); bits = line3.Split(','); while (bits[0] != "") { AtomicCityBusScheduler.Location aLocation = new AtomicCityBusScheduler.Location(); aLocation.name = bits[0].Replace("\"", ""); aLocation.address = bits[1]; aLocation.latitude = double.Parse(bits[2]); aLocation.longitude = double.Parse(bits[3]); aTransportSystem.cLocation.Add(aLocation); line3 = fileReader.ReadLine(); bits = line3.Split(','); //add for the combo box ListItem addList = new ListItem(); string location_name = aLocation.name.Replace("\"", ""); string latlong =aLocation.latitude + "," + aLocation.longitude; if (!IsPostBack) { addList.Text = location_name; addList.Value = latlong; lstStart.Items.Add(addList); lstDest.Items.Add(addList); } } line = fileReader.ReadLine(); bits = line.Split(','); while (bits[0] != "") { List table = new List(); aRoute = new Route(); aRoute.name = bits[0]; line2 = fileReader.ReadLine(); line3 = fileReader.ReadLine(); bits = line3.Split(','); while (bits[0] != "") { table.Add(bits); line3 = fileReader.ReadLine(); if (line3 == null) { bits = "".Split(','); } else { bits = line3.Split(','); } } List cLocation = new List(); List c2ArrivalTime = new List(); List c1WaitTime = new List(); string[] firstRow = table[0]; List firstList = new List(); firstList.AddRange(firstRow); int index = firstList.FindIndex( delegate(string str) { return str == ""; }); int n1TripPattern; if (index > 0) { n1TripPattern = index / 5; //5 = arrival time, wait time, repeat time, first repeat, ;last repeat } else { n1TripPattern = firstList.Count / 5; } for (int i = 0; i < n1TripPattern; i++) { aTripPattern = new TripPattern(); foreach (string[] cString in table) { if (i == 0) { string locationName = cString[0]; AtomicCityBusScheduler.Location aLocation = aTransportSystem.aLocationAt(locationName); cLocation.Add(aLocation); } if (cString[1] != "") { int offset = 5 * i; int aArrivalTime = int.Parse(cString[offset + 1]); int aWaitTime = int.Parse(cString[offset + 2]); if (cString[offset + 3] != "") { aTripPattern.repeatMinutes = int.Parse(cString[offset + 3]); aTripPattern.firstRepeatTime = ConvertTime(cString[offset + 4]); aTripPattern.lastRepeatTime = ConvertTime(cString[offset + 5]); } aTripPattern.c1ArrivalTime.Add(aArrivalTime); aTripPattern.cWaitTime.Add(aWaitTime); } } aRoute.addTripPattern(aTripPattern); } aRoute.cLocation = cLocation; allRoutes.Add(aRoute); line3 = fileReader.ReadLine(); if (line3 == null) { bits = "".Split(','); } else { bits = line3.Split(','); } } foreach (Route r in allRoutes) { r.setcTrip(); } string[] Items = new string[aTransportSystem.cLocation.Count]; for (int i = 0; i < aTransportSystem.cLocation.Count; i++) { Items[i] = aTransportSystem.cLocation[i].name; } if (!IsPostBack) { CallScript(); } } public AtomicCityBusScheduler.Location startDest, endDest; public int hour, minute; public string clockTime(int min) { string hour = (min / 60).ToString(); int minutes = (min % 60); string digits; if (minutes < 10) { digits = "0" + minutes.ToString(); } else { digits = minutes.ToString(); } string clockTime = hour + ":" + digits; return clockTime; } protected void ok_Click(object sender, EventArgs e) { if (listBoxMinute.SelectedItem == null || listBoxHour.SelectedItem == null) { lstDirection.Items.Clear(); lstDirection.Items.Add("Please set the Destinated Time of Departure."); CallScript(); return; } if (lstStart.SelectedItem.Text == lstDest.SelectedItem.Text) { lstDirection.Items.Clear(); lstDirection.Items.Add("Please select different location."); CallScript(); return; } else { bool solutionFound = false; startDest = aTransportSystem.aLocationAt(lstStart.SelectedItem.Text); endDest = aTransportSystem.aLocationAt(lstDest.SelectedItem.Text); hour = int.Parse(listBoxHour.Text); minute = int.Parse(listBoxMinute.Text); List endRoutes = new List(); foreach (Route r in allRoutes) { if (r.ContainsEnd(endDest)) { endRoutes.Add(r); } } List startRoutes = new List(); foreach (Route r in allRoutes) { if (r.ContainsStart(startDest)) { startRoutes.Add(r); } } List goodRoutes = new List(); foreach (Route r in startRoutes) { if (endRoutes.Contains(r)) { goodRoutes.Add(r); } } int aArrivalTime = int.Parse(listBoxHour.Text) * 60 + int.Parse(listBoxMinute.Text); List goodTrips = new List(); string mapInfo="",path=""; int count = TabMenu.Items.Count; //reset tab menu if (count != 1) { for (int i = count-1; i >= 1; i--) { TabMenu.Items.RemoveAt(i); } } lstDirection.Items.Clear(); foreach (Route r in goodRoutes) { List startandEndTime = r.startandEndTime(startDest, endDest, aArrivalTime); int startTime; int endTime; if (startandEndTime[1] < startandEndTime[3]) { startTime = startandEndTime[2]; endTime = startandEndTime[3]; } else { startTime = startandEndTime[0]; endTime = startandEndTime[1]; } if (startTime > 0 & endTime > (aArrivalTime - 60)) { solutionFound = true; path = "from: " + r.getLocationIndex(startDest, endDest); mapInfo = r.name + "~" + startDest.name + "~" + clockTime(startTime) + "~" + endDest.name + "~" + clockTime(endTime)+"~"+path; CreateTab(mapInfo); } } if (goodRoutes.Count == 0) { foreach (Route rs in startRoutes) { foreach (Route re in endRoutes) { try { AtomicCityBusScheduler.Location changeDest = re.joinStartandEnd(rs, startDest, endDest); List startandEndTime = re.startandEndTime(changeDest, endDest, aArrivalTime); int getOnTime = startandEndTime[0]; int arriveTime = startandEndTime[1]; startandEndTime = rs.startandEndTime(startDest, changeDest, getOnTime); int startTime = startandEndTime[0]; int getOffTime = startandEndTime[1]; if ((startTime > 0) & (arriveTime - startTime < 180) & arriveTime > (aArrivalTime - 60)) { solutionFound = true; path = "from: " + rs.getLocationIndex(startDest, changeDest) + " to: " + re.getLocationIndex(changeDest, endDest); mapInfo = rs.name + "~" + startDest.name + "~" + clockTime(startTime) + "~" + endDest.name + "~" + clockTime(arriveTime) + "~" + re.name + "~" + changeDest.name + "~" + clockTime(getOffTime) + "~" + clockTime(getOnTime)+"~"+path; CreateTab(mapInfo); } } catch { continue; } } } } if (solutionFound == false) { lstDirection.Items.Clear(); lstDirection.Items.Add("The bus doesn't run this early."); } else { TabMenu.TabIndex = 1; callDirection("1"); } } } protected void Clear_Click(object sender, EventArgs e) { //clear lstStart.SelectedIndex = 0; lstDest.SelectedIndex = 0; listBoxMinute.SelectedIndex = -1; listBoxHour.SelectedIndex = -1; int count = TabMenu.Items.Count; //reset tab menu if (count != 1) { for (int i = count-1; i >= 1; i--) { TabMenu.Items.RemoveAt(i); } } lstDirection.Items.Clear(); CallScript(); } public void CallScript(){ AtomicCityBusScheduler.Location StartLocation = aTransportSystem.aLocationAt(lstStart.SelectedItem.Text); AtomicCityBusScheduler.Location DestLocation = aTransportSystem.aLocationAt(lstDest.SelectedItem.Text); String script = ""; script = script + "DrawMap('lstStart',0);DrawMap('lstDest',1);"; Page.RegisterStartupScript("myscript", ""); } public int ConvertTime(string time) { string[] splitTime = time.Split(':'); int ConvertedTime = int.Parse(splitTime[0].Trim()) * 60 + int.Parse(splitTime[1].Trim()); return ConvertedTime; } int tabNum = 0; string tabName = ""; public void CreateTab(string mapInfo) { tabNum += 1; //add tab to view tabName = "Route" + tabNum; MenuItem newRouteTab = new MenuItem(); newRouteTab.Text = tabName; newRouteTab.Value = mapInfo; TabMenu.Items.Add(newRouteTab); } protected void TabMenu_MenuItemClick(object sender, MenuEventArgs e) { lstDirection.Items.Clear(); if (TabMenu.SelectedItem.Value == "0") { CallScript(); } else { callDirection(TabMenu.SelectedItem.Value); } } public void callDirection(String index) { string script = "", txtStart = "", txtEnd = "", txtTransOff = "", txtTransOn="",txtTrans=""; String[] info ; if (index == "1") { String mapInfo = TabMenu.Items[int.Parse(index)].Value; info = mapInfo.Split('~'); } else { info = index.Split('~'); } //0: r.name //1: startDest.name //2: clockTime(startTime) //3: endDest.name //4: clockTime(endTime) //5: re.name || for length=6: path //6: changeDest.name //7: clockTime(getOffTime) //8: clockTime(getOnTime) //9: path if (info.Length == 6) { txtStart = "From: \"" + info[1] + "\" Departure Time: " + info[2] + " Route: " + info[0]; txtEnd = "To: \"" + info[3] + "\" Arrival Time: " + info[4]; lstDirection.Items.Add(txtStart); lstDirection.Items.Add(txtEnd); //draw map txtStart = "From: " + info[1] + "
Time: " + info[2] + "
Route: " + info[0]; txtEnd = "To: " + info[3] + "
Time: " + info[4] + "
Route: " + info[0]; script = "DrawMap('lstStart',0);DrawMap('lstDest',0);Draw2Destination( \"" + txtStart + "\" , \"" + txtEnd + "\",\""+ info[5]+"\");"; } else if (info.Length == 10) { txtStart = "From: \"" + info[1] + "\" Departure Time: " + info[2] + " Route: " + info[0]; txtTransOff = "Get Off at: \"" + info[6] + "\" Arrival Time: " + info[7]; txtTransOn = "Get On from: \"" + info[6] + "\" Departure Time: " + info[8] + " Route: " + info[5]; txtEnd = "To: \"" + info[3] + "\" Arrival Time: " + info[4]; lstDirection.Items.Add(txtStart); lstDirection.Items.Add(txtTransOff); lstDirection.Items.Add(txtTransOn); lstDirection.Items.Add(txtEnd); txtStart = "From: " + info[1] + "
Departure Time: " + info[2] + "
Route: " + info[0]; txtTrans = "Get Off at: " + info[6] + "
Arrival Time: " + info[7] + "
Departure Time: " + info[8] + "
Route: " + info[5]; txtEnd = "To: " + info[3] + "
Arrival Time: " + info[4]; AtomicCityBusScheduler.Location TransLocation = aTransportSystem.aLocationAt(info[6]); string transLatlng = "new google.maps.LatLng(" + TransLocation.latitude + " , " + TransLocation.longitude + " )"; //draw map script = "DrawMap('lstStart',0);DrawMap('lstDest',0);Draw3Destination( \"" + txtStart + "\" , \"" + txtTrans + "\" , \"" + txtEnd + "\" , " + transLatlng + " ,\"" + info[9] + "\");"; } Page.RegisterStartupScript("myscript", ""); } }