A simulation of trains running on a railway track consisting of different sections and stations.
-
Train (Train.java)
TheTrainclass represents a train with a name and a position on the railway track. A train can move to the next element of the track. The simulation is managed in a thread using theRunnableinterface. -
Element (Element.java)
TheElementclass is an abstract class that represents different types of elements on the railway track, such as stations and sections. It handles the logic for moving trains between elements. -
Section (Section.java)
TheSectionclass, a concrete subclass ofElement, represents a section of the railway track. It keeps track of whether the section is occupied by a train and how many trains are present. -
Station (Station.java)
TheStationclass, a concrete subclass ofElement, represents a station on the railway track. It has a maximum capacity for the number of trains it can accommodate and manages the entry and exit of trains. -
Position (Position.java)
ThePositionclass represents the position of a train on the railway track, characterized by an element and a direction (left to right or right to left). It allows the train to move to the next element. -
Direction (Direction.java)
TheDirectionenumeration represents the direction of a train, either left to right (LR) or right to left (RL). -
Railway (Railway.java)
TheRailwayclass represents the complete railway circuit, consisting of elements like stations and sections. It also tracks the number of trains in each direction. -
BadPositionForTrainException (BadPositionForTrainException.java)
This class is a custom exception that is thrown when a train is improperly positioned on the railway track.
To run the simulation, you can either:
- Run the
Mainclass and observe the movement of the trains on the console. - Run the
TrainSimulationAppclass to see the movement through a graphical user interface.
You can manipulate the elements of the track, such as the number of trains, stations, or sections, by modifying the Main or TrainSimulationApp classes to test the behavior of trains in various scenarios.
- Java Development Kit (JDK) 8 or later.
- A development environment such as Eclipse, IntelliJ IDEA, or any IDE that supports Java.
