Merge remote-tracking branch 'origin/Exceptions' into main

C3POTest
cedri 2023-01-08 11:27:45 +01:00
commit 65ab237439
6 changed files with 114 additions and 34 deletions

View File

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

View File

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

View File

@ -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,15 +47,26 @@ 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();
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 {
@ -64,6 +78,14 @@ public abstract class Robotermodell implements Robot {
sb.setLength(sb.length() - 2);
} catch (RobotIllegalStateException rise) {
letzteException = rise;
rise.printStackTrace();
} catch (RobotMagicValueException rmve) {
letzteException = rmve;
rmve.printStackTrace();
}
return sb.toString();
}

View File

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

View File

@ -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);
}
}

View File

@ -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);
}
}