From 93dec93db44f416b2c9725903e13ecc6b4505970 Mon Sep 17 00:00:00 2001 From: azadehobenland Date: Mon, 9 Jan 2023 23:31:54 +0100 Subject: [PATCH] -implemented getLastException Method for all classes - implemented RobotMagicValueException for speak and think - implemented RobotIllegalStateException for the number 42 - implemented tests for the above 3 points --- out/production/RobotAbgabe/roboter/C3PO.class | Bin 2720 -> 3947 bytes .../RobotAbgabe/roboter/Nexus6.class | Bin 1892 -> 1941 bytes out/production/RobotAbgabe/roboter/R2D2.class | Bin 2685 -> 3913 bytes .../roboter/RobotInstructions.class | Bin 261 -> 314 bytes .../exceptions/RobotMagicValueException.class | Bin 335 -> 499 bytes .../RobotAbgabe/roboter/ExceptionTest.class | Bin 2009 -> 3086 bytes out/test/RobotAbgabe/roboter/R2D2Test.class | Bin 1134 -> 1185 bytes src/main/java/roboter/C3PO.java | 59 +++++++++++++----- src/main/java/roboter/Nexus6.java | 8 ++- src/main/java/roboter/R2D2.java | 35 +++++++++-- src/main/java/roboter/RobotInstructions.java | 4 +- .../exceptions/RobotMagicValueException.java | 7 ++- src/test/java/roboter/C3PoSortierungTest.java | 20 ++++++ src/test/java/roboter/ExceptionTest.java | 25 ++++++++ .../java/roboter/GetLastExceptionTest.java | 44 +++++++++++++ src/test/java/roboter/R2D2Test.java | 20 +----- 16 files changed, 181 insertions(+), 41 deletions(-) create mode 100644 src/test/java/roboter/C3PoSortierungTest.java create mode 100644 src/test/java/roboter/GetLastExceptionTest.java diff --git a/out/production/RobotAbgabe/roboter/C3PO.class b/out/production/RobotAbgabe/roboter/C3PO.class index 124a63f5a68369aae392da3e6b4d2aaf21634908..d18e1a7845ee168a0ea0d9112b9180ae6ffe8df1 100644 GIT binary patch literal 3947 zcmb7HYj+dZ72Q`4k1Y?jVvq@Tz$P}qmW@S9peZsAM!`kYMg|iTaFQ~X26=3GMvO*) zY13CiU-bPbMu_ud0=1b>Mkgs_5$iYS@{;)~i9EmhLYVk$SksOKGlrcuK*oH2p$z~F2Pn$e;l zr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ip8zVE&F-TVD~^ULs`z0R+HJ^lf} zOStoeQ*9gd>jI*ZL;_tB8FUL+zPDZz$ez9F-SJ9mp1)eU*b3_YYFS%lCHA0KAX#rN zHa@Ne=Y8Ur9p3awJcGRgvDS^ce@h@fu~L~_spzX`B?d4k(Cr0*_vz_Iuv! zd9y$5U>N&sj7W@PO#LAi(kbjm!N$160Zgb*jD2d^98UnQ#1x9^bF-M5LCMB*63^oW z^`m(uJI64J4i4gwjTa>jPu_kz;Q;fJ1H9{1kyp{YNJ&PN*A-mT2^JbBxxT{PW!{^V8TWe#cVH8?TiitYeh4_lH4BSt znrj!K8Lr(p&6AOB7+%F0?nn^26QS1D9u0b^;I+t4iBez2+y}5Xs2I@f@$j%OWR%+m z5u?ECILi|qWG6U%Zx?vrzu-1;)sS1GFNL81IlAuo3MtpxM9()!xF)4e)C@z}PILq| zXDNd`YgOoA&P8FJqrC+qW6{MPod2Hydp6MicY2c@Jh7eh7V$=eE>h~+7-DyBGaX1{ z2YCU5Oi8D9j#76|x>FC4tGdO9=qE=zs;)x`%&`&&L!#}T$%u$BT*RAn9p~>7H3M(a zUZ?gpb-p`l+Rbh5w440YBMfaJFYp+Xz!s(rWM>~?ehXy-KVhL-3^P~?3);sq1CEfd z%&9qw9ADEYj&($m--#q2MKVfHm!sj~CxJWd|7UV&uCiKBA8+fq8RsklC*W|9ducO7 zk7Hz>AmcR8W_fp*^P?wqj@U6y=Q8uvQ(V9bHT^&i;T>waM&+GI%26b`(LIPgWmlde rC5E3M#y<-*`%i^TT%nDDckv#jp5FJ#x{5k{e8>}rC%4hWN9g|>y$q_; diff --git a/out/production/RobotAbgabe/roboter/RobotInstructions.class b/out/production/RobotAbgabe/roboter/RobotInstructions.class index 9963796f43d63305faf67a33155655837e5ca0a8..59837e5d001a0d54e183f9709b3ae973651c17e1 100644 GIT binary patch delta 100 zcmZo=+Qr0m>ff$?3=9mm3<49mx-{4sxEUF=i}I85OHzyUQ!A2F3raHc^NRI@KtjHW t>6yu4i8-aI6T2f-*%+7@I2gEC7#VmNco`TO*n!4C#eh6MAdiWG9{{XX75e}H delta 55 zcmdnR)XKzl>ff$?3=9mm415#0x+WeCP-J0XWZ+<7WZ+`p2C~?Ja$pf4n+M2dV&DY; DV?_uZ diff --git a/out/production/RobotAbgabe/roboter/exceptions/RobotMagicValueException.class b/out/production/RobotAbgabe/roboter/exceptions/RobotMagicValueException.class index 7e465aaf46dc2153c82e41503e8920d66f8fe925..22b789ffd52d58961e6ef59efa403b03ef31032d 100644 GIT binary patch delta 296 zcmX@l^qD#0)W2Q(7#J9A86>$Gm>8JZ8CZB2SQ*$D8F-8Glk!Vai}Zs)G$R9hdTNPZ zVs0uUgNTNvPgY`CqJBEUuHUSR^#+*;Z@85kKEwllDA1ag@e1c4+MSPJM=pjIIuUksv@ XL6|`V$OCF(5Cw{X3={|Qm>47gky$#6 delta 156 zcmey&e4feX)W2Q(7#J9A83ec(m>8JZ8CZB2SQ*$D83eKt%M$f-67$ma{gbj%lS>#G z*laTMGE3|j8JIOR!`K-(CeAgO_$YO9BBNdaJ5VzSFajY^JCNiA^7O%c1_oBG?F@_? jfdWhnTtE^e&A`CSzzrmMz-(3^%?so)GVlROCI)^0PD&NI diff --git a/out/test/RobotAbgabe/roboter/ExceptionTest.class b/out/test/RobotAbgabe/roboter/ExceptionTest.class index 66b0e279d432a02f888d21dfcd184d72a624fb30..8051207b746087a93ab249f3655d1fbcf0923e7c 100644 GIT binary patch literal 3086 zcmb7GZC4XV6n+K*3tSLzqkLS zAL<8-9#4NjPk&QSpV>`z6A%x6$n4y?Gta&Ex%bZGpZ}i!4d4oXkE0It3Suf6&?wNp zsI6)lQ?rVh{OqD$@B|t!8WKPtI{0xd<| z8?jtZvkJOEB9+e7?#L?GA#l2~s6Q&`E1qFnZf0B}jhLoh)XWJ_^Yoh)U4ovrtJsMI zeQ-*az=hOC*mTZzikZa{Jl-Pog`L&aWnGT7+M zHP_W02CHiR#6Rf@jHYrCAwwpW*=-q0R~_@QeDev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fe^CKUSE>q~IDys@wc5$quCr*+Thk*p5 zNa6tD_aYtW8z_Z9|EZI0>L_~XDO@I^JtDffy1T%@Z$4NaS?!SUz(S1RyZ0CmjR4am zz#t=634#8_KLmGlAA$%3cCKz80_nX*Xd32F32bS&PJ2tZMPHcRrtB{DI5FNs9S@ND E3m=kd9{>OV diff --git a/out/test/RobotAbgabe/roboter/R2D2Test.class b/out/test/RobotAbgabe/roboter/R2D2Test.class index aa9925730bd3b943c40e8790e69f483595f745f3..3a51ee954410abbb4a3b9526337131e4b639351c 100644 GIT binary patch delta 126 zcmaFIv5=GN)W2Q(7#J9A8EiIkO<>YsXE0@C&@ReP$}dSR(od~OPAw?O%+D*<4+07K zCZ=a5hb88ercR#7^o~(raszWCuPB2OgBXK0gCv7FgVba`7F$&|1||k01``G|puubm Sj6iV)poj&Sv}CXX)7AhTvK&GH delta 83 zcmZ3;`Hq9@)W2Q(7#J9A8LT#PO<-c}4uAjv 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");