added Power, rewrite of speak
parent
4e3852da9e
commit
cdfdf10aac
|
@ -1,10 +1,16 @@
|
|||
# RobotFactory
|
||||
|
||||
## Style Guide
|
||||
|
||||
* For every method you write you also write the JUnit test and comments.
|
||||
* [reference](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html)
|
||||
* Always mark the task you're currently working on the kanban board and move it to the right phase.
|
||||
|
||||
## References
|
||||
|
||||
* [JavaDocs](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html)
|
||||
* [Markdown](https://www.markdownguide.org/extended-syntax/)
|
||||
* [JUnit](https://junit.org/junit5/docs/current/user-guide/)
|
||||
|
||||
## Contributors
|
||||
|
||||
* Luc Dreibholz (Matr.Nr.: 2210544)
|
||||
|
|
|
@ -3,22 +3,33 @@ package de.hsmannheim.informatik.name.domain;
|
|||
import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
|
||||
import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot;
|
||||
import de.hsmannheim.informatik.name.domain.requirements.Robot;
|
||||
|
||||
public abstract class RobotBluePrint implements Robot {
|
||||
|
||||
protected final int ID;
|
||||
|
||||
protected final int MAGIC_NUMBER = 42;
|
||||
private final String name;
|
||||
private boolean isPowered;
|
||||
|
||||
public RobotBluePrint(int id, String name) throws RobotIllegalStateException {
|
||||
if(!this.isValidID(id)) {
|
||||
if (!this.isValidID(id)) {
|
||||
//TODO write own Exception
|
||||
throw new RobotIllegalStateException();
|
||||
}
|
||||
this.ID = id;
|
||||
this.name = name;
|
||||
this.isPowered = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an {@code int} represents a valid {@code ID},
|
||||
* concerning a specific model within its own criteria
|
||||
*
|
||||
* @param id {@code ID} to check
|
||||
* @return {@code true} if the ID is valid, else {@code false}
|
||||
*/
|
||||
protected abstract boolean isValidID(int id);
|
||||
|
||||
@Override
|
||||
|
@ -33,12 +44,12 @@ public abstract class RobotBluePrint implements Robot {
|
|||
|
||||
@Override
|
||||
public void triggerPowerSwitch() {
|
||||
|
||||
this.isPowered = !this.isPowered;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowerOn() {
|
||||
return false;
|
||||
return this.isPowered;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -47,12 +58,8 @@ public abstract class RobotBluePrint implements Robot {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
|
||||
return null;
|
||||
}
|
||||
public abstract String speak(int[] zahlen) throws RobotException, RobotMagicValueException;
|
||||
|
||||
@Override
|
||||
public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException {
|
||||
return new int[0];
|
||||
}
|
||||
public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* (c) 2012 Thomas Smits */
|
||||
package de.hsmannheim.informatik.name.domain.robot_interfaces;
|
||||
package de.hsmannheim.informatik.name.domain.requirements;
|
||||
|
||||
/**
|
||||
* Interface für Roboter.
|
|
@ -1,4 +1,4 @@
|
|||
package de.hsmannheim.informatik.name.domain.robot_interfaces;
|
||||
package de.hsmannheim.informatik.name.domain.requirements;
|
||||
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.hsmannheim.informatik.name.domain.robot_interfaces;
|
||||
package de.hsmannheim.informatik.name.domain.requirements;
|
||||
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
|
|
@ -4,7 +4,9 @@ import de.hsmannheim.informatik.name.domain.RobotBluePrint;
|
|||
import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
|
||||
import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
|
||||
import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public abstract class StarWarsRobot extends RobotBluePrint {
|
||||
|
@ -16,16 +18,6 @@ public abstract class StarWarsRobot extends RobotBluePrint {
|
|||
SEPARATOR = separator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void triggerPowerSwitch() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowerOn() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RobotException getLastException() {
|
||||
return null;
|
||||
|
@ -33,29 +25,41 @@ public abstract class StarWarsRobot extends RobotBluePrint {
|
|||
|
||||
@Override
|
||||
public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
//Loops through every element of zahlen[]
|
||||
for (int i = 0; i < zahlen.length; i++) {
|
||||
if (zahlen[i] == 42) {
|
||||
throw new RobotMagicValueException();
|
||||
}
|
||||
sb.append(zahlen[i]);
|
||||
//if current number is not the last number add a separator
|
||||
if (i != zahlen.length - 1) {
|
||||
sb.append(SEPARATOR).append(" ");
|
||||
}
|
||||
if (Arrays.stream(zahlen).anyMatch(i -> i == MAGIC_NUMBER)) {
|
||||
throw new RobotMagicValueException();
|
||||
}
|
||||
return sb.toString();
|
||||
return this.buildString(zahlen);
|
||||
}
|
||||
|
||||
public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException;
|
||||
/**
|
||||
* Creates a String representation of the given {@code payload},
|
||||
* with separated values, by the delimiter {@code SEPARATOR}.
|
||||
*
|
||||
* @param payload {@code int[]} of elements
|
||||
* @return {@link String representation} of the {@code payload}
|
||||
*/
|
||||
private String buildString(int[] payload) {
|
||||
return Arrays.stream(payload).mapToObj(String::valueOf).collect(Collectors.joining(SEPARATOR + " "));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper-Method to swap Elements of a given {@code array}.
|
||||
*
|
||||
* @param arr {@code array} to swap Elements
|
||||
* @param a Index of the first Element
|
||||
* @param b Index of the second Element
|
||||
*/
|
||||
protected static void swap(int[] arr, int a, int b) {
|
||||
int temp = arr[a];
|
||||
arr[a] = arr[b];
|
||||
arr[b] = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper-Method to reverse an {@code array}.
|
||||
*
|
||||
* @param array {@code array} to reverse
|
||||
*/
|
||||
protected static void reverse(int[] array) {
|
||||
for (int i = 0; i < array.length / 2; i++) {
|
||||
int temp = array[i];
|
||||
|
|
|
@ -41,6 +41,6 @@ class R2D2SpeakTest {
|
|||
|
||||
@Test
|
||||
void speakTestMagicNumberException() throws RobotException {
|
||||
assertThrows(RobotMagicValueException.class,() -> r2d2.speak(new int[]{42}));
|
||||
assertThrows(RobotMagicValueException.class, () -> r2d2.speak(new int[]{42}));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue