Pr_robot_factory/domain/C3PO.java

65 lines
1.9 KiB
Java
Raw Normal View History

2022-12-08 14:58:44 +01:00
package domain;
2023-01-06 22:55:33 +01:00
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){
2023-01-09 23:32:30 +01:00
super(id, name, RobotType.C3PO);
}
//returns the sorted list as String with the delimiter ';'
@Override
public String speak(int[] input) throws RobotException {
if(isPowerOn()){
try{
return output(input, ";");
}catch(RobotException re){
return re.toString();
}
}else{
2022-12-25 19:57:20 +01:00
RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
this.exceptions = new ExceptionStorage(robotException);
throw robotException;
}
}
2023-01-06 22:55:33 +01:00
/**
* 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;
}
}
//Sorting then returning the input arr with insertion Sort
2022-12-14 09:54:18 +01:00
@Override
public int[] think(int[] input) throws RobotException {
if(isPowerOn()){
return insertionSort(input);
}else{
2023-01-06 22:55:33 +01:00
RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
this.exceptions = new ExceptionStorage(robotException);
throw robotException;
}
2022-12-14 09:54:18 +01:00
}
}