Pr_robot_factory/domain/Robot.java

127 lines
3.2 KiB
Java
Raw Normal View History

2022-12-09 00:29:00 +01:00
package domain;
import utility.robot_exceptions.ExceptionStorage;
import utility.robot_exceptions.RobotException;
import utility.robot_exceptions.robotExceptions;
2022-12-14 09:54:18 +01:00
import java.io.Serializable;
2022-12-14 09:54:18 +01:00
import java.util.Arrays;
import java.util.stream.Collectors;
public abstract class Robot implements utility.interfaces.Robot, Serializable {
// ENUMS für Robot Type
static enum RobotType {
C3PO, R2D2, Nexus6;
}
protected ExceptionStorage exceptions;
2022-12-09 00:29:00 +01:00
private int id;
2023-01-09 09:28:13 +01:00
private final String name;
2022-12-09 00:29:00 +01:00
private boolean power;
2023-01-09 23:32:30 +01:00
private RobotType type;
2023-01-09 23:32:30 +01:00
public Robot(int id, String name, RobotType type){
this.id = id;
this.name = name;
this.power = false;
this.exceptions = null;
this.type = type;
}
2022-12-09 00:29:00 +01:00
/**
* @see utility.interfaces.RobotControl;
2022-12-09 00:29:00 +01:00
*/
@Override
public int getId() {
return id;
}
/**
* @see utility.interfaces.RobotControl;
2022-12-09 00:29:00 +01:00
*/
@Override
public String getName() {
return name;
}
/**
* @see utility.interfaces.RobotControl;
2022-12-09 00:29:00 +01:00
*/
@Override
public void triggerPowerSwitch() {
power = !power;
2022-12-09 00:29:00 +01:00
}
/**
* @see utility.interfaces.RobotControl;
2022-12-09 00:29:00 +01:00
*/
@Override
public boolean isPowerOn() {
return power;
}
/**
* @see utility.interfaces.RobotControl;
2022-12-14 09:54:18 +01:00
*/
@Override
public RobotException getLastException() {
return exceptions.getLastErrorMessage();
2022-12-14 09:54:18 +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
*/
// Check lists for the forbidden Number 42
public boolean checkArray(int[] input) throws RobotException{
if(input.length != 0){
for(int x: input){
if(x == 42){ return false; }
}
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;
}
}
/**
* 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
*/
// Write an array with a delimiter to the command line
public String output(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;
}
}
2023-01-06 22:55:33 +01:00
public String getType(){
2023-01-09 23:32:30 +01:00
return this.type.toString();
}
2022-12-25 19:57:20 +01:00
// Override the to String method to get a clean looking outcome
@Override
public String toString(){
return "Name: " + name + "; ID: " + id + "; Type: " + type;
}
2022-12-25 19:57:20 +01:00
2022-12-09 00:29:00 +01:00
}