As a prerequisite, here is the code for my two classes, Station and SubwaySystem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment7
{
class Station
{
//attributes
private string stName;
private int stID;
private Station next;
private Station previous;
//properties
public string StName { get { return this.stName; } }
public int StID { get { return this.stID; } }
public Station Next { get; set; }
public Station Previous { get; set; }
//constructor
public Station(string name, int ID)
{
this.stName = name;
this.stID = ID;
}
//ToString override
public override string ToString()
{
return "Name: " + stName + ", ID number: " + stID;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment7
{
class SubwaySystem
{
//attributes
Dictionary<int, Station> stationDictionary = new Dictionary<int, Station>();
//methods
/**
* Inserts a new station ahead of one with the given ID
**/
void InsertAhead(int id, Station insert)
{
if (stationDictionary.Count == 0)
{
stationDictionary.Add(insert.StID, insert);
insert.Next = insert;
insert.Previous = insert;
}
else
{
if (stationDictionary.ContainsKey(id) == false)
Console.WriteLine("There is no element at the given ID.");
else if (stationDictionary.ContainsKey(insert.StID))
Console.WriteLine("The ID of the station being added already exists.");
else
{
stationDictionary.Add(insert.StID, insert);
insert.Next = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value.Next;
insert.Previous = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value;
stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value.Previous = insert;
insert.Previous.Next = insert;
}
}
}//End InsertAhead
/**
* Inserts a new station before one with the given ID
**/
void InsertBehind(int id, Station insert)
{
if (stationDictionary.Count == 0)
{
stationDictionary.Add(insert.StID, insert);
insert.Next = insert;
insert.Previous = insert;
}
else
{
if (stationDictionary.ContainsKey(id) == false)
Console.WriteLine("There is no element at the given ID.");
else if (stationDictionary.ContainsKey(insert.StID))
Console.WriteLine("The ID of the station being added already exists.");
else
{
stationDictionary.Add(insert.StID, insert);
insert.Next = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value;
insert.Previous = stationDictionary.ElementAt<KeyValuePair<int, Station>>(id).Value.Previous;
insert.Next.Previous = insert;
insert.Previous.Next = insert;
}
}
}
/**
* Removes station with specified ID
**/
void RemoveStation(int id)
{
if (stationDictionary.ContainsKey(id) == false)
Console.WriteLine("There is no element at the given ID.");
else
{
stationDictionary.Remove(id);
}
}
/**
* Starts at the station with the specified id, following the references forward around the subway line,
* writing the station name and id until returning to the starting station.
**/
void TraverseSubwayLine(int startId)
{
//Make sure dictionary has some elements
if (stationDictionary.Count == 0)
Console.WriteLine("There are no stations in the dictionary.");
//Make sure a station at the given id exists
else if (stationDictionary.ContainsKey(startId) == false)
Console.WriteLine("There is no element at the given ID.");
else
{
//Write out the first station
Station currentStation = stationDictionary.ElementAt<KeyValuePair<int, Station>>(startId).Value;
Console.WriteLine(currentStation.ToString());
currentStation = currentStation.Next;
//Check for next station pretest, then loop until all are written
while (stationDictionary.ElementAt<KeyValuePair<int, Station>>(startId).Key != currentStation.StID)
{
Console.WriteLine(currentStation.ToString());
currentStation = currentStation.Next;
}
}
}
static void Main(string[] args)
{
SubwaySystem subSystem = new SubwaySystem();
//1. Insert a new station with ID 1 and name Boardwalk.
subSystem.stationDictionary.Add(1, new Station("Boardwalk", 1));
//2. Insert a new station with ID 6 and name Park Place ahead of the station with ID 1.
subSystem.InsertAhead(1, new Station("Boardwalk", 6));
//3. Insert a new station with ID 3 and name New York Ave. behind the station with ID 1.
subSystem.InsertBehind(1, new Station("New York Ave.", 3));
//4. Insert a new station with ID 1 and name B&O Railroad ahead of the station with ID 20.
subSystem.InsertAhead(20, new Station("B&O Railroad", 1));
//5. Insert a new station with ID 7 and name Baltic Ave. ahead of the station with ID 3.
subSystem.InsertAhead(3, new Station("Baltic Ave.", 7));
//6. Insert a new station with ID 2 and name Marvin Gardens behind the station with ID 1.
subSystem.InsertBehind(1, new Station("Marvin Gardens", 2));
//7. Insert a new station with ID 4 and name Illinois Ave. behind the station with ID 7.
subSystem.InsertAhead(7, new Station("Illinois Ave.", 4));
//8. Insert a new station with ID 6 and name Mediterranean Ave. behind the station with ID 3.
subSystem.InsertBehind(3, new Station("Mediterranean Ave.", 6));
//9. Record the list of subway stations in the order they lie on the subway line.
subSystem.TraverseSubwayLine(1);
//10. Remove the station with ID 15.
subSystem.RemoveStation(15);
//11. Remove the station with ID 3.
subSystem.RemoveStation(3);
//12. Record the list of subway stations in the order they lie on the subway line.
subSystem.TraverseSubwayLine(1);
}
}
}
Now at line 34 of the SubwaySystem class I get an out of range exception, and I have no idea why. Any help?