diff --git a/README.md b/README.md index 4ed4604..9b45e4f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ # RobotFactory ## Style Guide + * For every method you write you also write the JUnit test and comments. - * [reference](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html) * Always mark the task you're currently working on the kanban board and move it to the right phase. +## References + +* [JavaDocs](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html) +* [Markdown](https://www.markdownguide.org/extended-syntax/) +* [JUnit](https://junit.org/junit5/docs/current/user-guide/) + ## Contributors * Luc Dreibholz (Matr.Nr.: 2210544) diff --git a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java index 902da3b..f398904 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java @@ -3,22 +3,33 @@ package de.hsmannheim.informatik.name.domain; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; -import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot; +import de.hsmannheim.informatik.name.domain.requirements.Robot; public abstract class RobotBluePrint implements Robot { protected final int ID; + + protected final int MAGIC_NUMBER = 42; private final String name; + private boolean isPowered; public RobotBluePrint(int id, String name) throws RobotIllegalStateException { - if(!this.isValidID(id)) { + if (!this.isValidID(id)) { //TODO write own Exception throw new RobotIllegalStateException(); } this.ID = id; this.name = name; + this.isPowered = false; } + /** + * Checks if an {@code int} represents a valid {@code ID}, + * concerning a specific model within its own criteria + * + * @param id {@code ID} to check + * @return {@code true} if the ID is valid, else {@code false} + */ protected abstract boolean isValidID(int id); @Override @@ -33,12 +44,12 @@ public abstract class RobotBluePrint implements Robot { @Override public void triggerPowerSwitch() { - + this.isPowered = !this.isPowered; } @Override public boolean isPowerOn() { - return false; + return this.isPowered; } @Override @@ -47,12 +58,8 @@ public abstract class RobotBluePrint implements Robot { } @Override - public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { - return null; - } + public abstract String speak(int[] zahlen) throws RobotException, RobotMagicValueException; @Override - public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException { - return new int[0]; - } + public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; } diff --git a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/Robot.java b/src/de/hsmannheim/informatik/name/domain/requirements/Robot.java similarity index 74% rename from src/de/hsmannheim/informatik/name/domain/robot_interfaces/Robot.java rename to src/de/hsmannheim/informatik/name/domain/requirements/Robot.java index 95827d5..fb1de90 100644 --- a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/Robot.java +++ b/src/de/hsmannheim/informatik/name/domain/requirements/Robot.java @@ -1,5 +1,5 @@ /* (c) 2012 Thomas Smits */ -package de.hsmannheim.informatik.name.domain.robot_interfaces; +package de.hsmannheim.informatik.name.domain.requirements; /** * Interface für Roboter. diff --git a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotControl.java b/src/de/hsmannheim/informatik/name/domain/requirements/RobotControl.java similarity index 97% rename from src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotControl.java rename to src/de/hsmannheim/informatik/name/domain/requirements/RobotControl.java index cfd6b36..4b425f4 100644 --- a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotControl.java +++ b/src/de/hsmannheim/informatik/name/domain/requirements/RobotControl.java @@ -1,4 +1,4 @@ -package de.hsmannheim.informatik.name.domain.robot_interfaces; +package de.hsmannheim.informatik.name.domain.requirements; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; diff --git a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java b/src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java similarity index 97% rename from src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java rename to src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java index dce1a18..4a2dd66 100644 --- a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java +++ b/src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java @@ -1,4 +1,4 @@ -package de.hsmannheim.informatik.name.domain.robot_interfaces; +package de.hsmannheim.informatik.name.domain.requirements; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java index 3e50bea..7ad894b 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java @@ -4,7 +4,9 @@ import de.hsmannheim.informatik.name.domain.RobotBluePrint; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; -import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot; + +import java.util.Arrays; +import java.util.stream.Collectors; public abstract class StarWarsRobot extends RobotBluePrint { @@ -16,16 +18,6 @@ public abstract class StarWarsRobot extends RobotBluePrint { SEPARATOR = separator; } - @Override - public void triggerPowerSwitch() { - - } - - @Override - public boolean isPowerOn() { - return false; - } - @Override public RobotException getLastException() { return null; @@ -33,29 +25,41 @@ public abstract class StarWarsRobot extends RobotBluePrint { @Override public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { - StringBuilder sb = new StringBuilder(); - //Loops through every element of zahlen[] - for (int i = 0; i < zahlen.length; i++) { - if (zahlen[i] == 42) { - throw new RobotMagicValueException(); - } - sb.append(zahlen[i]); - //if current number is not the last number add a separator - if (i != zahlen.length - 1) { - sb.append(SEPARATOR).append(" "); - } + if (Arrays.stream(zahlen).anyMatch(i -> i == MAGIC_NUMBER)) { + throw new RobotMagicValueException(); } - return sb.toString(); + return this.buildString(zahlen); } - public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; + /** + * Creates a String representation of the given {@code payload}, + * with separated values, by the delimiter {@code SEPARATOR}. + * + * @param payload {@code int[]} of elements + * @return {@link String representation} of the {@code payload} + */ + private String buildString(int[] payload) { + return Arrays.stream(payload).mapToObj(String::valueOf).collect(Collectors.joining(SEPARATOR + " ")); + } + /** + * Helper-Method to swap Elements of a given {@code array}. + * + * @param arr {@code array} to swap Elements + * @param a Index of the first Element + * @param b Index of the second Element + */ protected static void swap(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } + /** + * Helper-Method to reverse an {@code array}. + * + * @param array {@code array} to reverse + */ protected static void reverse(int[] array) { for (int i = 0; i < array.length / 2; i++) { int temp = array[i]; diff --git a/tests/R2D2SpeakTest.java b/tests/R2D2SpeakTest.java index 3660289..3c894a0 100644 --- a/tests/R2D2SpeakTest.java +++ b/tests/R2D2SpeakTest.java @@ -41,6 +41,6 @@ class R2D2SpeakTest { @Test void speakTestMagicNumberException() throws RobotException { - assertThrows(RobotMagicValueException.class,() -> r2d2.speak(new int[]{42})); + assertThrows(RobotMagicValueException.class, () -> r2d2.speak(new int[]{42})); } } \ No newline at end of file