161 lines
4.2 KiB
Java
161 lines
4.2 KiB
Java
package domain;
|
|
|
|
import robot.exceptions.ExceptionStorage;
|
|
import robot.exceptions.RobotException;
|
|
import robot.exceptions.robotExceptions;
|
|
import robot.interfaces.Robot;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public abstract class RobotBasics implements Robot {
|
|
protected ExceptionStorage exceptions;
|
|
private int id;
|
|
private String name;
|
|
private boolean power;
|
|
|
|
public RobotBasics(int id, String name){
|
|
this.id = id;
|
|
this.name = name;
|
|
this.power = false;
|
|
this.exceptions = null;
|
|
}
|
|
|
|
/**
|
|
* @see robot.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public int getId() {
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* @see robot.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* @see robot.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public void triggerPowerSwitch() {
|
|
if(power){
|
|
power = false;
|
|
}else{
|
|
power = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see robot.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public boolean isPowerOn() {
|
|
return power;
|
|
}
|
|
|
|
/**
|
|
* @see robot.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public RobotException getLastException() {
|
|
return exceptions.getLastErrorMessage();
|
|
}
|
|
|
|
/**
|
|
* 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; }
|
|
}
|
|
return true;
|
|
}else{
|
|
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.EMPTYARRAY, getName()));
|
|
throw new RobotException(robotExceptions.EMPTYARRAY, getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 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{
|
|
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
|
|
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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)){
|
|
return new int[0];
|
|
}else{
|
|
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
|
|
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 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{
|
|
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
|
|
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
}
|
|
|
|
}
|
|
}
|