From f276084c7e884cad7ff8fddbc4778ee28acb5e2e Mon Sep 17 00:00:00 2001 From: Milan Lukic <2122158@stud.hs-mannheim.de> Date: Thu, 5 Jan 2023 19:44:43 +0100 Subject: [PATCH] Eine Facade als Factorysystem als Klassennamen erstellt. idVergabe dahin verschoben. Methode roboterAnlegen in der Facade erstellt. --- Roboterfabrik/src/Domäne/RobotFactory.java | 14 +++---- Roboterfabrik/src/facade/Factorysystem.java | 45 +++++++++++++++++++++ Roboterfabrik/src/ui/Factory.java | 36 ++++++----------- 3 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 Roboterfabrik/src/facade/Factorysystem.java diff --git a/Roboterfabrik/src/Domäne/RobotFactory.java b/Roboterfabrik/src/Domäne/RobotFactory.java index ace98bc..46f63dd 100644 --- a/Roboterfabrik/src/Domäne/RobotFactory.java +++ b/Roboterfabrik/src/Domäne/RobotFactory.java @@ -4,23 +4,23 @@ import java.util.HashMap; public class RobotFactory { private String name; -private HashMap roboters = new HashMap<>(); +private HashMap roboterLager = new HashMap<>(); public RobotFactory (String name) { this.name = name; } - public int addRobot (RobotType robotType, String name) { + public int addRobot (RobotType robotType, String name, int id) { Roboter roboter; if (RobotType.R2D2 == robotType) { - roboter = new R2D2 (name); - roboters.put(roboter.getId(), roboter); + roboter = new R2D2 (name, id); + roboterLager.put(roboter.getId(), roboter); return roboter.getId(); } else if (RobotType.C3PO == robotType) { - roboter = new C3PO (name); - roboters.put(roboter.getId(), roboter); + roboter = new C3PO (name,id); + roboterLager.put(roboter.getId(), roboter); return roboter.getId(); } @@ -33,6 +33,6 @@ public RobotFactory (String name) { } public Roboter findeRoboter (int id) { - return roboters.get(id); + return roboterLager.get(id); } } diff --git a/Roboterfabrik/src/facade/Factorysystem.java b/Roboterfabrik/src/facade/Factorysystem.java new file mode 100644 index 0000000..8c31307 --- /dev/null +++ b/Roboterfabrik/src/facade/Factorysystem.java @@ -0,0 +1,45 @@ +package facade; + +import java.util.HashMap; + +import Domäne.C3PO; +import Domäne.R2D2; +import Domäne.RobotFactory; +import Domäne.RobotType; +import Domäne.Roboter; + +public class Factorysystem { + private RobotFactory robotFactory; + public HashMap roboterLager = new HashMap<>(); + + public Factorysystem (String name) { + this.robotFactory = new RobotFactory (name); + } + + public int roboterAnlegen (String name, int auswahl) { + RobotType robottype; + if(auswahl == 1) { + robottype = RobotType.R2D2; + int id = idVergeben(0, 9999); + robotFactory.addRobot(robottype, name, id); + return id; + } else if(auswahl == 2) { + robottype = RobotType.C3PO; + int id = idVergeben(10000, 19999); + robotFactory.addRobot(robottype, name, id); + return id; + } + roboterLager.put(id, r); + + } + + private int idVergeben(int min, int max) { + + int randomValue = (int) (Math.random()*(max - min)) + min; + if (roboterLager.containsKey(randomValue)) { + idVergeben(min, max); + } + + return randomValue; + } +} diff --git a/Roboterfabrik/src/ui/Factory.java b/Roboterfabrik/src/ui/Factory.java index 98c8950..0ee2b2f 100644 --- a/Roboterfabrik/src/ui/Factory.java +++ b/Roboterfabrik/src/ui/Factory.java @@ -4,15 +4,17 @@ import java.util.Scanner; import Domäne.C3PO; import Domäne.R2D2; -import Domäne.Roboter; +import facade.Factorysystem; + -import java.util.HashMap; public class Factory { Scanner sc = new Scanner(System.in); - public HashMap roboterLager = new HashMap<>(); + private Factorysystem factorysystem; + - public Factory() { + public Factory(Factorysystem factorysystem) { + this.factorysystem = factorysystem; hauptmenü(); } @@ -55,34 +57,22 @@ 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()); System.out.println("Wie wollen Sie ihren Roboter nennen?"); String name = sc.nextLine(); - if(auswahl == 1) { - 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); + int seriennummer = factorysystem.roboterAnlegen(name, auswahl); + System.out.println(name + " mit der Seriennummer: " + seriennummer + " wurde erstellt."); + + + + } - private int idVergeben(int min, int max) { - - int randomValue = (int) (Math.random()*(max - min)) + min; - if (roboterLager.containsKey(randomValue)) { - idVergeben(min, max); - } - - return randomValue; - } + } \ No newline at end of file