diff --git a/out/production/RobotAbgabe/roboter/C3PO.class b/out/production/RobotAbgabe/roboter/C3PO.class index 124a63f..d18e1a7 100644 Binary files a/out/production/RobotAbgabe/roboter/C3PO.class and b/out/production/RobotAbgabe/roboter/C3PO.class differ diff --git a/out/production/RobotAbgabe/roboter/Nexus6.class b/out/production/RobotAbgabe/roboter/Nexus6.class index d1e6917..ed8d714 100644 Binary files a/out/production/RobotAbgabe/roboter/Nexus6.class and b/out/production/RobotAbgabe/roboter/Nexus6.class differ diff --git a/out/production/RobotAbgabe/roboter/R2D2.class b/out/production/RobotAbgabe/roboter/R2D2.class index b7ec4b3..51f6cae 100644 Binary files a/out/production/RobotAbgabe/roboter/R2D2.class and b/out/production/RobotAbgabe/roboter/R2D2.class differ diff --git a/out/production/RobotAbgabe/roboter/RobotInstructions.class b/out/production/RobotAbgabe/roboter/RobotInstructions.class index 9963796..59837e5 100644 Binary files a/out/production/RobotAbgabe/roboter/RobotInstructions.class and b/out/production/RobotAbgabe/roboter/RobotInstructions.class differ diff --git a/out/production/RobotAbgabe/roboter/exceptions/RobotMagicValueException.class b/out/production/RobotAbgabe/roboter/exceptions/RobotMagicValueException.class index 7e465aa..22b789f 100644 Binary files a/out/production/RobotAbgabe/roboter/exceptions/RobotMagicValueException.class and b/out/production/RobotAbgabe/roboter/exceptions/RobotMagicValueException.class differ diff --git a/out/test/RobotAbgabe/roboter/ExceptionTest.class b/out/test/RobotAbgabe/roboter/ExceptionTest.class index 66b0e27..8051207 100644 Binary files a/out/test/RobotAbgabe/roboter/ExceptionTest.class and b/out/test/RobotAbgabe/roboter/ExceptionTest.class differ diff --git a/out/test/RobotAbgabe/roboter/R2D2Test.class b/out/test/RobotAbgabe/roboter/R2D2Test.class index aa99257..3a51ee9 100644 Binary files a/out/test/RobotAbgabe/roboter/R2D2Test.class and b/out/test/RobotAbgabe/roboter/R2D2Test.class differ diff --git a/src/main/java/roboter/C3PO.java b/src/main/java/roboter/C3PO.java index 8927458..a69f38e 100644 --- a/src/main/java/roboter/C3PO.java +++ b/src/main/java/roboter/C3PO.java @@ -1,19 +1,25 @@ package roboter; import roboter.exceptions.RobotException; +import roboter.exceptions.RobotIllegalStateException; +import roboter.exceptions.RobotMagicValueException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; public class C3PO implements Robot { private int id; private String name; - private boolean isPowerOn=false; + private boolean isPowerOn = true; + ArrayList exceptionList=new ArrayList<>(); -public C3PO(int id, String name){ - this.id=id; - this.name=name; -} + public C3PO(int id, String name) { + this.id = id; + this.name = name; + } @Override public int getId() { @@ -27,11 +33,11 @@ public C3PO(int id, String name){ @Override public void triggerPowerSwitch() { - if(isPowerOn==false){ - isPowerOn=true; - }else{ - isPowerOn=false; - } + if (isPowerOn == false) { + isPowerOn = true; + } else { + isPowerOn = false; + } } //tes test @@ -42,31 +48,56 @@ public C3PO(int id, String name){ @Override public RobotException getLastException() { + if (!exceptionList.isEmpty()){ + return exceptionList.get(exceptionList.size() - 1); + + } return null; } @Override - public String speak(int[] zahlen) throws RobotException { - + public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { + if(!isPowerOn){ + var ex=new RobotIllegalStateException(this); + exceptionList.add(ex); + throw ex; + } + if (Arrays.stream(zahlen).anyMatch(value -> value ==42)){ + var exc= new RobotMagicValueException(this); + exceptionList.add(exc); + throw exc; + } var sortiert = think(zahlen); return arrayFormatieren(sortiert); } - private String arrayFormatieren(int[] zahlen){ + private String arrayFormatieren(int[] zahlen) throws RobotMagicValueException { + var ergebnis = Arrays.stream(zahlen) .boxed() .map(String::valueOf) .collect(Collectors.joining(";")); return ergebnis; } + @Override public int[] think(int[] zahlen) throws RobotException { + if(!isPowerOn){ + var ex=new RobotIllegalStateException(this); + exceptionList.add(ex); + throw ex; + } + if (Arrays.stream(zahlen).anyMatch(value -> value ==42)){ + var exc= new RobotMagicValueException(this); + exceptionList.add(exc); + throw exc; + } int n = zahlen.length; for (int i = 1; i < n; ++i) { int key = zahlen[i]; int j = i - 1; - while (j >= 0 && zahlen[j] < key) { + while (j >= 0 && zahlen[j] > key) { zahlen[j + 1] = zahlen[j]; j = j - 1; } diff --git a/src/main/java/roboter/Nexus6.java b/src/main/java/roboter/Nexus6.java index 06cff81..516f6f6 100644 --- a/src/main/java/roboter/Nexus6.java +++ b/src/main/java/roboter/Nexus6.java @@ -49,8 +49,12 @@ public class Nexus6 implements Robot { } @Override - public RobotException getLastException() { - return exceptionList.get(exceptionList.size()-1); + public RobotException getLastException() { + if (!exceptionList.isEmpty()){ + return exceptionList.get(exceptionList.size() - 1); + + } + return null; } @Override diff --git a/src/main/java/roboter/R2D2.java b/src/main/java/roboter/R2D2.java index 41de9f8..75aba33 100644 --- a/src/main/java/roboter/R2D2.java +++ b/src/main/java/roboter/R2D2.java @@ -1,14 +1,18 @@ package roboter; import roboter.exceptions.RobotException; +import roboter.exceptions.RobotIllegalStateException; +import roboter.exceptions.RobotMagicValueException; +import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Collectors; public class R2D2 implements Robot { private int id; private String name; - private boolean isPowerOn; + private boolean isPowerOn=true; + ArrayList exceptionList=new ArrayList<>(); public R2D2(int id, String name){ this.id=id; @@ -43,12 +47,25 @@ public class R2D2 implements Robot { @Override public RobotException getLastException() { + if (!exceptionList.isEmpty()){ + return exceptionList.get(exceptionList.size() - 1); + + } return null; } @Override - public String speak(int[] zahlen) throws RobotException { - + public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { + if(!isPowerOn){ + var ex=new RobotIllegalStateException(this); + exceptionList.add(ex); + throw ex; + } + if (Arrays.stream(zahlen).anyMatch(value -> value ==42)){ + var exc= new RobotMagicValueException(this); + exceptionList.add(exc); + throw exc; + } var sortiert = think(zahlen); return arrayFormatieren(sortiert); @@ -62,7 +79,17 @@ public class R2D2 implements Robot { return ergebnis; } @Override - public int[] think(int[] zahlen) throws RobotException { + public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException { + if(!isPowerOn){ + var ex=new RobotIllegalStateException(this); + exceptionList.add(ex); + throw ex; + } + if (Arrays.stream(zahlen).anyMatch(value -> value ==42)){ + var exc= new RobotMagicValueException(this); + exceptionList.add(exc); + throw exc; + } for (int i = 0; i < zahlen.length - 1; i++) { for (int j = i + 1; j < zahlen.length; j++) { if (zahlen[i] > zahlen[j]) { diff --git a/src/main/java/roboter/RobotInstructions.java b/src/main/java/roboter/RobotInstructions.java index 374bec5..c88e436 100644 --- a/src/main/java/roboter/RobotInstructions.java +++ b/src/main/java/roboter/RobotInstructions.java @@ -35,7 +35,7 @@ public interface RobotInstructions { * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * oder das Array nicht seinen Vorstellungen entspricht. */ - public String speak(int[] zahlen) throws RobotException; + public String speak(int[] zahlen) throws RobotException, RobotMagicValueException; /** * Sortiert ein Array von Zahlen. Die Reihenfolge hängt von dem Typ des @@ -46,5 +46,5 @@ public interface RobotInstructions { * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * oder das Array nicht seinen Vorstellungen entspricht. */ - public int[] think(int[] zahlen) throws RobotException; + public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; } \ No newline at end of file diff --git a/src/main/java/roboter/exceptions/RobotMagicValueException.java b/src/main/java/roboter/exceptions/RobotMagicValueException.java index 6c47b7a..d7e1a83 100644 --- a/src/main/java/roboter/exceptions/RobotMagicValueException.java +++ b/src/main/java/roboter/exceptions/RobotMagicValueException.java @@ -1,4 +1,9 @@ package roboter.exceptions; -public class RobotMagicValueException { +import roboter.Robot; + +public class RobotMagicValueException extends RobotException { + public RobotMagicValueException(Robot robot) { + super(robot.getName()); + } } diff --git a/src/test/java/roboter/C3PoSortierungTest.java b/src/test/java/roboter/C3PoSortierungTest.java new file mode 100644 index 0000000..2f70dc1 --- /dev/null +++ b/src/test/java/roboter/C3PoSortierungTest.java @@ -0,0 +1,20 @@ +package roboter; + +import org.junit.Assert; +import org.junit.Test; +import roboter.exceptions.RobotException; + +public class C3PoSortierungTest { + + @Test + public void sortieren() throws RobotException { + var list = new int[]{12,6,7,10,18,2}; + Robot robot= RobotFactory.getRobot(Typ.C3PO,"roboter2"); + + + assert robot != null; + Assert.assertEquals("2;6;7;10;12;18",robot.speak(list)); + } + + +} diff --git a/src/test/java/roboter/ExceptionTest.java b/src/test/java/roboter/ExceptionTest.java index 4c1a8de..0fe258b 100644 --- a/src/test/java/roboter/ExceptionTest.java +++ b/src/test/java/roboter/ExceptionTest.java @@ -17,6 +17,31 @@ public class ExceptionTest { } + @Test + public void powerOffSpeak(){ + Robot robot=RobotFactory.getRobot(Typ.C3PO,"dfg"); + robot.triggerPowerSwitch(); + + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot.speak(new int[8]); + }); + Robot robot2=RobotFactory.getRobot(Typ.R2D2,"dfg"); + robot2.triggerPowerSwitch(); + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot2.speak(new int[8]); + }); + + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot.think(new int[8]); + }); + + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot2.think(new int[8]); + }); + } + + + @Test public void testNexus6Trigger(){ Nexus6 robot = Nexus6.getInstance(); diff --git a/src/test/java/roboter/GetLastExceptionTest.java b/src/test/java/roboter/GetLastExceptionTest.java new file mode 100644 index 0000000..141c74c --- /dev/null +++ b/src/test/java/roboter/GetLastExceptionTest.java @@ -0,0 +1,44 @@ +package roboter; + +import org.junit.Assert; +import org.junit.Test; +import roboter.exceptions.RobotIllegalStateException; +import roboter.exceptions.RobotMagicValueException; + +import java.util.ArrayList; + +public class GetLastExceptionTest { + @Test + public void testGetLastExcetion(){ + int[]zahlen=new int[]{1,3,5,42}; + Robot robot= RobotFactory.getRobot(Typ.C3PO,"ukzfiouz"); + Assert.assertThrows(RobotMagicValueException.class,()->{ + robot.speak(zahlen); + }); + Assert.assertThrows(RobotMagicValueException.class,()->{ + robot.think(zahlen); + }); + Assert.assertTrue(robot.getLastException() instanceof RobotMagicValueException); + + Robot robot1= RobotFactory.getRobot(Typ.R2D2,"ukzfiouz"); + Assert.assertThrows(RobotMagicValueException.class,()->{ + robot1.speak(zahlen); + }); + Assert.assertThrows(RobotMagicValueException.class,()->{ + robot1.think(zahlen); + }); + Assert.assertTrue(robot1.getLastException() instanceof RobotMagicValueException); + + Robot robot2= RobotFactory.getRobot(Typ.Nexus6,"ukzfiouz"); + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot2.speak(zahlen); + }); + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot2.think(zahlen); + }); + Assert.assertTrue(robot2.getLastException() instanceof RobotIllegalStateException); + } + + + +} diff --git a/src/test/java/roboter/R2D2Test.java b/src/test/java/roboter/R2D2Test.java index 12338e9..124ff4b 100644 --- a/src/test/java/roboter/R2D2Test.java +++ b/src/test/java/roboter/R2D2Test.java @@ -4,6 +4,7 @@ import org.junit.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DynamicNode; import roboter.exceptions.RobotException; +import roboter.exceptions.RobotMagicValueException; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -14,26 +15,9 @@ import java.util.Properties; public class R2D2Test { - private final ByteArrayOutputStream out = new ByteArrayOutputStream(); - private final ByteArrayOutputStream err = new ByteArrayOutputStream(); - private final PrintStream originalOut = System.out; - private final PrintStream originalErr = System.err; - - @Before - public void setStreams() { - System.setOut(new PrintStream(out)); - System.setErr(new PrintStream(err)); - } - - @After - public void restoreInitialStreams() { - System.setOut(originalOut); - System.setErr(originalErr); - } - @Test - public void sortieren() throws RobotException { + public void sortieren() throws RobotException, RobotMagicValueException { var list = new int[]{12,6,7,10,18,2}; Robot robot= RobotFactory.getRobot(Typ.R2D2,"roboter2");