Pr_robot_factory/domain/R2D2.java

98 lines
2.5 KiB
Java
Raw Normal View History

2022-12-08 14:58:44 +01:00
package domain;
2022-12-14 10:18:36 +01:00
2022-12-14 09:54:18 +01:00
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;
2022-12-14 09:54:18 +01:00
2022-12-09 00:30:35 +01:00
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] < )
}
}
}*/
2022-12-14 09:54:18 +01:00
public int[] think(int[] zahlen) throws RobotException {
2022-12-14 10:18:36 +01:00
if(isPowerOn()){
return sorting(zahlen);
2022-12-14 09:54:18 +01:00
}else{
2022-12-14 10:18:36 +01:00
throw new RobotIllegalStateException(getName() + " is turned off!");
2022-12-14 09:54:18 +01:00
}
}
2022-12-14 10:18:36 +01:00
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;
}
2022-12-14 11:08:49 +01:00
// 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.");
2022-12-14 10:18:36 +01:00
}
2022-12-14 11:08:49 +01:00
}
2022-12-14 09:54:18 +01:00
2022-12-14 11:08:49 +01:00
/**
* 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 {
2022-12-14 11:08:49 +01:00
throw new RobotIllegalStateException(getName() + " is turned off!");
2022-12-14 11:08:49 +01:00
}
/*public void something() {
System.out.println("Hello");
}*/
2022-12-14 11:08:49 +01:00
}
2022-12-14 10:18:36 +01:00
}