From 9d8e66a820c1a9ddb4a8f0810d4d8237b4cc7f8a Mon Sep 17 00:00:00 2001 From: Auguste <auguste.celerier@imt-atlantique.net> Date: Tue, 21 Jan 2025 10:02:11 +0100 Subject: [PATCH] nextElement --- src/train/Railway.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/train/Railway.java b/src/train/Railway.java index 1b96c58..2332ede 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."); + } } -- GitLab