Each station holds an array of passengers. |
Issue here is that the Passenger class must be able to hold a list of stations, so that a passenger object can pathfind between them. But the Station class must be able to hold an array of passengers. I can't compile either, because each is calling the other.
|
Definitely a design problem, I think. The problem is more complex - can't be solved with 2 classes IMO. Both the relationships you mentioned are not correct.
A completely different approach is needed IMO.
These sort of problem are usually solved with a graph of the train network. Graphs have nodes (the stations), they also have edges which connect 2 stations.
The Nodes store the name of the station, and which stations are connected to it.
The Edges store info like the two stations which define which node it is, and info such as cost, distance, time etc.
So now we need a data structure to store all this info. No matter how simple or complex the network is, you need a Graph object. Whether you call it a graph or a network is up to you
The simplest situation is a network with just one line - so you could use a linked list for this.
Next there is a main line, with other lines branching off. I am not sure whether having a tree to model the network is the best for this, or whether there is a better solution. You might be able to have a <set> (sets are sorted )of Edges. The stations could be numbered so it is easy to tell which stations are further away from the root (Central Station), or store the distance form the root as a member variable of a Station node.
The most complex model of a network is one that involves interconnected circuits.
To solve a route calculation, requires matrix calculations (so far a I remember - it was bleems ago when we looked at this very briefly at Uni)
Anyway the Route calculation should be a part of the Graph class, although the Route returned could be stored in the Passenger object.
I don't really know how to do this in detail, but hopefully I have given you some ideas to work with.
Hop all goes well.