master
azadehobenland 2023-01-06 14:41:58 +01:00
parent bd3a5c4c2b
commit 023365e653
15 changed files with 120 additions and 43 deletions

View File

@ -3,9 +3,20 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -1,21 +0,0 @@
import tpe.exceptions.roboter.Robot;
public class RobotFactory {
private static int R2D2Id=0;
private static int C2POId=10000;
public static Robot getRobot(String typ){
switch (typ){
case "R2D2":
return new R2D2(R2D2Id++,"blah");
case "C3PO":
return new C3PO(C2POId++,"blah");
case "Nexus6":
return Nexus6.getInstance();
}
return null;
}
}

View File

@ -1,5 +1,6 @@
import tpe.exceptions.roboter.Robot;
import tpe.exceptions.roboter.exceptions.RobotException;
package roboter;
import roboter.exceptions.RobotException;
import java.util.Arrays;
import java.util.stream.Collectors;

View File

@ -1,9 +1,8 @@
import tpe.exceptions.roboter.Robot;
import tpe.exceptions.roboter.exceptions.RobotException;
import tpe.exceptions.roboter.exceptions.RobotIllegalStateException;
package roboter;
import java.util.Arrays;
import java.util.stream.Collectors;
import roboter.exceptions.RobotException;
import roboter.exceptions.RobotIllegalStateException;
import roboter.exceptions.RobotException;
public class Nexus6 implements Robot {

View File

@ -1,5 +1,6 @@
import tpe.exceptions.roboter.Robot;
import tpe.exceptions.roboter.exceptions.RobotException;
package roboter;
import roboter.exceptions.RobotException;
import java.util.Arrays;
import java.util.stream.Collectors;

View File

@ -1,6 +1,6 @@
/* (c) 2012 Thomas Smits */
package tpe.exceptions.roboter;
package roboter;
/**
* Interface für Roboter.

View File

@ -1,7 +1,8 @@
package tpe.exceptions.roboter;
package roboter;
import tpe.exceptions.roboter.exceptions.RobotException;
import roboter.exceptions.RobotException;
import roboter.exceptions.RobotException;
/**
* Das Interface repräsentiert einen einfachen Roboter mit seinen Funktionen.

View File

@ -0,0 +1,22 @@
package roboter;
public class RobotFactory {
private static int R2D2Id=0;
private static int C2POId=10000;
public static Robot getRobot(Typ typ,String name){
switch (typ){
case R2D2:
return new R2D2(R2D2Id++,name);
case C3PO:
return new C3PO(C2POId++,name);
case Nexus6:
return Nexus6.getInstance();
}
return null;
}
}

View File

@ -1,9 +1,9 @@
package tpe.exceptions.roboter;
package roboter;
import tpe.exceptions.roboter.exceptions.RobotException;
import tpe.exceptions.roboter.exceptions.RobotIllegalStateException;
import tpe.exceptions.roboter.exceptions.RobotMagicValueException;
import roboter.exceptions.RobotException;
import roboter.exceptions.RobotIllegalStateException;
import roboter.exceptions.RobotMagicValueException;
/**
* Das Interface repräsentiert den Befehlssatz eines einfachen Roboters.

View File

@ -0,0 +1,5 @@
package roboter;
public enum Typ {
R2D2,C3PO,Nexus6
}

View File

@ -1,4 +1,4 @@
package tpe.exceptions.roboter.exceptions;
package roboter.exceptions;
public class RobotException extends Exception{
public RobotException(String errormessage) {

View File

@ -1,7 +1,7 @@
package tpe.exceptions.roboter.exceptions;
import tpe.exceptions.roboter.Robot;
package roboter.exceptions;
import roboter.Robot;
import roboter.exceptions.*;
public class RobotIllegalStateException extends RobotException {
public RobotIllegalStateException(Robot roboter) {

View File

@ -1,4 +1,4 @@
package tpe.exceptions.roboter.exceptions;
package roboter.exceptions;
public class RobotMagicValueException {
}

View File

@ -0,0 +1,29 @@
package roboter;
import org.junit.Assert;
import org.junit.Test;
import roboter.exceptions.RobotIllegalStateException;
public class ExceptionTest {
@Test
public void testNexus6Speak(){
Nexus6 robot = Nexus6.getInstance();
Assert.assertThrows(RobotIllegalStateException.class,()->{
robot.speak(new int[]{1,2,3,4});
});
Assert.assertThrows(RobotIllegalStateException.class,()->{
robot.think(new int[]{1,2,3,4});
});
}
@Test
public void testNexus6Trigger(){
Nexus6 robot = Nexus6.getInstance();
boolean isPowerOn = robot.isPowerOn();
robot.triggerPowerSwitch();
Assert.assertEquals(isPowerOn, robot.isPowerOn());
}
}

View File

@ -0,0 +1,29 @@
package roboter;
import org.junit.Assert;
import org.junit.Test;
public class FactoryTest {
@Test
public void singletonNexus6(){
Nexus6 roboter1 = Nexus6.getInstance();
Nexus6 roboter2=Nexus6.getInstance();
Assert.assertEquals(roboter1,roboter2);
}
@Test
public void factoryMethodTest(){
Robot robot1=RobotFactory.getRobot(Typ.R2D2,"roboterX");
Assert.assertTrue(robot1 instanceof R2D2);
Robot robot2=RobotFactory.getRobot(Typ.C3PO,"roboter2");
Assert.assertTrue(robot2 instanceof C3PO);
Assert.assertEquals(robot1.getName(),"roboterX");
Assert.assertEquals(robot2.getName(),"roboter2");
}
}