Superklasse für Roboter

main
Lukas Berens 2023-01-07 18:48:00 +01:00
parent 05e8fe64b6
commit 40c35b2ea8
6 changed files with 115 additions and 62 deletions

View File

@ -3,14 +3,16 @@ package tpe.exceptions.roboter;
import tpe.exceptions.RobotException;
public class Nexus6 implements RobotControl, RobotInstructions {
private RobotType robotType;
private static int id=19281982;
private static String name="Pris";
private static boolean powerSwitch=false;
private static Nexus6 instance = new Nexus6(id, name, powerSwitch);
private Nexus6(int id, String name, boolean powerSwitch) {
super();
robotType= RobotType.NEXUS6;
}
public static Nexus6 getInstance() {

View File

@ -6,55 +6,21 @@ import tpe.exceptions.RobotException;
import tpe.exceptions.RobotIllegalStateException;
import tpe.exceptions.RobotMagicValueException;
public class R2D2 implements Robot {
public class R2D2 extends Robots {
RobotType robotType;
private RobotException lastException;
private String name;
private boolean powerSwitch;
private int id;
StringBuilder sb = new StringBuilder();
public R2D2(String name, boolean powerSwitch) {
super();
id = createId();
public R2D2(String name,int id) {
super(name);
this.id=id;
this.name = name;
this.powerSwitch = powerSwitch;
}
private int createId() {
Random randomID = new Random();
return randomID.nextInt(10000);
}
@Override
public int getId() {
return id;
robotType= RobotType.R2D2;
}
@Override
public String getName() {
return name;
}
@Override
public void triggerPowerSwitch() {
if(powerSwitch=false)
powerSwitch=true;
else
powerSwitch=false;
}
@Override
public boolean isPowerOn() {
return powerSwitch;
}
public void setLastException(RobotException lastException) {
this.lastException = lastException;
}
@Override
public RobotException getLastException() {
return lastException;
}
@Override
public String speak(int[] zahlen) throws RobotException {
@ -71,15 +37,6 @@ public class R2D2 implements Robot {
}
public boolean checkRobotMagicValueException(int[] zahlen) {
boolean error=false;
for(int i = 0; i < zahlen.length; i++) {
if(zahlen[i]==42) {
error=true;
}
}
return error;
}
@Override
public int[] think(int[] zahlen) throws RobotException {
@ -111,5 +68,8 @@ public class R2D2 implements Robot {
}
}
public RobotType getRobotType () {
return this.robotType;
}
}

View File

@ -1,5 +1,32 @@
package tpe.exceptions.roboter;
public class RobotFactory {
import java.util.HashMap;
public class RobotFactory {
private Nexus6 nexus6=Nexus6.getInstance();
private HashMap<Integer,Robots> robotStock=new HashMap<>();
public int constructRobot(RobotType robotType, String name) {
Robots robot;
if(RobotType.R2D2==robotType) {
robot=new R2D2(name, checkID(0,9999));
robotStock.put(robot.getId(), robot);
return robot.getId();
}else if(RobotType.C3PO==robotType) {
robot=new C3PO(name, checkID());
robotStock.put(robot.getId(), robot);
return robot.getId();
}
}
private int checkID(int minValue, int maxValue) {
int randomID = (int) (Math.random()*(maxValue - minValue)) + minValue;
if(robotStock.containsKey(randomID))
{
checkID(minValue,maxValue);
}
return randomID;
}
}

View File

@ -0,0 +1,9 @@
package tpe.exceptions.roboter;
public enum RobotType {
R2D2,
C3PO,
NEXUS6
}

View File

@ -0,0 +1,64 @@
package tpe.exceptions.roboter;
import tpe.exceptions.RobotException;
import tpe.exceptions.RobotIllegalStateException;
import tpe.exceptions.RobotMagicValueException;
public abstract class Robots implements Robot{
protected String name;
protected RobotException error;
protected boolean powerStatus;
public Robots(String name) {
this.name = name;
this.powerStatus = false;
}
@Override
public abstract int getId();
@Override
public String getName() {
return name;
}
@Override
public void triggerPowerSwitch() {
if(powerStatus==false)
powerStatus=true;
else
powerStatus=false;
}
@Override
public boolean isPowerOn() {
return powerStatus;
}
@Override
public RobotException getLastException() {
return error;
}
public boolean checkRobotMagicValueException(int[] zahlen) {
boolean error=false;
for(int i = 0; i < zahlen.length; i++) {
if(zahlen[i]==42) {
error=true;
}
}
return error;
}
public String speak(int[] zahlen) throws RobotException {
if(powerStatus==false)
{
throw new RobotIllegalStateException("Der Roboter ist ausgeschaltet!", this.getName());
}
else if(checkRobotMagicValueException(zahlen)==true)
{
throw new RobotMagicValueException("Zahl 42 kann nicht verarbeitet werden!",this.getName());
}
return null;
}
@Override
public abstract int[] think(int[] zahlen) throws RobotException;
}

View File

@ -3,16 +3,7 @@ package tpe.facade;
import tpe.exceptions.roboter.C3PO;
import tpe.exceptions.roboter.R2D2;
public class RobotFactory {
private R2D2 r2d2;
private C3PO c3po;
enum RobotType{
R2D2,
C3PO,
NEXUS6
}
public int constructRobot() {
}
public class FactorySystem {
}