public class Switch extends Slot
Switch
represents a railway switch.
A Switch
has a single fixed neighboring Slot
, and a list
of Slot
s the User can choose from while the Switch
isn't occupied.Constructor and Description |
---|
Switch(int numberOfOptions)
Creates a
Switch with all options and its fixSlot set
as the single instance of Ground . |
Modifier and Type | Method and Description |
---|---|
Slot |
getNextSlot(Slot previousSlot)
Returns the
Slot a Vehicle standing on this Switch must move to after its
next step determined by previousSlot . |
int |
getNumberOfOptions()
Returns the number of selectable
options connected to this Rail . |
int |
getSelectedOption() |
void |
setFixSlot(Slot fixSlot)
Sets the
fixSlot of this Switch to the given Slot . |
void |
setOption(int index,
Slot newOption)
Adds a new
Slot to the selectable list of options . |
void |
switchTo(int selectedOption)
Selects the
Slot that is the Nth element of options ,
where N=selectedOption +1 to be the active optional Slot of this Slot . |
void |
switchToNext() |
acquire, addStation, arrive, checkCollision, depart, getStation, getVehicle, isAcquired, release
public Switch(int numberOfOptions)
Switch
with all options
and its fixSlot
set
as the single instance of Ground
. The number of its options
is determined
by the parameter numberOfOptions
.numberOfOptions
- the number of options.public Slot getNextSlot(Slot previousSlot)
Slot
a Vehicle
standing on this Switch
must move to after its
next step determined by previousSlot
.
If the Vehicle
came from the fixSlot
it moves to the Slot
that is the
Nth element of options
where N=selectedOption+1, otherwise it moves onto fixSlot
.getNextSlot
in class Slot
previousSlot
- Slot
that determines the direction of movementSlot
to move topublic int getNumberOfOptions()
options
connected to this Rail
.options
arraypublic void setFixSlot(Slot fixSlot)
fixSlot
of this Switch
to the given Slot
.fixSlot
- the Slot
to be set as this Switch
's fixSlot
public void setOption(int index, Slot newOption)
Slot
to the selectable list of options
.
If the Slot
is already present in the list, it's not added.index
- which option is alterednewOption
- the Slot
to be addedjava.lang.IllegalArgumentException
- if N is not in the specified domain, that is, there is no
optional Slot
identified by index
in options
public void switchTo(int selectedOption) throws java.lang.UnsupportedOperationException, java.lang.IllegalArgumentException
Slot
that is the Nth element of options
,
where N=selectedOption
+1 to be the active optional Slot
of this Slot
.
1<=N1<=(size of options
).selectedOption
- the number of the optional Slot
to be selectedjava.lang.UnsupportedOperationException
- if this Switch
is currently occupiedjava.lang.IllegalArgumentException
- if N is not in the specified domain, that is, there is no
optional Slot
identified by selectedOption
in options
public int getSelectedOption()
public void switchToNext()