From b6150d2254f495b941b13b67eabc6ef4c57d1f07 Mon Sep 17 00:00:00 2001 From: Stefan <31254851+piregler@users.noreply.github.com> Date: Mon, 26 Dec 2022 17:06:42 +0100 Subject: [PATCH] random serial number generating implemented --- src/Main.java | 20 +++++++++-- .../informatik/name/domain/RobotFactory.java | 36 +++++++++++++++++-- .../informatik/name/domain/starwars/C3PO.java | 1 + .../informatik/name/domain/starwars/R2D2.java | 1 + 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3e59c38..3430513 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,21 @@ +import de.hsmannheim.informatik.name.domain.RobotFactory; +import de.hsmannheim.informatik.name.domain.RobotType; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import java.util.Random; + public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main(String[] args) throws RobotIllegalStateException { + Random r = new Random(); + RobotFactory rf = new RobotFactory(); + RobotType R2D2 = RobotType.R2D2; + RobotType C3P0 = RobotType.C3P0; + for (int i = 0; i < 10000; i++) { + rf.getRobot(R2D2, 0); + } + System.out.println("finished R2D2"); + for (int i = 0; i < 10000; i++) { + rf.getRobot(C3P0, 0); + } + System.out.println("finished C3P0"); } } \ No newline at end of file diff --git a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java index aae92fa..e14e2f8 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java @@ -5,6 +5,10 @@ import de.hsmannheim.informatik.name.domain.requirements.Robot; import de.hsmannheim.informatik.name.domain.starwars.C3PO; import de.hsmannheim.informatik.name.domain.starwars.R2D2; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + /** *

RobotFactory

*

@@ -13,6 +17,8 @@ import de.hsmannheim.informatik.name.domain.starwars.R2D2; * @see RobotType */ public class RobotFactory { + private Set serialNumbersR2D2 = new HashSet<>(); + private Set serialNumbersC3P0 = new HashSet<>(); /** * Factory Method to get a {@link Robot} @@ -23,12 +29,38 @@ public class RobotFactory { * @throws RobotIllegalStateException the robot initializes in an illegal state */ public Robot getRobot(RobotType type, int serialNumber) throws RobotIllegalStateException { + Random r = new Random(); + boolean usedSerialNumber = true; switch (type) { case C3P0 -> { - return new C3PO(serialNumber); + int serialNumberC3P0; + do{ +// String temp = String.format("%04d", r.nextInt(1000)+10000); + serialNumberC3P0 = r.nextInt(10000)+10000; + if(!serialNumbersC3P0.contains(serialNumberC3P0)){ + serialNumbersC3P0.add(serialNumberC3P0); + usedSerialNumber = false; + } + }while(usedSerialNumber); + //Debug output + System.out.println(serialNumberC3P0); + // + return new C3PO(serialNumberC3P0); } case R2D2 -> { - return new R2D2(serialNumber); + int serialNumberR2D2; + do{ + String temp = String.format("%05d", r.nextInt(10000)); + serialNumberR2D2 = Integer.parseInt(temp); + if(!serialNumbersR2D2.contains(serialNumberR2D2)){ + serialNumbersR2D2.add(serialNumberR2D2); + usedSerialNumber = false; + } + }while(usedSerialNumber); + //Debug output + System.out.println(serialNumberR2D2); + // + return new R2D2(serialNumberR2D2); } default -> { return null; diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java index 3843f6b..cd99b3a 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java @@ -24,6 +24,7 @@ public class C3PO extends StarWarsRobot { /** * C3PO think method sorts numbers with insertion sort in descending order + * * @param numbers integer numbers for sorting * @return arrToSort integer array with numbers sorted in descending order * @throws RobotException diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java index 7650119..855bbd8 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java @@ -24,6 +24,7 @@ public class R2D2 extends StarWarsRobot { /** * R2D2 think method sorts numbers with selection sort in ascending order + * * @param numbers integer numbers for sorting * @return arrayToSort integer array with numbers sorted in ascending order * @throws RobotException