comments + added MagicValueException in C3PO

main
Stefan 2022-12-26 15:48:09 +01:00
parent 58cac3d48a
commit f8ce7fa3e8
3 changed files with 26 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package de.hsmannheim.informatik.name.domain.starwars;
import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
/** /**
* <h1>C3P0</h1> * <h1>C3P0</h1>
@ -21,9 +22,21 @@ public class C3PO extends StarWarsRobot {
return 10000 <= id && id <= 19999; return 10000 <= id && id <= 19999;
} }
/**
* C3PO think method sorts numbers with insertion sort in descending order
* @param numbers integer numbers for sorting
* @return arrToSort integer array with numbers sorted in descending order
* @throws RobotException
*/
@Override @Override
public int[] think(int[] zahlen) throws RobotException { //TODO why is RobotMagicValueException grayed out?
int[] arrToSort = zahlen.clone(); public int[] think(int[] numbers) throws RobotException, RobotMagicValueException {
int[] arrToSort = numbers.clone();
for (int i = 0; i < arrToSort.length; i++) {
if (arrToSort[i] == MAGIC_NUMBER) {
throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this);
}
}
for (int i = 1; i < arrToSort.length; i++) { for (int i = 1; i < arrToSort.length; i++) {
for (int j = i; j > 0; j--) { for (int j = i; j > 0; j--) {
if (arrToSort[j - 1] > arrToSort[j]) { if (arrToSort[j - 1] > arrToSort[j]) {

View File

@ -23,7 +23,7 @@ public class R2D2 extends StarWarsRobot {
} }
/** /**
* R2D2 think method sorts numbers with selection sort * R2D2 think method sorts numbers with selection sort in ascending order
* @param numbers integer numbers for sorting * @param numbers integer numbers for sorting
* @return arrayToSort integer array with numbers sorted in ascending order * @return arrayToSort integer array with numbers sorted in ascending order
* @throws RobotException * @throws RobotException

View File

@ -24,12 +24,19 @@ public abstract class StarWarsRobot extends RobotBluePrint {
SEPARATOR = separator; SEPARATOR = separator;
} }
/**
* Speak method checks for occurrence of Magic Number, if the number is not in the input array,
* the input gets returned after applying the method buildString
* @param numbers integer numbers for output
* @return String with for robot type matching seperator
* @throws RobotException
*/
@Override @Override
public String speak(int[] zahlen) throws RobotException { public String speak(int[] numbers) throws RobotException {
if (Arrays.stream(zahlen).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);
} }
return this.buildString(zahlen); return this.buildString(numbers);
} }
/** /**