diff --git a/Roboterfabrik/src/Domäne/RobotFactory.java b/Roboterfabrik/src/Domäne/RobotFactory.java index 46f63dd..6ad3a9d 100644 --- a/Roboterfabrik/src/Domäne/RobotFactory.java +++ b/Roboterfabrik/src/Domäne/RobotFactory.java @@ -10,16 +10,16 @@ public RobotFactory (String name) { this.name = name; } - public int addRobot (RobotType robotType, String name, int id) { + public int addRobot (RobotType robotType, String name) { Roboter roboter; if (RobotType.R2D2 == robotType) { - roboter = new R2D2 (name, id); + roboter = new R2D2 (name, idVergeben(0,9999)); roboterLager.put(roboter.getId(), roboter); return roboter.getId(); } else if (RobotType.C3PO == robotType) { - roboter = new C3PO (name,id); + roboter = new C3PO (name,idVergeben(10000,19999)); roboterLager.put(roboter.getId(), roboter); return roboter.getId(); @@ -35,4 +35,13 @@ public RobotFactory (String name) { public Roboter findeRoboter (int id) { return roboterLager.get(id); } + private int idVergeben(int min, int max) { + + int randomValue = (int) (Math.random()*(max - min)) + min; + if (roboterLager.containsKey(randomValue)) { + idVergeben(min, max); + } + + return randomValue; + } } diff --git a/Roboterfabrik/src/facade/Factorysystem.java b/Roboterfabrik/src/facade/Factorysystem.java index 8c31307..7e6129e 100644 --- a/Roboterfabrik/src/facade/Factorysystem.java +++ b/Roboterfabrik/src/facade/Factorysystem.java @@ -10,7 +10,7 @@ import Domäne.Roboter; public class Factorysystem { private RobotFactory robotFactory; - public HashMap roboterLager = new HashMap<>(); + public Factorysystem (String name) { this.robotFactory = new RobotFactory (name); @@ -20,26 +20,16 @@ public class Factorysystem { RobotType robottype; if(auswahl == 1) { robottype = RobotType.R2D2; - int id = idVergeben(0, 9999); - robotFactory.addRobot(robottype, name, id); + int id = robotFactory.addRobot(robottype, name); return id; } else if(auswahl == 2) { robottype = RobotType.C3PO; - int id = idVergeben(10000, 19999); - robotFactory.addRobot(robottype, name, id); + int id = robotFactory.addRobot(robottype, name); return id; } - roboterLager.put(id, r); + return -1; } - private int idVergeben(int min, int max) { - - int randomValue = (int) (Math.random()*(max - min)) + min; - if (roboterLager.containsKey(randomValue)) { - idVergeben(min, max); - } - - return randomValue; - } + }