From 28af833e03b49eaaa9b07c1d4009c4b87c431a39 Mon Sep 17 00:00:00 2001 From: nikow Date: Thu, 5 Jan 2023 18:58:08 +0100 Subject: [PATCH] =?UTF-8?q?Ids=20werden=20jetzt=20zuf=C3=A4llig=20vergeben?= =?UTF-8?q?=20und=20die=20Roboter=20werden=20in=20roboterLager=20gespeiche?= =?UTF-8?q?rt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Roboterfabrik/src/Domäne/C3PO.java | 2 +- Roboterfabrik/src/Domäne/R2D2.java | 2 +- Roboterfabrik/src/ui/Factory.java | 21 ++++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Roboterfabrik/src/Domäne/C3PO.java b/Roboterfabrik/src/Domäne/C3PO.java index 958df77..13318f3 100644 --- a/Roboterfabrik/src/Domäne/C3PO.java +++ b/Roboterfabrik/src/Domäne/C3PO.java @@ -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; diff --git a/Roboterfabrik/src/Domäne/R2D2.java b/Roboterfabrik/src/Domäne/R2D2.java index 4566db0..c336977 100644 --- a/Roboterfabrik/src/Domäne/R2D2.java +++ b/Roboterfabrik/src/Domäne/R2D2.java @@ -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; diff --git a/Roboterfabrik/src/ui/Factory.java b/Roboterfabrik/src/ui/Factory.java index fd8e8af..98c8950 100644 --- a/Roboterfabrik/src/ui/Factory.java +++ b/Roboterfabrik/src/ui/Factory.java @@ -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; }