package Domäne; import java.util.HashMap; import tpe.exceptions.roboter.exceptions.RobotException; public class RobotFactory { private String name; private HashMap roboterLager = new HashMap<>(); public RobotFactory (String name) { this.name = name; } public int addRobot (RobotType robotType, String name) { Roboter roboter; if (RobotType.R2D2 == robotType) { roboter = new R2D2 (name, idVergeben(0,9999)); roboterLager.put(roboter.getId(), roboter); return roboter.getId(); } else if (RobotType.C3PO == robotType) { roboter = new C3PO (name,idVergeben(10000,19999)); roboterLager.put(roboter.getId(), roboter); return roboter.getId(); } else return -1; } public String getName() { return 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; } public boolean roboterZustand(int id) { Roboter r = findeRoboter(id); boolean zustand = r.isPowerOn(); return zustand; } public boolean schalterAnAus(int id){ Roboter r = findeRoboter(id); r.triggerPowerSwitch(); boolean zustand = r.isPowerOn(); return zustand; } public String aufrufSpeakAndThink (int id, int [] zahlen) throws RobotException { Roboter r = findeRoboter(id); int [] sotiert = r.think(zahlen); String ausgabe = r.speak(sotiert); return ausgabe; } public RobotException letzterFehler(int id) { Roboter r = findeRoboter(id); return r.getLastException(); } }