added Nexus6, added Documentation

main
romanamo 2022-12-23 13:37:38 +01:00
parent cdfdf10aac
commit 160d3c5ce3
12 changed files with 172 additions and 18 deletions

View File

View File

@ -5,6 +5,14 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateExceptio
import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
import de.hsmannheim.informatik.name.domain.requirements.Robot;
/**
* <h1>RobotBluePrint</h1>
*
* Represents a blueprint for all robots,
* implementing the specified requirements
*
* @see Robot
*/
public abstract class RobotBluePrint implements Robot {
protected final int ID;
@ -13,14 +21,16 @@ public abstract class RobotBluePrint implements Robot {
private final String name;
private boolean isPowered;
private RobotException lastException;
public RobotBluePrint(int id, String name) throws RobotIllegalStateException {
if (!this.isValidID(id)) {
//TODO write own Exception
throw new RobotIllegalStateException();
}
this.ID = id;
this.name = name;
this.isPowered = false;
if (!this.isValidID(id)) {
//TODO write own Exception & change Tests
throw new RobotIllegalStateException(String.format("%d is not a valid ID", id), this);
}
this.ID = id;
}
/**
@ -54,7 +64,11 @@ public abstract class RobotBluePrint implements Robot {
@Override
public RobotException getLastException() {
return null;
return this.lastException;
}
public void setLastException(RobotException lastException) {
this.lastException = lastException;
}
@Override

View File

@ -1,4 +1,11 @@
package de.hsmannheim.informatik.name.domain;
/**
* <h1>RobotFactory</h1>
* <p>
* Factory Class for producing Robots of Robots specified by {@link RobotType}.
*
* @see RobotType
*/
public class RobotFactory {
}

View File

@ -1,4 +1,11 @@
package de.hsmannheim.informatik.name.domain;
/**
* <h1>RobotType</h1>
* <p>
* Enumeration to represent a creatable Robot
*/
public enum RobotType {
C3P0,
R2D2
}

View File

@ -0,0 +1,59 @@
package de.hsmannheim.informatik.name.domain.bladerunner;
import de.hsmannheim.informatik.name.domain.RobotBluePrint;
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
/**
* <h1>Nexus6</h1>
* <p>
* Nexus-6 Robot with Name "Pris" and SerialNumber 19281982.
* This Robot doesnt work properly, since it has been damaged.
* <p>
* There can only exist one Instance of this Robot.
*
* @see RobotBluePrint
*/
public class Nexus6 extends RobotBluePrint {
private static Nexus6 nexus6 = null;
private final static int NEXUS_ID = 19281982;
private final static String NEXUS_NAME = "Pris";
private Nexus6() throws RobotIllegalStateException {
super(NEXUS_ID, NEXUS_NAME);
}
/**
* Creates an instance of {@link Nexus6}, if there currently is no available instance yet.
*
* @return instance of {@link Nexus6}
* @throws RobotIllegalStateException if the instance could not be created
*/
public static Nexus6 getInstance() throws RobotIllegalStateException {
if (nexus6 == null) {
nexus6 = new Nexus6();
}
return nexus6;
}
@Override
protected boolean isValidID(int id) {
return id == NEXUS_ID;
}
@Override
public void triggerPowerSwitch() {
}
@Override
public String speak(int[] zahlen) throws RobotIllegalStateException {
throw new RobotIllegalStateException("Robot is defect", this);
}
@Override
public int[] think(int[] zahlen) throws RobotIllegalStateException {
throw new RobotIllegalStateException("Robot is defect", this);
}
}

View File

@ -1,4 +1,16 @@
package de.hsmannheim.informatik.name.domain.exceptions;
import de.hsmannheim.informatik.name.domain.RobotBluePrint;
/**
* <h1>RobotException</h>
* <p>
* Exception for any unexpected Behaviour for any Robot.
*/
public class RobotException extends Exception {
public RobotException(String s, RobotBluePrint robot) {
super(s);
robot.setLastException(this);
}
}

View File

@ -1,4 +1,9 @@
package de.hsmannheim.informatik.name.domain.exceptions;
public class RobotIllegalStateException extends Exception {
import de.hsmannheim.informatik.name.domain.RobotBluePrint;
public class RobotIllegalStateException extends RobotException {
public RobotIllegalStateException(String s, RobotBluePrint robot) {
super(s, robot);
}
}

View File

@ -1,4 +1,9 @@
package de.hsmannheim.informatik.name.domain.exceptions;
public class RobotMagicValueException extends Exception {
import de.hsmannheim.informatik.name.domain.RobotBluePrint;
public class RobotMagicValueException extends RobotException {
public RobotMagicValueException(String s, RobotBluePrint robot) {
super(s, robot);
}
}

View File

@ -3,6 +3,14 @@ package de.hsmannheim.informatik.name.domain.starwars;
import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
/**
* <h1>C3P0</h1>
* <p>
* R2D2 robot using the InsertionSort-algorithm for {@code think()} and separating
* each element in {@code speak()} with ";".
*
* @see StarWarsRobot
*/
public class C3PO extends StarWarsRobot {
public C3PO(int id) throws RobotIllegalStateException {
super(';', id, "C3PO");

View File

@ -4,6 +4,14 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
/**
* <h1>R2D2</h1>
* <p>
* R2D2 robot using the SelectionSort-algorithm for {@code think()} and separating
* each element in {@code speak()} with ",".
*
* @see StarWarsRobot
*/
public class R2D2 extends StarWarsRobot {
public R2D2(int id) throws RobotIllegalStateException {
super(',', id, "R2D2");
@ -18,8 +26,8 @@ public class R2D2 extends StarWarsRobot {
public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException {
int[] arrayToSort = zahlen.clone();
for (int i = 0; i < arrayToSort.length; i++) {
if (arrayToSort[i] == 42) {
throw new RobotMagicValueException();
if (arrayToSort[i] == MAGIC_NUMBER) {
throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this);
}
int smallest = Integer.MAX_VALUE;
int smallestIndex = 0;

View File

@ -8,7 +8,13 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* <h1>StarWarsRobot</h1>
*
* Class to represent a Star Wars robot.
*
* @see RobotBluePrint
*/
public abstract class StarWarsRobot extends RobotBluePrint {
private final char SEPARATOR;
@ -19,14 +25,9 @@ public abstract class StarWarsRobot extends RobotBluePrint {
}
@Override
public RobotException getLastException() {
return null;
}
@Override
public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
public String speak(int[] zahlen) throws RobotException {
if (Arrays.stream(zahlen).anyMatch(i -> i == MAGIC_NUMBER)) {
throw new RobotMagicValueException();
throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this);
}
return this.buildString(zahlen);
}

View File

@ -0,0 +1,28 @@
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
import de.hsmannheim.informatik.name.domain.starwars.R2D2;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class R2D2IDTest {
@Test
public void NegativeTest(){
assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1));
assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MIN_VALUE));
}
@Test
public void InIDRangeTest() {
assertDoesNotThrow(() -> new R2D2(0));
assertDoesNotThrow(() -> new R2D2(1));
assertDoesNotThrow(() -> new R2D2(9999));
}
@Test
public void OuterIDRangeTest() {
assertThrows(RobotIllegalStateException.class, () -> new R2D2(10000));
assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MAX_VALUE));
}
}