From 04c6dc90f16b2f10497aa34d4c07bdfd8cfb1bb2 Mon Sep 17 00:00:00 2001 From: Milan Lukic <2122158@stud.hs-mannheim.de> Date: Thu, 5 Jan 2023 19:55:17 +0100 Subject: [PATCH] =?UTF-8?q?Kleine=20=C3=84nderungen=20in=20der=20RobotFaco?= =?UTF-8?q?ry=20und=20Factorysystem.=20idVergeben()=20ist=20jetzt=20in=20d?= =?UTF-8?q?er=20RobotFactory.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Roboterfabrik/src/Domäne/RobotFactory.java | 15 ++++++++++++--- Roboterfabrik/src/facade/Factorysystem.java | 20 +++++--------------- 2 files changed, 17 insertions(+), 18 deletions(-) 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; - } + }