2022-12-27 11:09:41 +01:00
|
|
|
package tpe.exceptions.roboter;
|
|
|
|
|
2023-01-07 11:19:18 +01:00
|
|
|
import java.util.*;
|
2023-01-07 14:15:53 +01:00
|
|
|
|
|
|
|
import tpe.exceptions.RobotException;
|
2023-01-07 16:32:29 +01:00
|
|
|
import tpe.exceptions.RobotIllegalStateException;
|
|
|
|
import tpe.exceptions.RobotMagicValueException;
|
2022-12-27 11:09:41 +01:00
|
|
|
|
2023-01-07 14:10:45 +01:00
|
|
|
public class R2D2 implements Robot {
|
2023-01-07 16:32:29 +01:00
|
|
|
private RobotException lastException;
|
2022-12-28 12:22:05 +01:00
|
|
|
private String name;
|
|
|
|
private boolean powerSwitch;
|
|
|
|
private int id;
|
2023-01-03 12:14:11 +01:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2022-12-28 12:22:05 +01:00
|
|
|
|
|
|
|
public R2D2(String name, boolean powerSwitch) {
|
|
|
|
super();
|
2023-01-07 11:19:18 +01:00
|
|
|
id = createId();
|
2022-12-28 12:22:05 +01:00
|
|
|
this.name = name;
|
|
|
|
this.powerSwitch = powerSwitch;
|
|
|
|
}
|
2023-01-07 11:19:18 +01:00
|
|
|
private int createId() {
|
|
|
|
Random randomID = new Random();
|
|
|
|
return randomID.nextInt(10000);
|
|
|
|
}
|
2022-12-27 11:09:41 +01:00
|
|
|
@Override
|
|
|
|
public int getId() {
|
2023-01-03 12:14:11 +01:00
|
|
|
|
2022-12-28 12:22:05 +01:00
|
|
|
return id;
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2022-12-28 12:22:05 +01:00
|
|
|
return name;
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void triggerPowerSwitch() {
|
2022-12-28 12:22:05 +01:00
|
|
|
if(powerSwitch=false)
|
|
|
|
powerSwitch=true;
|
|
|
|
else
|
|
|
|
powerSwitch=false;
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPowerOn() {
|
2022-12-28 12:22:05 +01:00
|
|
|
return powerSwitch;
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
2023-01-07 16:32:29 +01:00
|
|
|
|
|
|
|
public void setLastException(RobotException lastException) {
|
|
|
|
this.lastException = lastException;
|
|
|
|
}
|
|
|
|
|
2022-12-27 11:09:41 +01:00
|
|
|
@Override
|
|
|
|
public RobotException getLastException() {
|
2023-01-07 16:32:29 +01:00
|
|
|
return lastException;
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-01-07 14:10:45 +01:00
|
|
|
public String speak(int[] zahlen) throws RobotException {
|
|
|
|
|
2023-01-03 12:14:11 +01:00
|
|
|
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;
|
2023-01-07 14:10:45 +01:00
|
|
|
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
2023-01-07 16:32:29 +01:00
|
|
|
|
|
|
|
public boolean checkRobotMagicValueException(int[] zahlen) {
|
|
|
|
boolean error=false;
|
|
|
|
for(int i = 0; i < zahlen.length; i++) {
|
|
|
|
if(zahlen[i]==42) {
|
|
|
|
error=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2022-12-27 11:09:41 +01:00
|
|
|
@Override
|
2023-01-07 14:10:45 +01:00
|
|
|
public int[] think(int[] zahlen) throws RobotException {
|
2023-01-07 16:32:29 +01:00
|
|
|
if(!isPowerOn())
|
|
|
|
{
|
|
|
|
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-07 14:10:45 +01:00
|
|
|
}
|
2023-01-07 16:32:29 +01:00
|
|
|
else {
|
2022-12-28 12:22:05 +01:00
|
|
|
// Iterate through zahlen from left to right
|
|
|
|
for (int i = 0; i < zahlen.length - 1; i++) {
|
|
|
|
// Set the index of the current smallest element to i
|
|
|
|
int minIndex = i;
|
|
|
|
// Search the smallest element in zahlen
|
|
|
|
for (int j = i + 1; j < zahlen.length; j++) {
|
|
|
|
if (zahlen[j] < zahlen[minIndex]) {
|
|
|
|
minIndex = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Switch the smallest with the current element
|
|
|
|
int temp = zahlen[i];
|
|
|
|
zahlen[i] = zahlen[minIndex];
|
|
|
|
zahlen[minIndex] = temp;
|
|
|
|
}
|
|
|
|
return zahlen;
|
2023-01-07 16:32:29 +01:00
|
|
|
}
|
2023-01-07 14:10:45 +01:00
|
|
|
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|
2023-01-03 12:14:11 +01:00
|
|
|
|
2022-12-27 11:09:41 +01:00
|
|
|
}
|