diff --git a/resources/T1000-Concept.txt b/resources/T1000-Concept.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java index f398904..cbf045f 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java @@ -5,6 +5,14 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateExceptio import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; import de.hsmannheim.informatik.name.domain.requirements.Robot; +/** + *

RobotBluePrint

+ * + * Represents a blueprint for all robots, + * implementing the specified requirements + * + * @see Robot + */ public abstract class RobotBluePrint implements Robot { protected final int ID; @@ -13,14 +21,16 @@ public abstract class RobotBluePrint implements Robot { private final String name; private boolean isPowered; + private RobotException lastException; + 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; this.isPowered = false; + if (!this.isValidID(id)) { + //TODO write own Exception & change Tests + throw new RobotIllegalStateException(String.format("%d is not a valid ID", id), this); + } + this.ID = id; } /** @@ -54,7 +64,11 @@ public abstract class RobotBluePrint implements Robot { @Override public RobotException getLastException() { - return null; + return this.lastException; + } + + public void setLastException(RobotException lastException) { + this.lastException = lastException; } @Override diff --git a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java index a4efcf5..1c4cdf5 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java @@ -1,4 +1,11 @@ package de.hsmannheim.informatik.name.domain; +/** + *

RobotFactory

+ *

+ * Factory Class for producing Robots of Robots specified by {@link RobotType}. + * + * @see RobotType + */ public class RobotFactory { } diff --git a/src/de/hsmannheim/informatik/name/domain/RobotType.java b/src/de/hsmannheim/informatik/name/domain/RobotType.java index 6ea83cd..53f116d 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotType.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotType.java @@ -1,4 +1,11 @@ package de.hsmannheim.informatik.name.domain; +/** + *

RobotType

+ *

+ * Enumeration to represent a creatable Robot + */ public enum RobotType { + C3P0, + R2D2 } diff --git a/src/de/hsmannheim/informatik/name/domain/bladerunner/Nexus6.java b/src/de/hsmannheim/informatik/name/domain/bladerunner/Nexus6.java new file mode 100644 index 0000000..080608f --- /dev/null +++ b/src/de/hsmannheim/informatik/name/domain/bladerunner/Nexus6.java @@ -0,0 +1,59 @@ +package de.hsmannheim.informatik.name.domain.bladerunner; + +import de.hsmannheim.informatik.name.domain.RobotBluePrint; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; + +/** + *

Nexus6

+ *

+ * Nexus-6 Robot with Name "Pris" and SerialNumber 19281982. + * This Robot doesnt work properly, since it has been damaged. + *

+ * There can only exist one Instance of this Robot. + * + * @see RobotBluePrint + */ +public class Nexus6 extends RobotBluePrint { + + private static Nexus6 nexus6 = null; + + private final static int NEXUS_ID = 19281982; + + private final static String NEXUS_NAME = "Pris"; + + private Nexus6() throws RobotIllegalStateException { + super(NEXUS_ID, NEXUS_NAME); + } + + /** + * Creates an instance of {@link Nexus6}, if there currently is no available instance yet. + * + * @return instance of {@link Nexus6} + * @throws RobotIllegalStateException if the instance could not be created + */ + public static Nexus6 getInstance() throws RobotIllegalStateException { + if (nexus6 == null) { + nexus6 = new Nexus6(); + } + return nexus6; + } + + @Override + protected boolean isValidID(int id) { + return id == NEXUS_ID; + } + + @Override + public void triggerPowerSwitch() { + } + + @Override + public String speak(int[] zahlen) throws RobotIllegalStateException { + throw new RobotIllegalStateException("Robot is defect", this); + } + + @Override + public int[] think(int[] zahlen) throws RobotIllegalStateException { + throw new RobotIllegalStateException("Robot is defect", this); + } +} diff --git a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java index e270194..d0d839b 100644 --- a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java +++ b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java @@ -1,4 +1,16 @@ package de.hsmannheim.informatik.name.domain.exceptions; +import de.hsmannheim.informatik.name.domain.RobotBluePrint; + +/** + *

RobotException + *

+ * Exception for any unexpected Behaviour for any Robot. + */ public class RobotException extends Exception { + + public RobotException(String s, RobotBluePrint robot) { + super(s); + robot.setLastException(this); + } } diff --git a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java index 4f85eec..9464394 100644 --- a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java +++ b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java @@ -1,4 +1,9 @@ package de.hsmannheim.informatik.name.domain.exceptions; -public class RobotIllegalStateException extends Exception { +import de.hsmannheim.informatik.name.domain.RobotBluePrint; + +public class RobotIllegalStateException extends RobotException { + public RobotIllegalStateException(String s, RobotBluePrint robot) { + super(s, robot); + } } diff --git a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotMagicValueException.java b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotMagicValueException.java index 21d809d..26f6f3d 100644 --- a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotMagicValueException.java +++ b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotMagicValueException.java @@ -1,4 +1,9 @@ package de.hsmannheim.informatik.name.domain.exceptions; -public class RobotMagicValueException extends Exception { +import de.hsmannheim.informatik.name.domain.RobotBluePrint; + +public class RobotMagicValueException extends RobotException { + public RobotMagicValueException(String s, RobotBluePrint robot) { + super(s, robot); + } } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java index 2679744..9c429b5 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java @@ -3,6 +3,14 @@ package de.hsmannheim.informatik.name.domain.starwars; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +/** + *

C3P0

+ *

+ * R2D2 robot using the InsertionSort-algorithm for {@code think()} and separating + * each element in {@code speak()} with ";". + * + * @see StarWarsRobot + */ public class C3PO extends StarWarsRobot { public C3PO(int id) throws RobotIllegalStateException { super(';', id, "C3PO"); diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java index 8b3e6e9..786aa11 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java @@ -4,6 +4,14 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; +/** + *

R2D2

+ *

+ * R2D2 robot using the SelectionSort-algorithm for {@code think()} and separating + * each element in {@code speak()} with ",". + * + * @see StarWarsRobot + */ public class R2D2 extends StarWarsRobot { public R2D2(int id) throws RobotIllegalStateException { super(',', id, "R2D2"); @@ -18,8 +26,8 @@ public class R2D2 extends StarWarsRobot { 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(); + if (arrayToSort[i] == MAGIC_NUMBER) { + throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this); } int smallest = Integer.MAX_VALUE; int smallestIndex = 0; diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java index 7ad894b..497181a 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java @@ -8,7 +8,13 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; import java.util.Arrays; import java.util.stream.Collectors; - +/** + *

StarWarsRobot

+ * + * Class to represent a Star Wars robot. + * + * @see RobotBluePrint + */ public abstract class StarWarsRobot extends RobotBluePrint { private final char SEPARATOR; @@ -19,14 +25,9 @@ public abstract class StarWarsRobot extends RobotBluePrint { } @Override - public RobotException getLastException() { - return null; - } - - @Override - public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { + public String speak(int[] zahlen) throws RobotException { if (Arrays.stream(zahlen).anyMatch(i -> i == MAGIC_NUMBER)) { - throw new RobotMagicValueException(); + throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this); } return this.buildString(zahlen); } diff --git a/tests/R2D2IDTest.java b/tests/R2D2IDTest.java new file mode 100644 index 0000000..cf33bc9 --- /dev/null +++ b/tests/R2D2IDTest.java @@ -0,0 +1,28 @@ +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import de.hsmannheim.informatik.name.domain.starwars.R2D2; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class R2D2IDTest { + + @Test + public void NegativeTest(){ + assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1)); + assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MIN_VALUE)); + } + + @Test + public void InIDRangeTest() { + assertDoesNotThrow(() -> new R2D2(0)); + assertDoesNotThrow(() -> new R2D2(1)); + assertDoesNotThrow(() -> new R2D2(9999)); + } + + @Test + public void OuterIDRangeTest() { + assertThrows(RobotIllegalStateException.class, () -> new R2D2(10000)); + assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MAX_VALUE)); + } +}