// Add an outbound track from this station public void AddTrack(Track track) { outboundTracks.Add(track); }
// constructor - takes a filename with lines of the following format // xyn, where x and y are single char names of train stations, and n is the distance between them // Note that there is no error checking in reading the file public TrainSystem(string strFileName) { // Instantiate an empty stations collection mStations = new Stations(); // Instantiate an empty tracks collection mTracks = new Tracks(); // Open the file for reading as a stream StreamReader fsIn = File.OpenText(strFileName); // local variable to hold the string representing each track string strTrack; // local variables to hold the source and target station objects Station sourceStation, targetStation; // Loop through each string in the input file, setting str to each string while ((strTrack = fsIn.ReadLine()) != null) { // If the source station doesn't already exist in the station collection, add it // Also - set a local variable to represent the source station if (!mStations.Exists(strTrack[0])) { Station newStation = new Station(strTrack[0]); mStations.Add(newStation); sourceStation = newStation; } // else, the source station exists - simply retrieve it from the collection else { sourceStation = mStations.GetStation(strTrack[0]); } // If the target station doesn't already exist in the station collection, add it // Also - set a local variable to represent the target station if (!mStations.Exists(strTrack[1])) { Station newStation = new Station(strTrack[1]); mStations.Add(newStation); targetStation = newStation; } // else, the target station exists - simply retrieve it from the collection else { targetStation = mStations.GetStation(strTrack[1]); } // Create a new track object to represent the track // Get the distance by converting the 1-character representation of the distance to a value int distance = Convert.ToByte(strTrack[2]) - Convert.ToByte('0'); Track newTrack = new Track(sourceStation, targetStation, distance); // Add the track to the track collection mTracks.Add(newTrack); // Add the track to the outbound track collection for the source station sourceStation.AddTrack(newTrack); } // Close the file fsIn.Close(); }