Kleine Änderungen in der RobotFacory und Factorysystem. idVergeben() ist

jetzt in der RobotFactory.
master
Milan Lukic 2023-01-05 19:55:17 +01:00
parent f276084c7e
commit 04c6dc90f1
2 changed files with 17 additions and 18 deletions

View File

@ -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;
}
}

View File

@ -10,7 +10,7 @@ import Domäne.Roboter;
public class Factorysystem {
private RobotFactory robotFactory;
public HashMap<Integer, Roboter> 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;
}
}