Exceptions eingebaut und werden nun geworfen

Exceptions
cedri 2023-01-07 22:06:06 +01:00
parent 0bead4f860
commit 8e874ca7b1
6 changed files with 114 additions and 34 deletions

View File

@ -1,6 +1,8 @@
package domain; package domain;
import exceptions.RobotException; import exceptions.RobotException;
import exceptions.RobotIllegalStateException;
import exceptions.RobotMagicValueException;
import roboterSystem.Robot; import roboterSystem.Robot;
public class C3PO extends Robotermodell implements Robot { public class C3PO extends Robotermodell implements Robot {
@ -15,6 +17,16 @@ public class C3PO extends Robotermodell implements Robot {
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
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++) { for (int i = 0; i < zahlen.length - 1; i++) {
int k = i; int k = i;
while (k >= 0 && zahlen[k] < zahlen[k+1]) { while (k >= 0 && zahlen[k] < zahlen[k+1]) {
@ -24,6 +36,13 @@ public class C3PO extends Robotermodell implements Robot {
k--; k--;
} }
} }
} catch (RobotIllegalStateException rise) {
letzteException = rise;
rise.printStackTrace();
} catch (RobotMagicValueException rmve) {
letzteException = rmve;
rmve.printStackTrace();
}
return zahlen; return zahlen;
} }

View File

@ -1,6 +1,8 @@
package domain; package domain;
import exceptions.RobotException; import exceptions.RobotException;
import exceptions.RobotIllegalStateException;
import exceptions.RobotMagicValueException;
import roboterSystem.Robot; import roboterSystem.Robot;
public class R2D2 extends Robotermodell implements Robot { public class R2D2 extends Robotermodell implements Robot {
@ -15,6 +17,17 @@ public class R2D2 extends Robotermodell implements Robot {
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
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; i++) { for (int i = 0; i < zahlen.length; i++) {
int kMin = i; int kMin = i;
for (int k = i + 1; k < zahlen.length; k++) { for (int k = i + 1; k < zahlen.length; k++) {
@ -29,6 +42,13 @@ public class R2D2 extends Robotermodell implements Robot {
zahlen[kMin] = zwischenspeicher; zahlen[kMin] = zwischenspeicher;
} }
} }
} catch (RobotIllegalStateException rise) {
letzteException = rise;
rise.printStackTrace();
} catch (RobotMagicValueException rmve) {
letzteException = rmve;
rmve.printStackTrace();
}
return zahlen; return zahlen;
} }

View File

@ -3,12 +3,15 @@ package domain;
import java.util.Arrays; import java.util.Arrays;
import exceptions.RobotException; import exceptions.RobotException;
import exceptions.RobotIllegalStateException;
import exceptions.RobotMagicValueException;
import roboterSystem.Robot; import roboterSystem.Robot;
public abstract class Robotermodell implements Robot { public abstract class Robotermodell implements Robot {
private int iD; private int iD;
private String name; private String name;
private boolean power = true; private boolean power = true;
protected RobotException letzteException;
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
@ -44,15 +47,26 @@ public abstract class Robotermodell implements Robot {
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
// TODO Auto-generated method stub return letzteException;
return null;
} }
@Override @Override
public String speak(int[] zahlen) throws RobotException { public String speak(int[] zahlen) throws RobotException {
String hilfszeichen;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
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) { if (this instanceof C3PO) {
hilfszeichen = ";"; hilfszeichen = ";";
} else { } else {
@ -64,6 +78,14 @@ public abstract class Robotermodell implements Robot {
sb.setLength(sb.length() - 2); sb.setLength(sb.length() - 2);
} catch (RobotIllegalStateException rise) {
letzteException = rise;
rise.printStackTrace();
} catch (RobotMagicValueException rmve) {
letzteException = rmve;
rmve.printStackTrace();
}
return sb.toString(); return sb.toString();
} }

View File

@ -2,6 +2,12 @@ package exceptions;
public class RobotException extends Exception{ public class RobotException extends Exception{
public RobotException() {
}
public RobotException(String fehlertext) {
super(fehlertext);
}
} }

View File

@ -1,6 +1,12 @@
package exceptions; 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);
}
} }

View File

@ -1,6 +1,13 @@
package exceptions; 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);
}
} }