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; 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,14 +17,31 @@ public class C3PO extends Robotermodell implements Robot {
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
for (int i = 0; i < zahlen.length - 1; i++) { try {
int k = i; if (this.isPowerOn() == false) {
while (k >= 0 && zahlen[k] < zahlen[k+1]) { throw new RobotIllegalStateException();
int zwischenspeicher = zahlen[k];
zahlen[k] = zahlen[k+1];
zahlen[k+1] = zwischenspeicher;
k--;
} }
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; 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,19 +17,37 @@ public class R2D2 extends Robotermodell implements Robot {
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
for (int i = 0; i < zahlen.length; i++) { try {
int kMin = i; if (this.isPowerOn() == false) {
for (int k = i + 1; k < zahlen.length; k++) { throw new RobotIllegalStateException();
if (zahlen[k] < zahlen[kMin]) { }
kMin = k;
for (int i = 0; i < zahlen.length; i++) {
if (zahlen[i] == 42) {
throw new RobotMagicValueException();
} }
} }
if(kMin != i) { for (int i = 0; i < zahlen.length; i++) {
int zwischenspeicher = zahlen[i]; int kMin = i;
zahlen[i] = zahlen[kMin]; for (int k = i + 1; k < zahlen.length; k++) {
zahlen[kMin] = zwischenspeicher; 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; 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,26 +47,45 @@ 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();
if (this instanceof C3PO) { try {
hilfszeichen = ";"; if (this.isPowerOn() == false) {
} else { throw new RobotIllegalStateException();
hilfszeichen = ","; }
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(); 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);
}
} }