Skip to content
Snippets Groups Projects
Commit 9d8e66a8 authored by CELERIER Auguste's avatar CELERIER Auguste
Browse files

nextElement

parent c9ff76f5
No related branches found
No related tags found
No related merge requests found
...@@ -26,11 +26,29 @@ public class Railway { ...@@ -26,11 +26,29 @@ public class Railway {
boolean first = true; boolean first = true;
for (Element e : this.elements) { for (Element e : this.elements) {
if (first) if (first)
first = false; first = false;
else else
result.append("--"); result.append("--");
result.append(e); result.append(e);
} }
return result.toString(); 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.");
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment