RoboterFabrik/Roboter/tpe/exceptions/roboter/RobotFactory.java

42 lines
1.0 KiB
Java
Raw Normal View History

package tpe.exceptions.roboter;
2023-01-07 18:48:00 +01:00
import java.util.HashMap;
public class RobotFactory {
2023-01-07 18:48:00 +01:00
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) {
2023-01-08 09:54:17 +01:00
robot=new R2D2(name, createIDR2D2(0,9999));
2023-01-07 18:48:00 +01:00
robotStock.put(robot.getId(), robot);
return robot.getId();
}else if(RobotType.C3PO==robotType) {
2023-01-08 09:54:17 +01:00
robot=new C3PO(name, createIDC3PO(10000, 19999));
2023-01-07 18:48:00 +01:00
robotStock.put(robot.getId(), robot);
return robot.getId();
}
}
2023-01-08 09:54:17 +01:00
private int createIDR2D2(int minValue, int maxValue) {
2023-01-07 18:48:00 +01:00
2023-01-08 09:54:17 +01:00
int randomID = (int) (Math.random()*(maxValue)) ;
2023-01-07 18:48:00 +01:00
if(robotStock.containsKey(randomID))
{
2023-01-08 09:54:17 +01:00
createIDR2D2(minValue,maxValue);
}
return randomID;
}
private int createIDC3PO(int minValue, int maxValue) {
int randomID = (int) (Math.random()*(maxValue))+minValue ;
if(robotStock.containsKey(randomID))
{
createIDC3PO(minValue,maxValue);
2023-01-07 18:48:00 +01:00
}
return randomID;
}
}