Exceptions eingebaut und werden nun geworfen
parent
0bead4f860
commit
8e874ca7b1
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,12 @@ package exceptions;
|
|||
|
||||
public class RobotException extends Exception{
|
||||
|
||||
public RobotException() {
|
||||
|
||||
}
|
||||
|
||||
public RobotException(String fehlertext) {
|
||||
super(fehlertext);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue