From 8e874ca7b14a3ed5ceed09fb79dcdd1b9cea1651 Mon Sep 17 00:00:00 2001 From: cedri Date: Sat, 7 Jan 2023 22:06:06 +0100 Subject: [PATCH] Exceptions eingebaut und werden nun geworfen --- Robbie-Management-System/src/domain/C3PO.java | 33 ++++++++++--- Robbie-Management-System/src/domain/R2D2.java | 40 ++++++++++++---- .../src/domain/Robotermodell.java | 46 ++++++++++++++----- .../src/exceptions/RobotException.java | 8 +++- .../RobotIllegalStateException.java | 10 +++- .../exceptions/RobotMagicValueException.java | 11 ++++- 6 files changed, 114 insertions(+), 34 deletions(-) diff --git a/Robbie-Management-System/src/domain/C3PO.java b/Robbie-Management-System/src/domain/C3PO.java index 8c78c74..48f436e 100644 --- a/Robbie-Management-System/src/domain/C3PO.java +++ b/Robbie-Management-System/src/domain/C3PO.java @@ -1,6 +1,8 @@ package domain; import exceptions.RobotException; +import exceptions.RobotIllegalStateException; +import exceptions.RobotMagicValueException; import roboterSystem.Robot; public class C3PO extends Robotermodell implements Robot { @@ -15,14 +17,31 @@ public class C3PO extends Robotermodell implements Robot { @Override public int[] think(int[] zahlen) throws RobotException { - for (int i = 0; i < zahlen.length - 1; i++) { - int k = i; - while (k >= 0 && zahlen[k] < zahlen[k+1]) { - int zwischenspeicher = zahlen[k]; - zahlen[k] = zahlen[k+1]; - zahlen[k+1] = zwischenspeicher; - k--; + try { + if (this.isPowerOn() == false) { + throw new RobotIllegalStateException(); } + + for (int i = 0; i < zahlen.length; i++) { + if (zahlen[i] == 42) { + throw new RobotMagicValueException(); + } + } + for (int i = 0; i < zahlen.length - 1; i++) { + int k = i; + while (k >= 0 && zahlen[k] < zahlen[k+1]) { + int zwischenspeicher = zahlen[k]; + zahlen[k] = zahlen[k+1]; + zahlen[k+1] = zwischenspeicher; + k--; + } + } + } catch (RobotIllegalStateException rise) { + letzteException = rise; + rise.printStackTrace(); + } catch (RobotMagicValueException rmve) { + letzteException = rmve; + rmve.printStackTrace(); } return zahlen; diff --git a/Robbie-Management-System/src/domain/R2D2.java b/Robbie-Management-System/src/domain/R2D2.java index b869779..7d1e4dc 100644 --- a/Robbie-Management-System/src/domain/R2D2.java +++ b/Robbie-Management-System/src/domain/R2D2.java @@ -1,6 +1,8 @@ package domain; import exceptions.RobotException; +import exceptions.RobotIllegalStateException; +import exceptions.RobotMagicValueException; import roboterSystem.Robot; public class R2D2 extends Robotermodell implements Robot { @@ -15,19 +17,37 @@ public class R2D2 extends Robotermodell implements Robot { @Override public int[] think(int[] zahlen) throws RobotException { - for (int i = 0; i < zahlen.length; i++) { - int kMin = i; - for (int k = i + 1; k < zahlen.length; k++) { - if (zahlen[k] < zahlen[kMin]) { - kMin = k; + try { + if (this.isPowerOn() == false) { + throw new RobotIllegalStateException(); + } + + for (int i = 0; i < zahlen.length; i++) { + if (zahlen[i] == 42) { + throw new RobotMagicValueException(); } } - if(kMin != i) { - int zwischenspeicher = zahlen[i]; - zahlen[i] = zahlen[kMin]; - zahlen[kMin] = zwischenspeicher; - } + for (int i = 0; i < zahlen.length; i++) { + int kMin = i; + for (int k = i + 1; k < zahlen.length; k++) { + if (zahlen[k] < zahlen[kMin]) { + kMin = k; + } + } + + if(kMin != i) { + int zwischenspeicher = zahlen[i]; + zahlen[i] = zahlen[kMin]; + zahlen[kMin] = zwischenspeicher; + } + } + } catch (RobotIllegalStateException rise) { + letzteException = rise; + rise.printStackTrace(); + } catch (RobotMagicValueException rmve) { + letzteException = rmve; + rmve.printStackTrace(); } return zahlen; diff --git a/Robbie-Management-System/src/domain/Robotermodell.java b/Robbie-Management-System/src/domain/Robotermodell.java index c72248c..704e661 100644 --- a/Robbie-Management-System/src/domain/Robotermodell.java +++ b/Robbie-Management-System/src/domain/Robotermodell.java @@ -3,12 +3,15 @@ package domain; import java.util.Arrays; import exceptions.RobotException; +import exceptions.RobotIllegalStateException; +import exceptions.RobotMagicValueException; import roboterSystem.Robot; public abstract class Robotermodell implements Robot { private int iD; private String name; private boolean power = true; + protected RobotException letzteException; public void setName(String name) { this.name = name; @@ -44,26 +47,45 @@ public abstract class Robotermodell implements Robot { @Override public RobotException getLastException() { - // TODO Auto-generated method stub - return null; + return letzteException; } @Override public String speak(int[] zahlen) throws RobotException { - String hilfszeichen; StringBuilder sb = new StringBuilder(); - if (this instanceof C3PO) { - hilfszeichen = ";"; - } else { - hilfszeichen = ","; + try { + if (this.isPowerOn() == false) { + throw new RobotIllegalStateException(); + } + + for (int i = 0; i < zahlen.length; i++) { + if (zahlen[i] == 42) { + throw new RobotMagicValueException(); + } + } + + String hilfszeichen; + + if (this instanceof C3PO) { + hilfszeichen = ";"; + } else { + hilfszeichen = ","; + } + + Arrays.stream(zahlen) + .forEach(z -> sb.append(z + hilfszeichen + " ")); + + sb.setLength(sb.length() - 2); + + } catch (RobotIllegalStateException rise) { + letzteException = rise; + rise.printStackTrace(); + } catch (RobotMagicValueException rmve) { + letzteException = rmve; + rmve.printStackTrace(); } - Arrays.stream(zahlen) - .forEach(z -> sb.append(z + hilfszeichen + " ")); - - sb.setLength(sb.length() - 2); - return sb.toString(); } diff --git a/Robbie-Management-System/src/exceptions/RobotException.java b/Robbie-Management-System/src/exceptions/RobotException.java index 2fdcaf3..093d121 100644 --- a/Robbie-Management-System/src/exceptions/RobotException.java +++ b/Robbie-Management-System/src/exceptions/RobotException.java @@ -2,6 +2,12 @@ package exceptions; public class RobotException extends Exception{ - + public RobotException() { + + } + + public RobotException(String fehlertext) { + super(fehlertext); + } } diff --git a/Robbie-Management-System/src/exceptions/RobotIllegalStateException.java b/Robbie-Management-System/src/exceptions/RobotIllegalStateException.java index 69d22a9..b70fe30 100644 --- a/Robbie-Management-System/src/exceptions/RobotIllegalStateException.java +++ b/Robbie-Management-System/src/exceptions/RobotIllegalStateException.java @@ -1,6 +1,12 @@ package exceptions; -public class RobotIllegalStateException extends Exception{ - +public class RobotIllegalStateException extends RobotException{ + public RobotIllegalStateException() { + super("Leider ist der Roboter aus und kann nichts machen!"); + } + + public RobotIllegalStateException(String fehlertext) { + super(fehlertext); + } } diff --git a/Robbie-Management-System/src/exceptions/RobotMagicValueException.java b/Robbie-Management-System/src/exceptions/RobotMagicValueException.java index 55c261d..1157167 100644 --- a/Robbie-Management-System/src/exceptions/RobotMagicValueException.java +++ b/Robbie-Management-System/src/exceptions/RobotMagicValueException.java @@ -1,6 +1,13 @@ package exceptions; -public class RobotMagicValueException extends Exception{ - +public class RobotMagicValueException extends RobotException{ + public RobotMagicValueException() { + super("Zahl 42 kommt vor - Roboter: \"Ihhhhh\"!"); + } + + public RobotMagicValueException(String fehlertext) { + super(fehlertext); + } + }