added ID, name, tests and more

main
zlohbierdcul 2022-12-21 13:55:39 +01:00
parent 83f4f99f04
commit 5df396e69a
7 changed files with 152 additions and 30 deletions

View File

@ -0,0 +1,58 @@
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;
public abstract class RobotBluePrint implements Robot {
protected final int ID;
private final String name;
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;
}
protected abstract boolean isValidID(int id);
@Override
public int getId() {
return this.ID;
}
@Override
public String getName() {
return this.name;
}
@Override
public void triggerPowerSwitch() {
}
@Override
public boolean isPowerOn() {
return false;
}
@Override
public RobotException getLastException() {
return null;
}
@Override
public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
return null;
}
@Override
public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException {
return new int[0];
}
}

View File

@ -34,7 +34,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
@ -45,5 +45,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,10 +1,16 @@
package de.hsmannheim.informatik.name.domain.starwars; 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;
public class C3PO extends StarWarsRobot { public class C3PO extends StarWarsRobot {
public C3PO() { public C3PO(int id) throws RobotIllegalStateException {
super(';'); super(';', id, "C3PO");
}
@Override
protected boolean isValidID(int id) {
return 10000 <= id && id <= 19999;
} }
@Override @Override
@ -17,6 +23,7 @@ public class C3PO extends StarWarsRobot {
} }
} }
} }
reverse(arrToSort);
return arrToSort; return arrToSort;
} }
} }

View File

@ -1,16 +1,26 @@
package de.hsmannheim.informatik.name.domain.starwars; 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.RobotMagicValueException;
public class R2D2 extends StarWarsRobot { public class R2D2 extends StarWarsRobot {
public R2D2() { public R2D2(int id) throws RobotIllegalStateException {
super(','); super(',', id, "R2D2");
} }
@Override @Override
public int[] think(int[] zahlen) throws RobotException { protected boolean isValidID(int id) {
return 0 <= id && id <= 9999;
}
@Override
public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException {
int[] arrayToSort = zahlen.clone(); int[] arrayToSort = zahlen.clone();
for (int i = 0; i < arrayToSort.length; i++) { for (int i = 0; i < arrayToSort.length; i++) {
if (arrayToSort[i] == 42) {
throw new RobotMagicValueException();
}
int smallest = Integer.MAX_VALUE; int smallest = Integer.MAX_VALUE;
int smallestIndex = 0; int smallestIndex = 0;
for (int j = i; j < arrayToSort.length; j++) { for (int j = i; j < arrayToSort.length; j++) {
@ -21,7 +31,7 @@ public class R2D2 extends StarWarsRobot {
} }
swap(arrayToSort, smallestIndex, i); swap(arrayToSort, smallestIndex, i);
} }
reverse(arrayToSort);
return arrayToSort; return arrayToSort;
} }
} }

View File

@ -1,27 +1,21 @@
package de.hsmannheim.informatik.name.domain.starwars; package de.hsmannheim.informatik.name.domain.starwars;
import de.hsmannheim.informatik.name.domain.RobotBluePrint;
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.RobotMagicValueException;
import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot; import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot;
public abstract class StarWarsRobot implements Robot { public abstract class StarWarsRobot extends RobotBluePrint {
private final char SEPARATOR; private final char SEPARATOR;
protected StarWarsRobot(char separator) { protected StarWarsRobot(char separator, int id, String name) throws RobotIllegalStateException {
super(id, name);
SEPARATOR = separator; SEPARATOR = separator;
} }
@Override
public int getId() {
return 0;
}
@Override
public String getName() {
return null;
}
@Override @Override
public void triggerPowerSwitch() { public void triggerPowerSwitch() {
@ -38,10 +32,13 @@ public abstract class StarWarsRobot implements Robot {
} }
@Override @Override
public String speak(int[] zahlen) throws RobotException { public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
//Loops through every element of zahlen[] //Loops through every element of zahlen[]
for (int i = 0; i < zahlen.length; i++) { for (int i = 0; i < zahlen.length; i++) {
if (zahlen[i] == 42) {
throw new RobotMagicValueException();
}
sb.append(zahlen[i]); sb.append(zahlen[i]);
//if current number is not the last number add a separator //if current number is not the last number add a separator
if (i != zahlen.length - 1) { if (i != zahlen.length - 1) {
@ -51,7 +48,7 @@ public abstract class StarWarsRobot implements Robot {
return sb.toString(); return sb.toString();
} }
public abstract int[] think(int[] zahlen) throws RobotException; public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException;
protected static void swap(int[] arr, int a, int b) { protected static void swap(int[] arr, int a, int b) {
int temp = arr[a]; int temp = arr[a];

View File

@ -1,36 +1,46 @@
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.RobotMagicValueException;
import de.hsmannheim.informatik.name.domain.starwars.R2D2; import de.hsmannheim.informatik.name.domain.starwars.R2D2;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
class R2D2SpeakTest { class R2D2SpeakTest {
private final R2D2 r2d2 = new R2D2(); private final R2D2 r2d2 = new R2D2(9999);
R2D2SpeakTest() throws RobotIllegalStateException {
}
@Test @Test
void speakTestStandard() throws RobotException { void speakTestStandard() throws RobotException, RobotMagicValueException {
assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
} }
@Test @Test
void speakTestEmpty() throws RobotException { void speakTestEmpty() throws RobotException, RobotMagicValueException {
assertEquals("", r2d2.speak(new int[]{})); assertEquals("", r2d2.speak(new int[]{}));
} }
@Test @Test
void speakTestZeros() throws RobotException { void speakTestZeros() throws RobotException, RobotMagicValueException {
assertEquals("0, 0, 0, 0", r2d2.speak(new int[4])); assertEquals("0, 0, 0, 0", r2d2.speak(new int[4]));
} }
@Test @Test
void speakTestOneElement() throws RobotException { void speakTestOneElement() throws RobotException, RobotMagicValueException {
assertEquals("1", r2d2.speak(new int[]{1})); assertEquals("1", r2d2.speak(new int[]{1}));
} }
@Test @Test
void speakTestUnitElements() throws RobotException { void speakTestUnitElements() throws RobotException, RobotMagicValueException {
assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1})); assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1}));
} }
@Test
void speakTestMagicNumberException() throws RobotException {
assertThrows(RobotMagicValueException.class,() -> r2d2.speak(new int[]{42}));
}
} }

View File

@ -1,4 +1,44 @@
public class R2D2ThinkTest { 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.starwars.R2D2;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class R2D2ThinkTest {
private final R2D2 r2d2 = new R2D2(9999);
R2D2ThinkTest() throws RobotIllegalStateException {
}
@Test
void thinkTestStandard() throws RobotException, RobotMagicValueException {
assertArrayEquals(new int[]{0,1,2,3,4,5}, r2d2.think(new int[]{3,0,1,5,2,4}));
}
@Test
void thinkTestNegative() throws RobotException, RobotMagicValueException {
assertArrayEquals(new int[]{-3,-2,-1,0,1,2,3}, r2d2.think(new int[]{0,-3,2,-1,1,3,-2}));
}
@Test
void thinkTestZeros() throws RobotException, RobotMagicValueException {
assertArrayEquals(new int[]{0,0,0,0}, r2d2.think(new int[]{0,0,0,0}));
}
@Test
void thinkTestEmpty() throws RobotException, RobotMagicValueException {
assertArrayEquals(new int[]{}, r2d2.think(new int[]{}));
}
@Test
void thinkTestMagicNumberException() {
assertThrows(RobotMagicValueException.class,() -> r2d2.think(new int[]{42}));
}
} }