Ids werden jetzt zufällig vergeben und die Roboter werden in

roboterLager gespeichert.
master
nikow 2023-01-05 18:58:08 +01:00
parent 4ec9407bfe
commit 28af833e03
3 changed files with 18 additions and 7 deletions

View File

@ -11,7 +11,7 @@ public class C3PO extends Roboter {
RobotType robotType;
RobotException fehler;
C3PO(String name, int id) {
public C3PO(String name, int id) {
super(name);
this.robotType = robotType.C3PO;
this.id = id;

View File

@ -13,7 +13,7 @@ public class R2D2 extends Roboter {
private RobotType robotType;
RobotException fehler;
R2D2(String name, int id) {
public R2D2(String name, int id) {
super(name);
this.robotType = RobotType.R2D2;
this.id = id;

View File

@ -2,11 +2,11 @@ package ui;
import java.util.Scanner;
import Domäne.C3PO;
import Domäne.R2D2;
import Domäne.Roboter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Random;
public class Factory {
Scanner sc = new Scanner(System.in);
@ -55,6 +55,8 @@ public class Factory {
}
private void roboterBauen() {
Roboter r = null;
int id = 0;
System.out.println("Welchen Robotertyp möchten Sie haben?(R2D2(1) oder C3PO (2)");
int auswahl = Integer.parseInt(sc.nextLine());
@ -62,15 +64,24 @@ public class Factory {
String name = sc.nextLine();
if(auswahl == 1) {
roboterLager.put(idVergeben(0, 9999), R2D2 roboter = new R2D2(name);
id = idVergeben(0, 9999);
r = new R2D2(name, id);
} else if(auswahl == 2) {
id = idVergeben(10000, 19999);
r = new C3PO(name, id);
}
roboterLager.put(id, r);
}
private int idVergeben(int min, int max) {
return 0;
int randomValue = (int) (Math.random()*(max - min)) + min;
if (roboterLager.containsKey(randomValue)) {
idVergeben(min, max);
}
return randomValue;
}