RoboterFabrik/Roboter/tpe/exceptions/roboter/Robots.java

65 lines
1.4 KiB
Java

package tpe.exceptions.roboter;
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 {
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());
}
return null;
}
@Override
public abstract int[] think(int[] zahlen) throws RobotException;
}