diff --git a/Roboter/tpe/exceptions/roboter/Nexus6.java b/Roboter/tpe/exceptions/roboter/Nexus6.java index 9dae737..8d3b462 100644 --- a/Roboter/tpe/exceptions/roboter/Nexus6.java +++ b/Roboter/tpe/exceptions/roboter/Nexus6.java @@ -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() { diff --git a/Roboter/tpe/exceptions/roboter/R2D2.java b/Roboter/tpe/exceptions/roboter/R2D2.java index cfc3609..118f5ed 100644 --- a/Roboter/tpe/exceptions/roboter/R2D2.java +++ b/Roboter/tpe/exceptions/roboter/R2D2.java @@ -6,56 +6,22 @@ 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; - } - - @Override - public String getName() { - return name; - } - - @Override - public void triggerPowerSwitch() { - if(powerSwitch=false) - powerSwitch=true; - else - powerSwitch=false; - } - - @Override - public boolean isPowerOn() { - return powerSwitch; + robotType= RobotType.R2D2; } - 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; + } } diff --git a/Roboter/tpe/exceptions/roboter/RobotFactory.java b/Roboter/tpe/exceptions/roboter/RobotFactory.java index 89e3cd0..834a29c 100644 --- a/Roboter/tpe/exceptions/roboter/RobotFactory.java +++ b/Roboter/tpe/exceptions/roboter/RobotFactory.java @@ -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 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; + } } diff --git a/Roboter/tpe/exceptions/roboter/RobotType.java b/Roboter/tpe/exceptions/roboter/RobotType.java new file mode 100644 index 0000000..7aa1978 --- /dev/null +++ b/Roboter/tpe/exceptions/roboter/RobotType.java @@ -0,0 +1,9 @@ +package tpe.exceptions.roboter; + +public enum RobotType { + + R2D2, + C3PO, + NEXUS6 + +} diff --git a/Roboter/tpe/exceptions/roboter/Robots.java b/Roboter/tpe/exceptions/roboter/Robots.java new file mode 100644 index 0000000..61e03e5 --- /dev/null +++ b/Roboter/tpe/exceptions/roboter/Robots.java @@ -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; +} diff --git a/Roboter/tpe/facade/FactorySystem.java b/Roboter/tpe/facade/FactorySystem.java index 080ed7a..d9da89a 100644 --- a/Roboter/tpe/facade/FactorySystem.java +++ b/Roboter/tpe/facade/FactorySystem.java @@ -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 { + }