From 5df396e69af71a1e7c347be303c3085973e3138c Mon Sep 17 00:00:00 2001 From: zlohbierdcul <82916072+zlohbierdcul@users.noreply.github.com> Date: Wed, 21 Dec 2022 13:55:39 +0100 Subject: [PATCH] added ID, name, tests and more --- .../name/domain/RobotBluePrint.java | 58 +++++++++++++++++++ .../robot_interfaces/RobotInstructions.java | 4 +- .../informatik/name/domain/starwars/C3PO.java | 11 +++- .../informatik/name/domain/starwars/R2D2.java | 18 ++++-- .../name/domain/starwars/StarWarsRobot.java | 25 ++++---- tests/R2D2SpeakTest.java | 24 +++++--- tests/R2D2ThinkTest.java | 42 +++++++++++++- 7 files changed, 152 insertions(+), 30 deletions(-) create mode 100644 src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java diff --git a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java new file mode 100644 index 0000000..902da3b --- /dev/null +++ b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java @@ -0,0 +1,58 @@ +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; + +public abstract class RobotBluePrint implements Robot { + + protected final int ID; + private final String name; + + public RobotBluePrint(int id, String name) throws RobotIllegalStateException { + if(!this.isValidID(id)) { + //TODO write own Exception + throw new RobotIllegalStateException(); + } + this.ID = id; + this.name = name; + } + + protected abstract boolean isValidID(int id); + + @Override + public int getId() { + return this.ID; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public void triggerPowerSwitch() { + + } + + @Override + public boolean isPowerOn() { + return false; + } + + @Override + public RobotException getLastException() { + return null; + } + + @Override + public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { + return null; + } + + @Override + public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException { + return new int[0]; + } +} diff --git a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java b/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java index 38a3529..dce1a18 100644 --- a/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java +++ b/src/de/hsmannheim/informatik/name/domain/robot_interfaces/RobotInstructions.java @@ -34,7 +34,7 @@ public interface RobotInstructions { * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * oder das Array nicht seinen Vorstellungen entspricht. */ - public String speak(int[] zahlen) throws RobotException; + public String speak(int[] zahlen) throws RobotException, RobotMagicValueException; /** * Sortiert ein Array von Zahlen. Die Reihenfolge hängt von dem Typ des @@ -45,5 +45,5 @@ public interface RobotInstructions { * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * oder das Array nicht seinen Vorstellungen entspricht. */ - public int[] think(int[] zahlen) throws RobotException; + public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java index 2889848..2679744 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java @@ -1,10 +1,16 @@ package de.hsmannheim.informatik.name.domain.starwars; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; public class C3PO extends StarWarsRobot { - public C3PO() { - super(';'); + public C3PO(int id) throws RobotIllegalStateException { + super(';', id, "C3PO"); + } + + @Override + protected boolean isValidID(int id) { + return 10000 <= id && id <= 19999; } @Override @@ -17,6 +23,7 @@ public class C3PO extends StarWarsRobot { } } } + reverse(arrToSort); return arrToSort; } } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java index ac84cd6..8b3e6e9 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java @@ -1,16 +1,26 @@ package de.hsmannheim.informatik.name.domain.starwars; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; public class R2D2 extends StarWarsRobot { - public R2D2() { - super(','); + public R2D2(int id) throws RobotIllegalStateException { + super(',', id, "R2D2"); } @Override - public int[] think(int[] zahlen) throws RobotException { + protected boolean isValidID(int id) { + return 0 <= id && id <= 9999; + } + + @Override + public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException { int[] arrayToSort = zahlen.clone(); for (int i = 0; i < arrayToSort.length; i++) { + if (arrayToSort[i] == 42) { + throw new RobotMagicValueException(); + } int smallest = Integer.MAX_VALUE; int smallestIndex = 0; for (int j = i; j < arrayToSort.length; j++) { @@ -21,7 +31,7 @@ public class R2D2 extends StarWarsRobot { } swap(arrayToSort, smallestIndex, i); } - reverse(arrayToSort); + return arrayToSort; } } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java index bedb7c3..3e50bea 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java @@ -1,27 +1,21 @@ package de.hsmannheim.informatik.name.domain.starwars; +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; -public abstract class StarWarsRobot implements Robot { +public abstract class StarWarsRobot extends RobotBluePrint { private final char SEPARATOR; - protected StarWarsRobot(char separator) { + protected StarWarsRobot(char separator, int id, String name) throws RobotIllegalStateException { + super(id, name); SEPARATOR = separator; } - @Override - public int getId() { - return 0; - } - - @Override - public String getName() { - return null; - } - @Override public void triggerPowerSwitch() { @@ -38,10 +32,13 @@ public abstract class StarWarsRobot implements Robot { } @Override - public String speak(int[] zahlen) throws RobotException { + 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) { @@ -51,7 +48,7 @@ public abstract class StarWarsRobot implements Robot { return sb.toString(); } - public abstract int[] think(int[] zahlen) throws RobotException; + public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; protected static void swap(int[] arr, int a, int b) { int temp = arr[a]; diff --git a/tests/R2D2SpeakTest.java b/tests/R2D2SpeakTest.java index f7a2586..3660289 100644 --- a/tests/R2D2SpeakTest.java +++ b/tests/R2D2SpeakTest.java @@ -1,36 +1,46 @@ 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.starwars.R2D2; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; class R2D2SpeakTest { - private final R2D2 r2d2 = new R2D2(); + private final R2D2 r2d2 = new R2D2(9999); + + R2D2SpeakTest() throws RobotIllegalStateException { + } @Test - void speakTestStandard() throws RobotException { + void speakTestStandard() throws RobotException, RobotMagicValueException { assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); } @Test - void speakTestEmpty() throws RobotException { + void speakTestEmpty() throws RobotException, RobotMagicValueException { assertEquals("", r2d2.speak(new int[]{})); } @Test - void speakTestZeros() throws RobotException { + void speakTestZeros() throws RobotException, RobotMagicValueException { assertEquals("0, 0, 0, 0", r2d2.speak(new int[4])); } @Test - void speakTestOneElement() throws RobotException { + void speakTestOneElement() throws RobotException, RobotMagicValueException { assertEquals("1", r2d2.speak(new int[]{1})); } @Test - void speakTestUnitElements() throws RobotException { + void speakTestUnitElements() throws RobotException, RobotMagicValueException { assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1})); } + + @Test + void speakTestMagicNumberException() throws RobotException { + assertThrows(RobotMagicValueException.class,() -> r2d2.speak(new int[]{42})); + } } \ No newline at end of file diff --git a/tests/R2D2ThinkTest.java b/tests/R2D2ThinkTest.java index bc99362..3e63e17 100644 --- a/tests/R2D2ThinkTest.java +++ b/tests/R2D2ThinkTest.java @@ -1,4 +1,44 @@ -public class R2D2ThinkTest { +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.starwars.R2D2; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class R2D2ThinkTest { + + private final R2D2 r2d2 = new R2D2(9999); + + R2D2ThinkTest() throws RobotIllegalStateException { + } + + @Test + void thinkTestStandard() throws RobotException, RobotMagicValueException { + + assertArrayEquals(new int[]{0,1,2,3,4,5}, r2d2.think(new int[]{3,0,1,5,2,4})); + } + + @Test + void thinkTestNegative() throws RobotException, RobotMagicValueException { + assertArrayEquals(new int[]{-3,-2,-1,0,1,2,3}, r2d2.think(new int[]{0,-3,2,-1,1,3,-2})); + } + + @Test + void thinkTestZeros() throws RobotException, RobotMagicValueException { + assertArrayEquals(new int[]{0,0,0,0}, r2d2.think(new int[]{0,0,0,0})); + } + + @Test + void thinkTestEmpty() throws RobotException, RobotMagicValueException { + assertArrayEquals(new int[]{}, r2d2.think(new int[]{})); + } + + @Test + void thinkTestMagicNumberException() { + assertThrows(RobotMagicValueException.class,() -> r2d2.think(new int[]{42})); + } + }