Browse Source

Merge branch 'chooseUserInput' into develop

AIPlayer
Jonas Wagner 3 years ago
parent
commit
6a9e9c4222
  1. 20
      src/main/java/Player.java
  2. 23
      src/test/java/PlayerTest.java

20
src/main/java/Player.java

@ -1,5 +1,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Player {
@ -7,6 +8,7 @@ public class Player {
ArrayList<Figure> figures;
int startHome;
int endHome;
Scanner scanner;
public Player (String name, int startHome, int endHome) {
this.name = name;
@ -50,4 +52,22 @@ public class Player {
}
return false;
}
public int choose() {
scanner = new Scanner(System.in);
System.out.print("Wählen Sie eine Figur(1-4): ");
try{
int input = scanner.nextInt();
if (input > 4 || input < 1) {
System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
"Bitte nur Zahlen von 1 bis 4 eingeben.");
return -1;
}
return input;
} catch (Exception e) {
System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
"Bitte nur Zahlen von 1 bis 4 eingeben.");
return -1;
}
}
}

23
src/test/java/PlayerTest.java

@ -6,6 +6,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -172,4 +173,26 @@ public class PlayerTest {
false)
);
}
@ParameterizedTest
@MethodSource("checkChooseMethodData")
void checkChoose(String testName, String input, int expectedResult) {
System.setIn(new ByteArrayInputStream(input.getBytes()));
int calculatedResult = p.choose();
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> checkChooseMethodData() {
return Stream.of(
Arguments.of("Figure 1 choosen", "1\n", 1),
Arguments.of("Figure 2 choosen", "2\n", 2),
Arguments.of("Figure 3 choosen", "3\n", 3),
Arguments.of("Figure 4 choosen", "4\n", 4),
Arguments.of("Index out of bounds choosen", "16\n", -1),
Arguments.of("Index out of bounds choosen", "-2\n", -1),
Arguments.of("Input a letter or char", "g\n", -1),
Arguments.of("Input a empty string", "\n", -1),
Arguments.of("Input a special character", "&\n", -1)
);
}
}
Loading…
Cancel
Save