random serial number generating implemented

main
Stefan 2022-12-26 17:06:42 +01:00
parent f8ce7fa3e8
commit b6150d2254
4 changed files with 54 additions and 4 deletions

View File

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

View File

@ -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;
/**
* <h1>RobotFactory</h1>
* <p>
@ -13,6 +17,8 @@ import de.hsmannheim.informatik.name.domain.starwars.R2D2;
* @see RobotType
*/
public class RobotFactory {
private Set<Integer> serialNumbersR2D2 = new HashSet<>();
private Set<Integer> 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;

View File

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

View File

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