Eine Facade als Factorysystem als Klassennamen erstellt. idVergabe dahin
verschoben. Methode roboterAnlegen in der Facade erstellt.master
parent
28af833e03
commit
f276084c7e
|
@ -4,23 +4,23 @@ import java.util.HashMap;
|
|||
|
||||
public class RobotFactory {
|
||||
private String name;
|
||||
private HashMap <Integer, Roboter> roboters = new HashMap<>();
|
||||
private HashMap <Integer, Roboter> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Integer, Roboter> 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;
|
||||
}
|
||||
}
|
|
@ -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<Integer, Roboter> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue