Pr_robot_factory/domain/R2D2.java

98 lines
2.5 KiB
Java

package domain;
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
import robot.exceptions.RobotMagicValueException;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.IntPredicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class R2D2 extends RobotBasics {
/**
*
* @param id> int
* @param name> String
*/
public R2D2(int id, String name){
super(id, name);
}
/*Sorting sort = (int[] input) -> {
int small;
for(int i = 0; i < input.length -1; i++){
small = i;
for(int j = i + 1; j < input.length; j++){
if(input[j] < )
}
}
}*/
public int[] think(int[] zahlen) throws RobotException {
if(isPowerOn()){
return sorting(zahlen);
}else{
throw new RobotIllegalStateException(getName() + " is turned off!");
}
}
public int[] sorting(int[] arr) {
//Selectionsort
int small;
for (int i = 0; i <arr.length - 1; i++) {
small = i;
for (int j = i + 1; j < arr.length; j++) {
//if there is a smaller number on this position
if (arr[j] < arr[small]) {
small = j;
//swap values
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
}
return arr;
}
// Diese ganze Methode muss zu C3P0 und ist hier falsch.
public String ausgabe(int[] input)throws RobotException{
if(input.length != 0 && !checkArray(input)) {
return Arrays.stream(input)
.mapToObj(Integer::toString)
.collect(Collectors.joining("; "));
}else{
throw new RobotMagicValueException(getName() +" has an unknown Error. Code 42.");
}
}
/**
* Die Methode nimmt ein Array aus int entgegen und wandelt diese in einen String um.
* @param input Zahlen, die ausgegeben werden sollen.
* @return String oder null
* @throws RobotException
*/
@Override
public String speak(int[] input) throws RobotException {
final boolean[] found_42 = {false};
if (isPowerOn()) {
try{
return ausgabe(input);
}catch(RobotException re){
return re.toString();
}
} else {
throw new RobotIllegalStateException(getName() + " is turned off!");
}
/*public void something() {
System.out.println("Hello");
}*/
}
}