speak Methode angepasst

main
Lukas Berens 2023-01-09 11:30:15 +01:00
parent 68deef36dc
commit 5684f0df51
3 changed files with 18 additions and 28 deletions

View File

@ -25,20 +25,7 @@ public class C3PO extends Robots{
}
@Override
public String speak(int[] zahlen) throws RobotException {
for (int i = 0; i < zahlen.length; i++) {
sb.append(zahlen[i]);
if (i < zahlen.length - 1) {
sb.append("; ");
}
}
String output = sb.toString();
return output;
}
@Override
public int[] think(int[] zahlen) throws RobotIllegalStateException, RobotMagicValueException {

View File

@ -25,20 +25,7 @@ public class R2D2 extends Robots {
return id;
}
@Override
public String speak(int[] zahlen) throws RobotException {
for (int i = 0; i < zahlen.length; i++) {
sb.append(zahlen[i]);
if (i < zahlen.length - 1) {
sb.append(", ");
}
}
String output = sb.toString();
return output;
}
@Override

View File

@ -1,5 +1,8 @@
package tpe.exceptions.roboter;
import java.util.Arrays;
import java.util.stream.Collectors;
import tpe.exceptions.RobotException;
import tpe.exceptions.RobotIllegalStateException;
import tpe.exceptions.RobotMagicValueException;
@ -48,6 +51,7 @@ public abstract class Robots implements Robot{
}
public String speak(int[] zahlen) throws RobotException {
if(powerStatus==false)
{
throw new RobotIllegalStateException("Der Roboter ist ausgeschaltet!", this.getName());
@ -55,9 +59,21 @@ public abstract class Robots implements Robot{
else if(checkRobotMagicValueException(zahlen)==true)
{
throw new RobotMagicValueException("Zahl 42 kann nicht verarbeitet werden!",this.getName());
}else
if(zahlen[0]<zahlen[zahlen.length-1])
{
return Arrays.stream(zahlen)
.mapToObj(String::valueOf)
.collect(Collectors.joining(", "));
}
else
{
return Arrays.stream(zahlen)
.mapToObj(String::valueOf)
.collect(Collectors.joining("; "));
}
return null;
}
@Override
public abstract int[] think(int[] zahlen) throws RobotException;