2022-12-09 00:29:00 +01:00
|
|
|
package domain;
|
|
|
|
|
2023-01-02 19:51:04 +01:00
|
|
|
import safety.robot_exceptions.ExceptionStorage;
|
|
|
|
import safety.robot_exceptions.RobotException;
|
|
|
|
import safety.robot_exceptions.robotExceptions;
|
|
|
|
import safety.interfaces.Robot;
|
2022-12-14 09:54:18 +01:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2022-12-21 16:44:49 +01:00
|
|
|
import java.util.stream.Collectors;
|
2022-12-09 10:45:51 +01:00
|
|
|
|
2022-12-09 01:38:11 +01:00
|
|
|
|
2022-12-20 12:03:41 +01:00
|
|
|
public abstract class RobotBasics implements Robot {
|
2022-12-21 16:44:49 +01:00
|
|
|
protected ExceptionStorage exceptions;
|
2022-12-09 00:29:00 +01:00
|
|
|
private int id;
|
|
|
|
private String name;
|
|
|
|
private boolean power;
|
|
|
|
|
2022-12-09 01:38:11 +01:00
|
|
|
public RobotBasics(int id, String name){
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
this.power = false;
|
2022-12-21 16:44:49 +01:00
|
|
|
this.exceptions = null;
|
2022-12-09 01:38:11 +01:00
|
|
|
}
|
2022-12-21 16:44:49 +01:00
|
|
|
|
2022-12-09 00:29:00 +01:00
|
|
|
/**
|
2023-01-02 19:51:04 +01:00
|
|
|
* @see safety.interfaces.RobotControl;
|
2022-12-09 00:29:00 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-02 19:51:04 +01:00
|
|
|
* @see safety.interfaces.RobotControl;
|
2022-12-09 00:29:00 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-02 19:51:04 +01:00
|
|
|
* @see safety.interfaces.RobotControl;
|
2022-12-09 00:29:00 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void triggerPowerSwitch() {
|
2022-12-20 12:03:41 +01:00
|
|
|
if(power){
|
2022-12-09 01:38:11 +01:00
|
|
|
power = false;
|
2022-12-20 12:03:41 +01:00
|
|
|
}else{
|
|
|
|
power = true;
|
2022-12-09 01:38:11 +01:00
|
|
|
}
|
2022-12-09 00:29:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-02 19:51:04 +01:00
|
|
|
* @see safety.interfaces.RobotControl;
|
2022-12-09 00:29:00 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean isPowerOn() {
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-02 19:51:04 +01:00
|
|
|
* @see safety.interfaces.RobotControl;
|
2022-12-14 09:54:18 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public RobotException getLastException() {
|
2022-12-21 16:44:49 +01:00
|
|
|
return exceptions.getLastErrorMessage();
|
2022-12-14 09:54:18 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 16:44:49 +01:00
|
|
|
/**
|
|
|
|
* This method checks an array of integers and gives back a boolean value.
|
|
|
|
* If the array contains the number 42 the method returns false.
|
|
|
|
* Otherwise, always true.
|
|
|
|
*
|
|
|
|
* If the length of the Array = 0 it throws an EMPTYARRAY-Exception
|
|
|
|
* @param input
|
|
|
|
* @return boolean
|
|
|
|
* @throws RobotException EMPTYARRAY Exception
|
|
|
|
*/
|
|
|
|
public boolean checkArray(int[] input) throws RobotException{
|
|
|
|
if(input.length != 0){
|
|
|
|
for(int x: input){
|
|
|
|
if(x == 42){ return false; }
|
2022-12-20 12:03:41 +01:00
|
|
|
}
|
2022-12-21 16:44:49 +01:00
|
|
|
return true;
|
|
|
|
}else{
|
2022-12-25 19:57:20 +01:00
|
|
|
RobotException robotexception = new RobotException(robotExceptions.EMPTYARRAY, getName());
|
|
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
|
|
throw robotexception;
|
2022-12-21 16:44:49 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method uses Streams to join any given array to a String.
|
|
|
|
*
|
|
|
|
* @param input int [ ]
|
|
|
|
* @param delemiter String
|
|
|
|
* @return String (array as String)
|
|
|
|
* @throws RobotException
|
|
|
|
*/
|
|
|
|
public String ausgabe(int[] input, String delemiter)throws RobotException{
|
|
|
|
if(checkArray(input)) {
|
|
|
|
return Arrays.stream(input)
|
|
|
|
.mapToObj(Integer::toString)
|
|
|
|
.collect(Collectors.joining(delemiter + " "));
|
|
|
|
}else{
|
2022-12-25 19:57:20 +01:00
|
|
|
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
|
|
throw robotexception;
|
2022-12-21 16:44:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts any given array of integers with the insertion Sort algorithm.
|
|
|
|
* @param input int [ ]
|
|
|
|
* @return input int [ ] (sorted)
|
|
|
|
* @throws RobotException
|
|
|
|
*/
|
2022-12-25 19:57:20 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-12-21 16:44:49 +01:00
|
|
|
|
2022-12-25 19:57:20 +01:00
|
|
|
}
|
2022-12-09 10:45:51 +01:00
|
|
|
|
2022-12-21 16:44:49 +01:00
|
|
|
/**
|
|
|
|
* Sorts any given array of integers with the selection Sort algorithm.
|
|
|
|
* @param input
|
|
|
|
* @return
|
|
|
|
* @throws RobotException
|
|
|
|
*/
|
|
|
|
public int[] selectionSort(int[] input) throws RobotException{
|
|
|
|
if(checkArray(input)){
|
|
|
|
int small;
|
|
|
|
for(int i = 0; i < input.length; i++){
|
|
|
|
small = i;
|
|
|
|
for(int j = i + 1; j < input.length; j++){
|
|
|
|
if(input[j] < input[small]){
|
|
|
|
small = j;
|
|
|
|
// System.out.println(small);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int temp = input[i];
|
|
|
|
input[i] = input[small];
|
|
|
|
input[small] = temp;
|
|
|
|
/*for(int n = 0; n < input.length; n++){
|
|
|
|
System.out.print( " " + input[n]);
|
|
|
|
}
|
|
|
|
System.out.println();*/
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}else{
|
2022-12-25 19:57:20 +01:00
|
|
|
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
|
|
throw robotexception;
|
2022-12-21 16:44:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-12-25 19:57:20 +01:00
|
|
|
|
|
|
|
|
2022-12-09 00:29:00 +01:00
|
|
|
}
|