Pr_robot_factory/domain/C3PO.java

86 lines
2.6 KiB
Java

package domain;
import utility.robot_exceptions.ExceptionStorage;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.robotExceptions;
public class C3PO extends Robot {
public C3PO(int id, String name){
super(id, name, "C3PO");
}
/* 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 RobotException(robotExceptions.MAGICVALUE, getName());
// throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
}
}*/
@Override
public String speak(int[] input) throws RobotException {
if(isPowerOn()){
try{
return ausgabe(input, ";");
}catch(RobotException re){
return re.toString();
}
}else{
RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
this.exceptions = new ExceptionStorage(robotException);
throw robotException;
}
}
public int[] sorting(int[] input) throws RobotException{
if(checkArray(input)){
return insertionSort(input);
}else{
throw new RobotException(robotExceptions.MAGICVALUE, getName());
//throw new RobotMagicValueException(getName() + " has an unknown error. Code 42");
}
}
/**
* Sorts any given array of integers with the insertion Sort algorithm.
* @param input int [ ]
* @return input int [ ] (sorted)
* @throws RobotException
*/
public int[] insertionSort(int[] input) throws RobotException {
if (checkArray(input)) {
for (int i = 1; i < input.length; i++) {
int b = i - 1;
int key = input[i];
while (b >= 0 && input[b] > key) input[b + 1] = input[b--];
input[b + 1] = key;
}
return input;
}else{
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
}
}
@Override
public int[] think(int[] input) throws RobotException {
//Insertionsort
if(isPowerOn()){
return sorting(input);
}else{
RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
this.exceptions = new ExceptionStorage(robotException);
throw robotException;
}
}
}