diff --git a/src/train/Railway.java b/src/train/Railway.java index 1b96c5824887f1db32bd94ebaf1571a2bcedc14f..2332edef2d2c291d892fb3889411c79753066e55 100644 --- a/src/train/Railway.java +++ b/src/train/Railway.java @@ -26,11 +26,29 @@ public class Railway { boolean first = true; for (Element e : this.elements) { if (first) - first = false; + first = false; else result.append("--"); result.append(e); } return result.toString(); } + + public Element nextElement(Element currentElement, Direction direction) { + if (currentElement == null || direction == null) { + throw new NullPointerException("Current element or direction cannot be null."); + } + + for (int i = 0; i < elements.length; i++) { + if (elements[i] == currentElement) { + if (direction == Direction.LR && i < elements.length - 1) { + return elements[i + 1]; + } else if (direction == Direction.RL && i > 0) { + return elements[i - 1]; + } + } + } + + throw new IllegalArgumentException("Current element is not part of the railway or no next element exists in the given direction."); + } }