-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
master
azadehobenland 2023-01-09 23:31:54 +01:00
parent 50104496da
commit 93dec93db4
16 changed files with 181 additions and 41 deletions

View File

@ -1,19 +1,25 @@
package roboter; package roboter;
import roboter.exceptions.RobotException; import roboter.exceptions.RobotException;
import roboter.exceptions.RobotIllegalStateException;
import roboter.exceptions.RobotMagicValueException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class C3PO implements Robot { public class C3PO implements Robot {
private int id; private int id;
private String name; private String name;
private boolean isPowerOn=false; private boolean isPowerOn = true;
ArrayList<RobotException> exceptionList=new ArrayList<>();
public C3PO(int id, String name){ public C3PO(int id, String name) {
this.id=id; this.id = id;
this.name=name; this.name = name;
} }
@Override @Override
public int getId() { public int getId() {
@ -27,10 +33,10 @@ public C3PO(int id, String name){
@Override @Override
public void triggerPowerSwitch() { public void triggerPowerSwitch() {
if(isPowerOn==false){ if (isPowerOn == false) {
isPowerOn=true; isPowerOn = true;
}else{ } else {
isPowerOn=false; isPowerOn = false;
} }
} }
//tes test //tes test
@ -42,31 +48,56 @@ public C3PO(int id, String name){
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
if (!exceptionList.isEmpty()){
return exceptionList.get(exceptionList.size() - 1);
}
return null; return null;
} }
@Override @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); var sortiert = think(zahlen);
return arrayFormatieren(sortiert); return arrayFormatieren(sortiert);
} }
private String arrayFormatieren(int[] zahlen){ private String arrayFormatieren(int[] zahlen) throws RobotMagicValueException {
var ergebnis = Arrays.stream(zahlen) var ergebnis = Arrays.stream(zahlen)
.boxed() .boxed()
.map(String::valueOf) .map(String::valueOf)
.collect(Collectors.joining(";")); .collect(Collectors.joining(";"));
return ergebnis; return ergebnis;
} }
@Override @Override
public int[] think(int[] zahlen) throws RobotException { 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; int n = zahlen.length;
for (int i = 1; i < n; ++i) { for (int i = 1; i < n; ++i) {
int key = zahlen[i]; int key = zahlen[i];
int j = i - 1; int j = i - 1;
while (j >= 0 && zahlen[j] < key) { while (j >= 0 && zahlen[j] > key) {
zahlen[j + 1] = zahlen[j]; zahlen[j + 1] = zahlen[j];
j = j - 1; j = j - 1;
} }

View File

@ -50,7 +50,11 @@ public class Nexus6 implements Robot {
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
return exceptionList.get(exceptionList.size()-1); if (!exceptionList.isEmpty()){
return exceptionList.get(exceptionList.size() - 1);
}
return null;
} }
@Override @Override

View File

@ -1,14 +1,18 @@
package roboter; package roboter;
import roboter.exceptions.RobotException; import roboter.exceptions.RobotException;
import roboter.exceptions.RobotIllegalStateException;
import roboter.exceptions.RobotMagicValueException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class R2D2 implements Robot { public class R2D2 implements Robot {
private int id; private int id;
private String name; private String name;
private boolean isPowerOn; private boolean isPowerOn=true;
ArrayList<RobotException> exceptionList=new ArrayList<>();
public R2D2(int id, String name){ public R2D2(int id, String name){
this.id=id; this.id=id;
@ -43,12 +47,25 @@ public class R2D2 implements Robot {
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
if (!exceptionList.isEmpty()){
return exceptionList.get(exceptionList.size() - 1);
}
return null; return null;
} }
@Override @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); var sortiert = think(zahlen);
return arrayFormatieren(sortiert); return arrayFormatieren(sortiert);
@ -62,7 +79,17 @@ public class R2D2 implements Robot {
return ergebnis; return ergebnis;
} }
@Override @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 i = 0; i < zahlen.length - 1; i++) {
for (int j = i + 1; j < zahlen.length; j++) { for (int j = i + 1; j < zahlen.length; j++) {
if (zahlen[i] > zahlen[j]) { if (zahlen[i] > zahlen[j]) {

View File

@ -35,7 +35,7 @@ public interface RobotInstructions {
* @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist,
* oder das Array nicht seinen Vorstellungen entspricht. * 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 * 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, * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist,
* oder das Array nicht seinen Vorstellungen entspricht. * oder das Array nicht seinen Vorstellungen entspricht.
*/ */
public int[] think(int[] zahlen) throws RobotException; public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException;
} }

View File

@ -1,4 +1,9 @@
package roboter.exceptions; package roboter.exceptions;
public class RobotMagicValueException { import roboter.Robot;
public class RobotMagicValueException extends RobotException {
public RobotMagicValueException(Robot robot) {
super(robot.getName());
}
} }

View File

@ -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));
}
}

View File

@ -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 @Test
public void testNexus6Trigger(){ public void testNexus6Trigger(){
Nexus6 robot = Nexus6.getInstance(); Nexus6 robot = Nexus6.getInstance();

View File

@ -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);
}
}

View File

@ -4,6 +4,7 @@ import org.junit.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicNode; import org.junit.jupiter.api.DynamicNode;
import roboter.exceptions.RobotException; import roboter.exceptions.RobotException;
import roboter.exceptions.RobotMagicValueException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
@ -14,26 +15,9 @@ import java.util.Properties;
public class R2D2Test<SystemOutRule, OutputCapture> { public class R2D2Test<SystemOutRule, OutputCapture> {
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 @Test
public void sortieren() throws RobotException { public void sortieren() throws RobotException, RobotMagicValueException {
var list = new int[]{12,6,7,10,18,2}; var list = new int[]{12,6,7,10,18,2};
Robot robot= RobotFactory.getRobot(Typ.R2D2,"roboter2"); Robot robot= RobotFactory.getRobot(Typ.R2D2,"roboter2");