From ae17a3013e8b2eec10e6a1fb1a992b2f2b795ecf Mon Sep 17 00:00:00 2001 From: romanamo Date: Sat, 24 Dec 2022 12:49:59 +0100 Subject: [PATCH] added RobotFactory method, documentation --- .../name/domain/RobotBluePrint.java | 1 - .../informatik/name/domain/RobotFactory.java | 27 +++++++++++++++++++ .../domain/exceptions/RobotException.java | 7 +++++ .../RobotIllegalStateException.java | 7 +++++ .../exceptions/RobotMagicValueException.java | 7 +++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java index cbf045f..d83c048 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java @@ -27,7 +27,6 @@ public abstract class RobotBluePrint implements Robot { 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; diff --git a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java index 1c4cdf5..aae92fa 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java @@ -1,5 +1,10 @@ package de.hsmannheim.informatik.name.domain; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import de.hsmannheim.informatik.name.domain.requirements.Robot; +import de.hsmannheim.informatik.name.domain.starwars.C3PO; +import de.hsmannheim.informatik.name.domain.starwars.R2D2; + /** *

RobotFactory

*

@@ -8,4 +13,26 @@ package de.hsmannheim.informatik.name.domain; * @see RobotType */ public class RobotFactory { + + /** + * Factory Method to get a {@link Robot} + * + * @param type {@link RobotType Type} of the robot + * @param serialNumber serialnumber in form of an {@code int} + * @return a {@link Robot robot instance} of specified type, if no type matches {@code null} + * @throws RobotIllegalStateException the robot initializes in an illegal state + */ + public Robot getRobot(RobotType type, int serialNumber) throws RobotIllegalStateException { + switch (type) { + case C3P0 -> { + return new C3PO(serialNumber); + } + case R2D2 -> { + return new R2D2(serialNumber); + } + default -> { + return null; + } + } + } } diff --git a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java index d0d839b..2277bcc 100644 --- a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java +++ b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotException.java @@ -9,8 +9,15 @@ import de.hsmannheim.informatik.name.domain.RobotBluePrint; */ public class RobotException extends Exception { + private final String robotName; + public RobotException(String s, RobotBluePrint robot) { super(s); robot.setLastException(this); + this.robotName = robot.getName(); + } + + public String getRobotName() { + return this.robotName; } } diff --git a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java index 9464394..f45e834 100644 --- a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java +++ b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotIllegalStateException.java @@ -2,6 +2,13 @@ package de.hsmannheim.informatik.name.domain.exceptions; import de.hsmannheim.informatik.name.domain.RobotBluePrint; +/** + *

RobotIllegalStateException + *

+ * Exception for the robot reaching any state that is considered illegal. + * + * @see RobotException + */ 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 26f6f3d..255996d 100644 --- a/src/de/hsmannheim/informatik/name/domain/exceptions/RobotMagicValueException.java +++ b/src/de/hsmannheim/informatik/name/domain/exceptions/RobotMagicValueException.java @@ -2,6 +2,13 @@ package de.hsmannheim.informatik.name.domain.exceptions; import de.hsmannheim.informatik.name.domain.RobotBluePrint; +/** + *

RobotMagicValueException + *

+ * Exception for the robot detecting any kind of Magic Number. + * + * @see RobotException + */ public class RobotMagicValueException extends RobotException { public RobotMagicValueException(String s, RobotBluePrint robot) { super(s, robot);