0

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.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

1 Answers1

0

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.


RollingStock freightCoach = StoregeManager.getFreightCoach();
RollingStock specialCoach = StoregeManager.getSpecialCoach();
RollingStock dieselEngine = StoregeManager.getDieselEngine();

// Check if they are null or throw an exception if has no more elements. Maybe from BBDD or from where you want

Composer.compone()
.add(dieselEngine)
.add(freightCoach)
.add(specialCoach)
.build()

And inside the componer, something like this:


public class Composer {

    private StoregeManager storeManager; //Injected or initialized, as you want.
    private static Train train;

    public Composer build(){
        train = new Train;
        return this;
    }

    public Composer add(RollingStock rs) {
        if(rs.isValid(train))
            train.add(rs);
        return this;
    }

    public RollingStock[] build() {
        storageManager.ckeckTrain(train);
        return train;
    }
}

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.

Balbu
  • 56
  • 5
  • I appreciate your help! However, I don't quite understand how the StorageManager should be implemented. Could you elaborate on that please. –  Feb 11 '20 at 12:34
  • What should the method compone() do? I implemented your idea with an ArrayList so far. –  Feb 11 '20 at 12:35
  • 1
    As you said: ``` The rolling stock that is being composed has not yet been used in another train. ``` The storage manager check if has elements that are not being used in another train. ¿How? You can have all the elements loaded in memory or in a database with a column to mark if is used or not. You make a petition to the StorageManager and it checks if is possible to return an element of the type that you want. If not, return null or throw an exception. If it can return, mark the element as used or drop it from the memory – Balbu Feb 11 '20 at 12:39
  • OK. Do you also have an idea for the shared ID-space (engine and trainsets). –  Feb 11 '20 at 12:42
  • Correct me if im wrong. ¿Is not the ID-space only a composed ID? – Balbu Feb 11 '20 at 12:56
  • Every Engine (SteamEngine, DieselEngine,...) and Trainset (Multiple unit) have an ID. This ID-space is shared among them. The ID consists of a "series" and a "name" (-). Name is inherited by RollingStock (every rolling stock has a name, e.g. "Emma") and both Engine and Trainset have an attribute series (String). Now, I have to make sure that there is ID twice and that both classes share the same ID-space. –  Feb 11 '20 at 13:02
  • Trains consist of multiple wagons (e. g. SteamEngine + Multiple Unit) and have another unique ID. –  Feb 11 '20 at 13:02
  • Maybe, instead of use RollingStock[] as a train, you need a Train class that generate that ID depending of the elements of the train and, in the build() method of the Composer, call the StorageManager to check if it already exist or not and register it. – Balbu Feb 11 '20 at 13:09