176 lines
4.5 KiB
Java
176 lines
4.5 KiB
Java
package domain;
|
|
|
|
import utility.robot_exceptions.ExceptionStorage;
|
|
import utility.robot_exceptions.RobotException;
|
|
import utility.robot_exceptions.robotExceptions;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.Arrays;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public abstract class Robot implements utility.interfaces.Robot, Serializable {
|
|
protected ExceptionStorage exceptions;
|
|
private int id;
|
|
private String name;
|
|
private boolean power;
|
|
|
|
private String type;
|
|
|
|
public Robot(int id, String name, String type){
|
|
this.id = id;
|
|
this.name = name;
|
|
this.power = false;
|
|
this.exceptions = null;
|
|
this.type = type;
|
|
}
|
|
|
|
/**
|
|
* @see utility.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public int getId() {
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* @see utility.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* @see utility.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public void triggerPowerSwitch() {
|
|
if(power){
|
|
power = false;
|
|
}else{
|
|
power = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see utility.interfaces.RobotControl;
|
|
*/
|
|
@Override
|
|
public boolean isPowerOn() {
|
|
return power;
|
|
}
|
|
|
|
/**
|
|
* @see utility.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{
|
|
RobotException robotexception = new RobotException(robotExceptions.EMPTYARRAY, getName());
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
throw robotexception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 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{
|
|
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
throw robotexception;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
return input;
|
|
}else{
|
|
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
throw robotexception;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public String toString(){
|
|
return "Name: " + name + "; ID: " + id + "; Type: " + type;
|
|
}
|
|
|
|
}
|