I am currently working on a train simulation project for uni.
This is my class hierarchy:
RollingStock
Coach
FreightCoach
PassengerCoach
SpecialCoach
Engine
DieselEngine
ElectricEngine
SteamEngine
Trainset
My questions:
- Every coach has a unique ID. However, Engines and Trainsets share their ID-Space ("series-name"). "Name" is inherited by RollingStock and both Trainset and Engine have the attribute "series".
I've created a class "SharedIdSpace" to implement this feature. But I am not quite sure how to solve this nicely (TreeMap, ..., ?).
Now, my main problem is I have to implement the following feature:
"Rolling stock can be composed into a train. The following restrictions must be observed when composing:
- There must always be at least one locomotive/train set at the beginning or end of a valid train.
- When composing, it must always be considered whether the rolling stock has a suitable coupling at the desired composition point.
- The rolling stock that is being composed has not yet been used in another train. [...]"
How can I implement this? I'm afraid I have no useful idea.
Im not sure about what are you asking, but I will try to give you my aproximation:
You need to create a composite object where every object has an unique ID and need to pass some validations.
I would implement an "StoregeManager" a "ComposerManager" and add an abstract validator method in "RollingStock" that validate if the vagon can be added.
The flow would be something like this:
DISCLAIMER This is written in Java, but with notepad++, please dont check the sintaxis.
And inside the componer, something like this:
You can put the storage inside the Composer and pass as argument the classname of the vagon if you need another aproximation to your problem.
I hope this helps you.