2023-01-07 18:48:00 +01:00
|
|
|
package tpe.exceptions.roboter;
|
|
|
|
|
2023-01-09 11:30:15 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2023-01-07 18:48:00 +01:00
|
|
|
import tpe.exceptions.RobotException;
|
|
|
|
import tpe.exceptions.RobotIllegalStateException;
|
|
|
|
import tpe.exceptions.RobotMagicValueException;
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class Robots implements Robot{
|
|
|
|
protected String name;
|
|
|
|
protected RobotException error;
|
|
|
|
protected boolean powerStatus;
|
|
|
|
|
|
|
|
public Robots(String name) {
|
|
|
|
this.name = name;
|
|
|
|
this.powerStatus = false;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public abstract int getId();
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void triggerPowerSwitch() {
|
|
|
|
if(powerStatus==false)
|
|
|
|
powerStatus=true;
|
|
|
|
else
|
|
|
|
powerStatus=false;
|
|
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean isPowerOn() {
|
|
|
|
|
|
|
|
return powerStatus;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public RobotException getLastException() {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
public boolean checkRobotMagicValueException(int[] zahlen) {
|
|
|
|
boolean error=false;
|
|
|
|
for(int i = 0; i < zahlen.length; i++) {
|
|
|
|
if(zahlen[i]==42) {
|
|
|
|
error=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String speak(int[] zahlen) throws RobotException {
|
2023-01-09 11:30:15 +01:00
|
|
|
|
2023-01-07 18:48:00 +01:00
|
|
|
if(powerStatus==false)
|
|
|
|
{
|
|
|
|
throw new RobotIllegalStateException("Der Roboter ist ausgeschaltet!", this.getName());
|
|
|
|
}
|
|
|
|
else if(checkRobotMagicValueException(zahlen)==true)
|
|
|
|
{
|
|
|
|
throw new RobotMagicValueException("Zahl 42 kann nicht verarbeitet werden!",this.getName());
|
2023-01-09 11:30:15 +01:00
|
|
|
}else
|
|
|
|
if(zahlen[0]<zahlen[zahlen.length-1])
|
|
|
|
{
|
|
|
|
return Arrays.stream(zahlen)
|
|
|
|
.mapToObj(String::valueOf)
|
|
|
|
.collect(Collectors.joining(", "));
|
2023-01-07 18:48:00 +01:00
|
|
|
}
|
2023-01-09 11:30:15 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return Arrays.stream(zahlen)
|
|
|
|
.mapToObj(String::valueOf)
|
|
|
|
.collect(Collectors.joining("; "));
|
|
|
|
}
|
|
|
|
|
2023-01-07 18:48:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public abstract int[] think(int[] zahlen) throws RobotException;
|
|
|
|
}
|