parent
91e1c5afa0
commit
87815a7318
|
@ -5,17 +5,13 @@ import tpe.exceptions.roboter.exceptions.RobotIllegalStateException;
|
|||
import tpe.exceptions.roboter.exceptions.RobotMagicValueException;
|
||||
|
||||
public class C3PO extends Roboter {
|
||||
private String name;
|
||||
private int id;
|
||||
//static int idZähler = 10000;
|
||||
private RobotType robotType;
|
||||
//RobotException fehler;
|
||||
|
||||
public C3PO(String name, int id) {
|
||||
super(name);
|
||||
this.robotType = robotType.C3PO;
|
||||
this.robotType = RobotType.C3PO;
|
||||
this.id = id;
|
||||
//idZähler++;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -51,9 +47,9 @@ public class C3PO extends Roboter {
|
|||
throw fehler;
|
||||
|
||||
}
|
||||
public RobotType getRobotType () {
|
||||
|
||||
public RobotType getRobotType() {
|
||||
return this.robotType;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -7,61 +7,53 @@ public class Nexus6 extends Roboter {
|
|||
|
||||
private String name;
|
||||
private int id;
|
||||
//RobotException fehler;
|
||||
private static Nexus6 PRIS;
|
||||
private RobotType robotType;
|
||||
|
||||
|
||||
private Nexus6() {
|
||||
super("Pris");
|
||||
this.id = 19_281_982;
|
||||
robotType = RobotType.NEXUS6;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Nexus6 getInstance () {
|
||||
public static Nexus6 getInstance() {
|
||||
if (PRIS == null) {
|
||||
PRIS = new Nexus6();
|
||||
|
||||
PRIS = new Nexus6();
|
||||
|
||||
}
|
||||
return PRIS;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void triggerPowerSwitch() {
|
||||
power = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
//Methode soll Fehler ausgeben, da der Roboter nicht Funktioniert.
|
||||
|
||||
// Methode soll Fehler ausgeben, da der Roboter nicht Funktioniert.
|
||||
@Override
|
||||
public String speak(int[] zahlen) throws RobotException {
|
||||
fehler = new RobotIllegalStateException ("Roboter ausgeschaltet und Defekt!", this.name);
|
||||
throw fehler;
|
||||
|
||||
fehler = new RobotIllegalStateException("Roboter ausgeschaltet und Defekt!", this.name);
|
||||
throw fehler;
|
||||
|
||||
}
|
||||
|
||||
//Methode soll Fehler ausgeben, da der Roboter nicht Funktioniert.
|
||||
|
||||
// Methode soll Fehler ausgeben, da der Roboter nicht Funktioniert.
|
||||
@Override
|
||||
public int[] think(int[] zahlen) throws RobotException {
|
||||
fehler = new RobotIllegalStateException ("Roboter ausgeschaltet und Defekt!", this.name);
|
||||
fehler = new RobotIllegalStateException("Roboter ausgeschaltet und Defekt!", this.name);
|
||||
throw fehler;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public RobotType getRobotType() {
|
||||
|
||||
|
||||
return this.robotType;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,23 +1,17 @@
|
|||
package Domäne;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import tpe.exceptions.roboter.Robot;
|
||||
import tpe.exceptions.roboter.exceptions.RobotException;
|
||||
import tpe.exceptions.roboter.exceptions.RobotIllegalStateException;
|
||||
import tpe.exceptions.roboter.exceptions.RobotMagicValueException;
|
||||
|
||||
public class R2D2 extends Roboter {
|
||||
private int id;
|
||||
//private static int idZähler = 0;
|
||||
private RobotType robotType;
|
||||
//RobotException fehler;
|
||||
|
||||
public R2D2(String name, int id) {
|
||||
super(name);
|
||||
this.robotType = RobotType.R2D2;
|
||||
this.id = id;
|
||||
//idZähler++;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -38,7 +32,7 @@ public class R2D2 extends Roboter {
|
|||
zahlen[k] = größer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return zahlen;
|
||||
} else
|
||||
|
@ -50,7 +44,8 @@ public class R2D2 extends Roboter {
|
|||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public RobotType getRobotType () {
|
||||
|
||||
public RobotType getRobotType() {
|
||||
return this.robotType;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,84 +5,85 @@ import java.util.HashMap;
|
|||
import tpe.exceptions.roboter.exceptions.RobotException;
|
||||
|
||||
public class RobotFactory {
|
||||
private String name;
|
||||
private Roboter pris = Nexus6.getInstance();
|
||||
private HashMap <Integer, Roboter> roboterLager = new HashMap<>();
|
||||
private String name;
|
||||
private Roboter pris = Nexus6.getInstance();
|
||||
private HashMap<Integer, Roboter> roboterLager = new HashMap<>();
|
||||
|
||||
public RobotFactory (String name) {
|
||||
this.name = name;
|
||||
roboterLager.put(pris.getId(), pris);
|
||||
}
|
||||
public RobotFactory(String name) {
|
||||
this.name = name;
|
||||
roboterLager.put(pris.getId(), pris);
|
||||
}
|
||||
|
||||
public int addRobot (RobotType robotType, String name) {
|
||||
public int addRobot(RobotType robotType, String name) {
|
||||
Roboter roboter;
|
||||
|
||||
|
||||
if (RobotType.R2D2 == robotType) {
|
||||
roboter = new R2D2 (name, idVergeben(0,9999));
|
||||
roboter = new R2D2(name, idVergeben(0, 9999));
|
||||
roboterLager.put(roboter.getId(), roboter);
|
||||
return roboter.getId();
|
||||
}
|
||||
else if (RobotType.C3PO == robotType) {
|
||||
roboter = new C3PO (name,idVergeben(10000,19999));
|
||||
} else if (RobotType.C3PO == robotType) {
|
||||
roboter = new C3PO(name, idVergeben(10000, 19999));
|
||||
roboterLager.put(roboter.getId(), roboter);
|
||||
return roboter.getId();
|
||||
|
||||
}
|
||||
else
|
||||
|
||||
} else
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Roboter findeRoboter (int id) {
|
||||
|
||||
public Roboter findeRoboter(int id) {
|
||||
return roboterLager.get(id);
|
||||
}
|
||||
|
||||
private int idVergeben(int min, int max) {
|
||||
|
||||
int randomValue = (int) (Math.random()*(max - min)) + min;
|
||||
|
||||
int randomValue = (int) (Math.random() * (max - min)) + min;
|
||||
if (roboterLager.containsKey(randomValue)) {
|
||||
idVergeben(min, max);
|
||||
}
|
||||
|
||||
|
||||
return randomValue;
|
||||
}
|
||||
|
||||
|
||||
public boolean roboterZustand(int id) {
|
||||
Roboter r = findeRoboter(id);
|
||||
boolean zustand = r.isPowerOn();
|
||||
return zustand;
|
||||
}
|
||||
public boolean schalterAnAus(int id){
|
||||
|
||||
public boolean schalterAnAus(int id) {
|
||||
Roboter r = findeRoboter(id);
|
||||
r.triggerPowerSwitch();
|
||||
boolean zustand = r.isPowerOn();
|
||||
return zustand;
|
||||
}
|
||||
|
||||
public String aufrufSpeakAndThink (int id, int [] zahlen) throws RobotException {
|
||||
|
||||
public String aufrufSpeakAndThink(int id, int[] zahlen) throws RobotException {
|
||||
Roboter r = findeRoboter(id);
|
||||
int [] sotiert = r.think(zahlen);
|
||||
int[] sotiert = r.think(zahlen);
|
||||
String ausgabe = r.speak(sotiert);
|
||||
return ausgabe;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public RobotException letzterFehler(int id) {
|
||||
Roboter r = findeRoboter(id);
|
||||
return r.getLastException();
|
||||
}
|
||||
|
||||
public String datenDesRoboters (int id) {
|
||||
|
||||
public String datenDesRoboters(int id) {
|
||||
Roboter r = findeRoboter(id);
|
||||
String robotType= r.getRobotType().toString();
|
||||
String robotType = r.getRobotType().toString();
|
||||
String name = r.getName();
|
||||
int seriennummer = r.getId();
|
||||
String ausgabe = "RoboterType: " + robotType + "; Name: " +name + "; Seriennummer: " + seriennummer;
|
||||
int seriennummer = r.getId();
|
||||
String ausgabe = "RoboterType: " + robotType + "; Name: " + name + "; Seriennummer: " + seriennummer;
|
||||
return ausgabe;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int getRoboterLagerSize() {
|
||||
return roboterLager.size();
|
||||
}
|
||||
|
|
|
@ -9,47 +9,47 @@ import tpe.exceptions.roboter.exceptions.RobotException;
|
|||
|
||||
class RobotFactoryTest {
|
||||
private static RobotFactory rf;
|
||||
|
||||
|
||||
@BeforeAll
|
||||
static void initRobotFactory() {
|
||||
rf = new RobotFactory ("Test");
|
||||
rf = new RobotFactory("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void smokeTest() {
|
||||
assertNotNull(rf);
|
||||
assertEquals(1, rf.getRoboterLagerSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@Test
|
||||
void addRoboterTest() {
|
||||
int id = rf.addRobot(RobotType.R2D2, "Test");
|
||||
assertEquals(2, rf.getRoboterLagerSize());
|
||||
int id2 = rf.addRobot(RobotType.C3PO, "Test2");
|
||||
assertEquals(3, rf.getRoboterLagerSize());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void zustandTest() {
|
||||
int id = rf.addRobot(RobotType.R2D2, "Test");
|
||||
assertFalse(rf.roboterZustand(id));
|
||||
rf.schalterAnAus(id);
|
||||
assertTrue(rf.roboterZustand(id));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void aufrufSpeakAndThinkTest() throws RobotException {
|
||||
int id = rf.addRobot(RobotType.R2D2, "Test");
|
||||
rf.schalterAnAus(id);
|
||||
int [] zahlen = {5,8,4,3,7};
|
||||
assertEquals("3, 4, 5, 7, 8, ",rf.aufrufSpeakAndThink(id, zahlen));
|
||||
int[] zahlen = { 5, 8, 4, 3, 7 };
|
||||
assertEquals("3, 4, 5, 7, 8, ", rf.aufrufSpeakAndThink(id, zahlen));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@Test
|
||||
void datenDesRobotersTest() {
|
||||
int id = rf.addRobot(RobotType.R2D2, "Test");
|
||||
assertEquals("RoboterType: " + "R2D2" + "; Name: " +"Test" + "; Seriennummer: " + id, rf.datenDesRoboters(id));
|
||||
assertEquals("RoboterType: " + "R2D2" + "; Name: " + "Test" + "; Seriennummer: " + id, rf.datenDesRoboters(id));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package Domäne;
|
||||
|
||||
public enum RobotType {
|
||||
R2D2, C3PO, NEXUS6
|
||||
R2D2, C3PO, NEXUS6
|
||||
}
|
||||
|
|
|
@ -90,7 +90,6 @@ public abstract class Roboter implements Robot {
|
|||
|
||||
@Override
|
||||
public abstract int[] think(int[] zahlen) throws RobotException;
|
||||
|
||||
|
||||
public abstract RobotType getRobotType();
|
||||
}
|
||||
|
||||
|
|
|
@ -13,11 +13,12 @@ class RoboterTest {
|
|||
Roboter roboter = new R2D2("Test", 1);
|
||||
Roboter robi = new C3PO("Test2", 10000);
|
||||
assertEquals(roboter.getName(), "Test");
|
||||
assertEquals(1,roboter.getId());
|
||||
assertEquals(1, roboter.getId());
|
||||
assertEquals(robi.getName(), "Test2");
|
||||
assertEquals(10000, robi.getId());
|
||||
}
|
||||
@Test
|
||||
|
||||
@Test
|
||||
void triggerPowerSwitchTest() {
|
||||
Roboter r2d2 = new R2D2("Test", 1);
|
||||
Roboter c3po = new C3PO("Test", 15000);
|
||||
|
@ -29,18 +30,21 @@ class RoboterTest {
|
|||
c3po.triggerPowerSwitch();
|
||||
assertTrue(r2d2.isPowerOn());
|
||||
assertTrue(c3po.isPowerOn());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLastExceptionTest() {
|
||||
Roboter r2d2 = new R2D2("Test", 1);
|
||||
int [] zahlen = {5,4,8,1};
|
||||
int[] zahlen = { 5, 4, 8, 1 };
|
||||
assertNull(r2d2.getLastException());
|
||||
try {
|
||||
r2d2.think(zahlen);
|
||||
} catch (RobotException e) {
|
||||
} assertNotNull(r2d2.getLastException());
|
||||
}
|
||||
assertNotNull(r2d2.getLastException());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nexus6SingletonTest() {
|
||||
Roboter pris = Nexus6.getInstance();
|
||||
|
@ -50,6 +54,6 @@ class RoboterTest {
|
|||
assertFalse(pris.isPowerOn());
|
||||
pris.triggerPowerSwitch();
|
||||
assertFalse(pris.isPowerOn());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,56 +1,50 @@
|
|||
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;
|
||||
import tpe.exceptions.roboter.exceptions.RobotException;
|
||||
|
||||
public class Factorysystem {
|
||||
private RobotFactory robotFactory;
|
||||
|
||||
|
||||
public Factorysystem (String name) {
|
||||
this.robotFactory = new RobotFactory (name);
|
||||
public Factorysystem(String name) {
|
||||
this.robotFactory = new RobotFactory(name);
|
||||
}
|
||||
|
||||
public int roboterAnlegen (String name, int auswahl) {
|
||||
|
||||
public int roboterAnlegen(String name, int auswahl) {
|
||||
RobotType robottype;
|
||||
if(auswahl == 1) {
|
||||
if (auswahl == 1) {
|
||||
robottype = RobotType.R2D2;
|
||||
int id = robotFactory.addRobot(robottype, name);
|
||||
return id;
|
||||
} else if(auswahl == 2) {
|
||||
} else if (auswahl == 2) {
|
||||
robottype = RobotType.C3PO;
|
||||
int id = robotFactory.addRobot(robottype, name);
|
||||
return id;
|
||||
}
|
||||
return -1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public boolean zustandRoboter (int id) {
|
||||
|
||||
public boolean zustandRoboter(int id) {
|
||||
return robotFactory.roboterZustand(id);
|
||||
}
|
||||
|
||||
public boolean schalterBetätigen(int id) {
|
||||
return robotFactory.schalterAnAus(id);
|
||||
}
|
||||
|
||||
|
||||
public String sprechenAufruf(int id, int[] zahlen) throws RobotException {
|
||||
String ausgabe = robotFactory.aufrufSpeakAndThink(id, zahlen);
|
||||
return ausgabe;
|
||||
}
|
||||
|
||||
|
||||
public RobotException fehlerAuslesen(int id) {
|
||||
|
||||
|
||||
return robotFactory.letzterFehler(id);
|
||||
}
|
||||
|
||||
public String roboterDaten (int id) {
|
||||
|
||||
public String roboterDaten(int id) {
|
||||
return robotFactory.datenDesRoboters(id);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,64 +4,66 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import tpe.exceptions.roboter.exceptions.RobotException;
|
||||
import tpe.exceptions.roboter.exceptions.RobotIllegalStateException;
|
||||
|
||||
class SystemTest {
|
||||
|
||||
private static Factorysystem fs;
|
||||
|
||||
private static Factorysystem fs;
|
||||
|
||||
@BeforeAll
|
||||
static void initFactorysystem() {
|
||||
fs = new Factorysystem("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void smokeTest() {
|
||||
assertNotNull(fs);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void roboterAnlegenTest() {
|
||||
int id = fs.roboterAnlegen("Test", 1);
|
||||
int id2 = fs.roboterAnlegen("Test2", 2);
|
||||
assertEquals("RoboterType: " + "R2D2" + "; Name: " +"Test" + "; Seriennummer: " + id,fs.roboterDaten(id));
|
||||
assertEquals("RoboterType: " + "C3PO" + "; Name: " +"Test2" + "; Seriennummer: " + id2,fs.roboterDaten(id2));
|
||||
assertEquals("RoboterType: " + "NEXUS6" + "; Name: " +"Pris" + "; Seriennummer: " + 19281982,fs.roboterDaten(19281982));
|
||||
assertEquals("RoboterType: " + "R2D2" + "; Name: " + "Test" + "; Seriennummer: " + id, fs.roboterDaten(id));
|
||||
assertEquals("RoboterType: " + "C3PO" + "; Name: " + "Test2" + "; Seriennummer: " + id2, fs.roboterDaten(id2));
|
||||
assertEquals("RoboterType: " + "NEXUS6" + "; Name: " + "Pris" + "; Seriennummer: " + 19281982,
|
||||
fs.roboterDaten(19281982));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@Test
|
||||
void zustandTest() {
|
||||
int id = fs.roboterAnlegen("Test", 1);
|
||||
assertFalse(fs.zustandRoboter(id));
|
||||
fs.schalterBetätigen(id);
|
||||
assertTrue(fs.zustandRoboter(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@Test
|
||||
void fehlerAuslesenTest() {
|
||||
int id = fs.roboterAnlegen("Test", 1);
|
||||
assertNull(fs.fehlerAuslesen(id));
|
||||
int [] zahlen = {5,4,6,2,1};
|
||||
int[] zahlen = { 5, 4, 6, 2, 1 };
|
||||
try {
|
||||
fs.sprechenAufruf(id, zahlen);
|
||||
} catch (RobotException e) {
|
||||
} catch (RobotException e) {
|
||||
}
|
||||
assertNotNull(fs.fehlerAuslesen(id));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void sprechenTest() throws RobotException {
|
||||
int id = fs.roboterAnlegen("Test", 1);
|
||||
int id2 = fs.roboterAnlegen("Test2", 2);
|
||||
|
||||
|
||||
fs.schalterBetätigen(id);
|
||||
fs.schalterBetätigen(id2);
|
||||
int [] zahlen = {5,9,1,6};
|
||||
|
||||
int[] zahlen = { 5, 9, 1, 6 };
|
||||
|
||||
assertEquals("1, 5, 6, 9, ", fs.sprechenAufruf(id, zahlen));
|
||||
assertEquals("9; 6; 5; 1; ", fs.sprechenAufruf(id2, zahlen));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ public class RobotException extends Exception {
|
|||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public RobotException (String message) {
|
||||
public RobotException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,12 +7,12 @@ public class RobotMagicValueException extends RobotException {
|
|||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
String name;
|
||||
|
||||
public RobotMagicValueException (String message, String name) {
|
||||
|
||||
public RobotMagicValueException(String message, String name) {
|
||||
super(message);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public String getRobotName() {
|
||||
return this.name;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue