fixed test for C3PO and added comments

master
Eline 2023-01-10 17:33:34 +01:00
parent 789c332b37
commit 965477fe59
4 changed files with 106 additions and 24 deletions

View File

@ -15,22 +15,31 @@ public class C3PO implements Robot {
private String name; private String name;
private boolean isPowerOn = true; private boolean isPowerOn = true;
ArrayList<RobotException> exceptionList=new ArrayList<>(); ArrayList<RobotException> exceptionList=new ArrayList<>();
public C3PO(int id, String name) { public C3PO(int id, String name) {
this.id = id; this.id = id;
this.name = name; this.name = name;
} }
/**
* @return id of the robot C3PO
*/
@Override @Override
public int getId() { public int getId() {
return id; return id;
} }
/**
* @return name of the robot
*/
@Override @Override
public String getName() { public String getName() {
return name; return name;
} }
/**
* @return isPorwerOn true if it's false
*/
@Override @Override
public void triggerPowerSwitch() { public void triggerPowerSwitch() {
if (isPowerOn == false) { if (isPowerOn == false) {
@ -39,13 +48,18 @@ public class C3PO implements Robot {
isPowerOn = false; isPowerOn = false;
} }
} }
//tes test
/**
* @return isPowerOn
*/
@Override @Override
public boolean isPowerOn() { public boolean isPowerOn() {
return isPowerOn; return isPowerOn;
} }
/**
* creating a RobotException getLastException
*/
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
if (!exceptionList.isEmpty()){ if (!exceptionList.isEmpty()){
@ -55,6 +69,11 @@ public class C3PO implements Robot {
return null; return null;
} }
/**
* speak method that returns the sorted and formated numbers from the array zahlen
* @param int[] zahlen Array of numbers for sorting
* @return sorted and formated numbers
*/
@Override @Override
public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
if(!isPowerOn){ if(!isPowerOn){
@ -71,7 +90,14 @@ public class C3PO implements Robot {
return arrayFormatieren(sortiert); return arrayFormatieren(sortiert);
} }
/**
*method to format the outcome
* Returns a Stream consisting of the elements of this stream,each boxed to an Integer
* and maps it into a String
* each element of the Stream is separated by ","
* @param int[] zahlen Array of numbers to sort
* @return formated outcome
*/
private String arrayFormatieren(int[] zahlen) throws RobotMagicValueException { private String arrayFormatieren(int[] zahlen) throws RobotMagicValueException {
var ergebnis = Arrays.stream(zahlen) var ergebnis = Arrays.stream(zahlen)
@ -81,6 +107,12 @@ public class C3PO implements Robot {
return ergebnis; return ergebnis;
} }
/**
* method that sorts the array zahlen with the Selectionsort-Algorithmin in descending order
* if the array zahlen contains the number 42 the RobotMagicValueException is thrown and added to the exception list
* @param int[] zahlen Array of numbers for sorting
* @return array of numbers in descending order
*/
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
if(!isPowerOn){ if(!isPowerOn){

View File

@ -26,23 +26,31 @@ public class Nexus6 implements Robot {
return instance; return instance;
} }
/**
* @return id of the robot
*/
@Override @Override
public int getId() { public int getId() {
return id; return id;
} }
/**
* @return name of the robot
*/
@Override @Override
public String getName() { public String getName() {
return name; return name;
} }
@Override @Override
public void triggerPowerSwitch() { public void triggerPowerSwitch() {
} }
/**
* Nexus can be turned on but it has no funktionality
* @return PowerisOn
*/
@Override @Override
public boolean isPowerOn() { public boolean isPowerOn() {
return isPowerOn; return isPowerOn;
@ -56,7 +64,11 @@ public class Nexus6 implements Robot {
} }
return null; return null;
} }
/**
* Nexus 6 can't format the sorted numbers so an exception RobotIllegalStateException is thrown
* and added to the exception list
* * @param int[] zahlen Array
*/
@Override @Override
public String speak(int[] zahlen) throws RobotIllegalStateException { public String speak(int[] zahlen) throws RobotIllegalStateException {
var newException = new RobotIllegalStateException(this); var newException = new RobotIllegalStateException(this);
@ -65,7 +77,11 @@ public class Nexus6 implements Robot {
} }
/**
* Nexus 6 can't sort the numbers so an exception RobotIllegalStateException is thrown
* and added to the exception list
* * @param int[] zahlen Array
*/
@Override @Override
public int[] think(int[] zahlen) throws RobotIllegalStateException { public int[] think(int[] zahlen) throws RobotIllegalStateException {
var newException2=new RobotIllegalStateException(this); var newException2=new RobotIllegalStateException(this);

View File

@ -20,17 +20,25 @@ public class R2D2 implements Robot {
} }
/**
* @return id of the robot
*/
@Override @Override
public int getId() { public int getId() {
return id; return id;
} }
/**
* @return name of the robot
*/
@Override @Override
public String getName() { public String getName() {
return name; return name;
} }
/**
* @return isPorwerOn true if it's false
*/
@Override @Override
public void triggerPowerSwitch() { public void triggerPowerSwitch() {
if(isPowerOn==false){ if(isPowerOn==false){
@ -40,11 +48,17 @@ public class R2D2 implements Robot {
} }
} }
/**
* @return isPowerOn
*/
@Override @Override
public boolean isPowerOn() { public boolean isPowerOn() {
return isPowerOn; return isPowerOn;
} }
/**
* creating a RobotException getLastException
*/
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
if (!exceptionList.isEmpty()){ if (!exceptionList.isEmpty()){
@ -54,6 +68,12 @@ public class R2D2 implements Robot {
return null; return null;
} }
/**
* speak method that returns the sorted and formated numbers from the array zahlen
* if the array zahlen contains the number 42 the RobotMagicValueException is thrown and added to the exception list
* @param int[] zahlen Array of numbers for sorting
* @return sorted and formated numbers
*/
@Override @Override
public String speak(int[] zahlen) throws RobotException, RobotMagicValueException { public String speak(int[] zahlen) throws RobotException, RobotMagicValueException {
if(!isPowerOn){ if(!isPowerOn){
@ -71,6 +91,14 @@ public class R2D2 implements Robot {
return arrayFormatieren(sortiert); return arrayFormatieren(sortiert);
} }
/**
* method to format the outcome
* Returns a Stream consisting of the elements of this stream,each boxed to an Integer
* and maps it into a String
* each element of the Stream is separated by ","
* @param int[] zahlen Array of numbers to sort
* @return formated outcome
*/
private String arrayFormatieren(int[] zahlen){ private String arrayFormatieren(int[] zahlen){
var ergebnis = Arrays.stream(zahlen) var ergebnis = Arrays.stream(zahlen)
.boxed() .boxed()
@ -78,6 +106,12 @@ public class R2D2 implements Robot {
.collect(Collectors.joining(",")); .collect(Collectors.joining(","));
return ergebnis; return ergebnis;
} }
/**
* method that sorts the array zahlen with the Selectionsort-Algorithmin in ascending order
* @param int[] zahlen Array of numbers for sorting
* @return array of numbers in ascending order
*/
@Override @Override
public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException { public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException {
if(!isPowerOn){ if(!isPowerOn){

View File

@ -33,36 +33,36 @@ public class C3POTest <SystemOutRule, OutputCapture> {
var list = new int[]{12,6,7,10,18,2}; var list = new int[]{12,6,7,10,18,2};
Robot robot= RobotFactory.getRobot(Typ.C3PO,"roboter2"); Robot robot= RobotFactory.getRobot(Typ.C3PO,"roboter2");
Assert.assertEquals("18,12,10,7,6,2",robot.speak(list)); Assert.assertEquals("18;12;10;7;6;2",robot.speak(list));
var list1 = new int[] {17,10,19,1,4,2,7}; var list1 = new int[] {17,10,19,1,4,2,7};
Assert.assertEquals("19,17,10,7,4,2,1", robot.speak(list)); Assert.assertEquals("19;17;10;7;4;2;1", robot.speak(list));
var list2 = new int[] {3,17,7,20,5,15,8}; var list2 = new int[] {3,17,7,20,5,15,8};
Assert.assertEquals("20,17,15,8,7,5,3", robot.speak(list)); Assert.assertEquals("20;17;15;8;7;5;3", robot.speak(list));
var list3 = new int[] {13,8,5,12,1,9,7}; var list3 = new int[] {13,8,5,12,1,9,7};
Assert.assertEquals("13,12,9,8,7,5,1", robot.speak(list)); Assert.assertEquals("13;12;9;8;7;5;1", robot.speak(list));
var list4 = new int[] {18,4,21,7,3,2,9}; var list4 = new int[] {18,4,21,7,3,2,9};
Assert.assertEquals("21,18,9,7,4,3,2", robot.speak(list)); Assert.assertEquals("21;18;9;7;4;3;2", robot.speak(list));
var list5 = new int[] {13,23,8,4,22,6}; var list5 = new int[] {13,23,8,4,22,6};
Assert.assertEquals("23,22,13,8,6,4", robot.speak(list)); Assert.assertEquals("23;22;13;8;6;4", robot.speak(list));
var list6 = new int[] {10,9,8,7,6,5}; var list6 = new int[] {10,9,8,7,6,5};
Assert.assertEquals("10,9,8,7,6,5", robot.speak(list)); Assert.assertEquals("10;9;8;7;6;5", robot.speak(list));
var list7 = new int[] {1,2,3,5,4,6}; var list7 = new int[] {1,2,3,5,4,6};
Assert.assertEquals("6,5,4,3,2,1", robot.speak(list)); Assert.assertEquals("6;5;4;3;2;1", robot.speak(list));
var list8 = new int[] {20,19,18,17,16,15}; var list8 = new int[] {20,19,18,17,16,15};
Assert.assertEquals("20,19,18,17,16,15", robot.speak(list)); Assert.assertEquals("20;19;18;17;16;15", robot.speak(list));
var list9 = new int[] {15,1,13,10,7,4}; var list9 = new int[] {15,1,13,10,7,4};
Assert.assertEquals("15,13,10,7,4,1", robot.speak(list)); Assert.assertEquals("15;13;10;7;4;1", robot.speak(list));
var list10 = new int[] {10,15,11,8,5,6,}; var list10 = new int[] {10,15,11,8,5,6,};
Assert.assertEquals("15,11,10,8,6,5", robot.speak(list)); Assert.assertEquals("15;11;10;8;6;5", robot.speak(list));
} }
} }