Class RailCar

java.lang.Object
com.amalgamasimulation.trains.RailCar

public class RailCar extends Object
Represents a single railcar that is part of a Train.

A RailCar has a fixed physical length and may be attached to a train at a specific position defined by its sequence number and offset along the train's overall length. The class also provides utility methods for computing its position within the train and for obtaining its animation segment.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
     
    protected double
     
    protected int
     
    protected Train
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    RailCar(double length)
    Creates a new RailCar with the specified length.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the animation Segment corresponding to this railcar’s position along the train’s current animation polyline.
    double
    Returns the starting offset of this railcar along the train’s total length.
    double
    Returns the ending offset of this railcar along the train’s total length.
    double
    Returns the physical length of this railcar.
    int
    Returns the sequence number of this railcar within the train.
    Returns the Train this railcar is assigned to.
    void
    setTrain(Train train, int sequenceNumber, double beginOffset)
    Assigns this railcar to a Train and defines its position within the train.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • length

      protected double length
    • train

      protected Train train
    • sequenceNumber

      protected int sequenceNumber
    • beginOffset

      protected double beginOffset
  • Constructor Details

    • RailCar

      public RailCar(double length)
      Creates a new RailCar with the specified length.
      Parameters:
      length - the physical length of the railcar
  • Method Details

    • setTrain

      public void setTrain(Train train, int sequenceNumber, double beginOffset)
      Assigns this railcar to a Train and defines its position within the train.
      Parameters:
      train - the train this railcar belongs to
      sequenceNumber - the railcar's index (position) within the train
      beginOffset - the starting offset of the railcar along the train’s total length
    • getLength

      public double getLength()
      Returns the physical length of this railcar.
      Returns:
      the length of the railcar
    • getTrain

      public Train getTrain()
      Returns the Train this railcar is assigned to.
      Returns:
      the train this railcar belongs to, or null if it is not assigned
    • getSequenceNumber

      public int getSequenceNumber()
      Returns the sequence number of this railcar within the train.
      Returns:
      the railcar’s sequence number, or -1 if not assigned
    • getBeginOffset

      public double getBeginOffset()
      Returns the starting offset of this railcar along the train’s total length.
      Returns:
      the starting offset of the railcar
    • getEndOffset

      public double getEndOffset()
      Returns the ending offset of this railcar along the train’s total length.
      Returns:
      the end offset of the railcar, computed as beginOffset + length
    • getAnimationSegment

      public Segment getAnimationSegment()
      Returns the animation Segment corresponding to this railcar’s position along the train’s current animation polyline.

      The returned segment represents the spatial location of the railcar based on the animation path of the train. If the train or its animation polyline is not available, a segment with coordinates set to Double.POSITIVE_INFINITY is returned.

      Returns:
      the animation segment representing the railcar’s position, or a dummy infinite segment if unavailable