small bug fix

main
zlohbierdcul 2023-01-09 22:09:11 +01:00
parent 6df925c403
commit 293143274a
5 changed files with 15 additions and 4 deletions

View File

@ -34,6 +34,9 @@ public class C3PO extends StarWarsRobot {
//A: RobotMagicValueException is a subclass of RobotException. So if a RobotException is thrown in a method, //A: RobotMagicValueException is a subclass of RobotException. So if a RobotException is thrown in a method,
// the Method signature is already covered for any subclass of RobotException that is thrown inside the method // the Method signature is already covered for any subclass of RobotException that is thrown inside the method
public int[] think(int[] numbers) throws RobotException { public int[] think(int[] numbers) throws RobotException {
if (!this.isPowerOn()) {
throw new RobotIllegalStateException("The robot is not on!", this);
}
int[] arrToSort = numbers.clone(); int[] arrToSort = numbers.clone();
for (int k : arrToSort) { for (int k : arrToSort) {
if (k == MAGIC_NUMBER) { if (k == MAGIC_NUMBER) {

View File

@ -31,6 +31,9 @@ public class R2D2 extends StarWarsRobot {
*/ */
@Override @Override
public int[] think(int[] numbers) throws RobotException { public int[] think(int[] numbers) throws RobotException {
if (!this.isPowerOn()) {
throw new RobotIllegalStateException("The robot is not on!", this);
}
int[] arrayToSort = numbers.clone(); int[] arrayToSort = numbers.clone();
for (int i = 0; i < arrayToSort.length; i++) { for (int i = 0; i < arrayToSort.length; i++) {
if (arrayToSort[i] == MAGIC_NUMBER) { if (arrayToSort[i] == MAGIC_NUMBER) {

View File

@ -33,6 +33,9 @@ public abstract class StarWarsRobot extends RobotBluePrint {
*/ */
@Override @Override
public String speak(int[] numbers) throws RobotException { public String speak(int[] numbers) throws RobotException {
if (!this.isPowerOn()) {
throw new RobotIllegalStateException("The robot is not on!", this);
}
if (Arrays.stream(numbers).anyMatch(i -> i == MAGIC_NUMBER)) { if (Arrays.stream(numbers).anyMatch(i -> i == MAGIC_NUMBER)) {
throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this); throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this);
} }

View File

@ -10,11 +10,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
class SpeakTest { class SpeakTest {
final R2D2 r2d2 = new R2D2(9999, "test");
final C3PO c3po = new C3PO(19999, "test");
private final R2D2 r2d2 = new R2D2(9999, "test");
private final C3PO c3po = new C3PO(19999, "test");
SpeakTest() throws RobotIllegalStateException { SpeakTest() throws RobotIllegalStateException {
r2d2.triggerPowerSwitch();
c3po.triggerPowerSwitch();
} }
@Test @Test

View File

@ -14,11 +14,12 @@ class ThinkTest {
private final C3PO c3po = new C3PO(19999, "test"); private final C3PO c3po = new C3PO(19999, "test");
ThinkTest() throws RobotIllegalStateException { ThinkTest() throws RobotIllegalStateException {
r2d2.triggerPowerSwitch();
c3po.triggerPowerSwitch();
} }
@Test @Test
void thinkTestStandard() throws RobotException { void thinkTestStandard() throws RobotException {
assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, r2d2.think(new int[]{3, 0, 1, 5, 2, 4})); assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, r2d2.think(new int[]{3, 0, 1, 5, 2, 4}));
} }
@ -43,7 +44,6 @@ class ThinkTest {
} }
@Test @Test
void thinkTestStandardC3PO() throws RobotException { void thinkTestStandardC3PO() throws RobotException {
assertArrayEquals(new int[]{5, 4, 3, 2, 1, 0}, c3po.think(new int[]{3, 0, 1, 5, 2, 4})); assertArrayEquals(new int[]{5, 4, 3, 2, 1, 0}, c3po.think(new int[]{3, 0, 1, 5, 2, 4}));
} }