Robotfactory bugfix

main
Stefan 2022-12-28 02:04:25 +01:00
parent 5cc917bbd3
commit 8705cf23ea
3 changed files with 9 additions and 8 deletions

View File

@ -10,12 +10,13 @@ public class Main {
RobotType R2D2 = RobotType.R2D2; RobotType R2D2 = RobotType.R2D2;
RobotType C3P0 = RobotType.C3P0; RobotType C3P0 = RobotType.C3P0;
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
rf.getRobot(R2D2, 0); rf.getRobot(R2D2, "TestR2D2");
} }
System.out.println("finished R2D2"); System.out.println("finished R2D2");
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
rf.getRobot(C3P0, 0); rf.getRobot(C3P0, "TestC3P0");
} }
System.out.println("finished C3P0"); System.out.println("finished C3P0");
System.out.println("finished");
} }
} }

View File

@ -17,8 +17,8 @@ import java.util.Set;
* @see RobotType * @see RobotType
*/ */
public class RobotFactory { public class RobotFactory {
private Set<Integer> serialNumbersR2D2 = new HashSet<>(); private static Set<Integer> serialNumbersR2D2 = new HashSet<>();
private Set<Integer> serialNumbersC3P0 = new HashSet<>(); private static Set<Integer> serialNumbersC3P0 = new HashSet<>();
/** /**
* Factory Method to get a {@link Robot} * Factory Method to get a {@link Robot}
@ -28,7 +28,7 @@ public class RobotFactory {
* @return a {@link Robot robot instance} of specified type, if no type matches {@code null} * @return a {@link Robot robot instance} of specified type, if no type matches {@code null}
* @throws RobotIllegalStateException the robot initializes in an illegal state * @throws RobotIllegalStateException the robot initializes in an illegal state
*/ */
public Robot getRobot(RobotType type, int serialNumber) throws RobotIllegalStateException { public Robot getRobot(RobotType type, String name) throws RobotIllegalStateException {
Random r = new Random(); Random r = new Random();
boolean usedSerialNumber = true; boolean usedSerialNumber = true;
switch (type) { switch (type) {
@ -53,7 +53,7 @@ public class RobotFactory {
usedSerialNumber = false; usedSerialNumber = false;
} }
}while(usedSerialNumber); }while(usedSerialNumber);
return new R2D2(serialNumberR2D2); return new R2D2(serialNumberR2D2, name);
} }
default -> { default -> {
return null; return null;

View File

@ -13,8 +13,8 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
* @see StarWarsRobot * @see StarWarsRobot
*/ */
public class R2D2 extends StarWarsRobot { public class R2D2 extends StarWarsRobot {
public R2D2(int id) throws RobotIllegalStateException { public R2D2(int id, String name) throws RobotIllegalStateException {
super(',', id, "R2D2"); super(',', id, name);
} }
@Override @Override