From 1aeb383f8c567fe8a8cf844344235ae86eccd62b Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 1 Feb 2022 08:52:08 +0100 Subject: [PATCH 1/4] Implement choose Method --- src/main/java/Player.java | 10 ++++++++++ src/test/java/PlayerTest.java | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 0ed8bd6..091eb59 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,5 +1,7 @@ +import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; +import java.util.Scanner; public class Player { @@ -7,6 +9,7 @@ public class Player { ArrayList
figures; int startHome; int endHome; + Scanner scanner; public Player (String name, int startHome, int endHome) { this.name = name; @@ -50,4 +53,11 @@ public class Player { } return false; } + + public int choose() { + scanner = new Scanner(System.in); + System.out.println("Eingabe:"); + int input = scanner.nextInt(); + return input; + } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index 07a3033..d63631d 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -6,6 +6,10 @@ 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.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -172,4 +176,18 @@ public class PlayerTest { false) ); } + + @Test + void checkChooseMethod() { + + InputStream stdin = System.in; + System.setIn(new ByteArrayInputStream("0\n".getBytes())); + + int calculatedResult = p.choose(); + + System.setIn(stdin); + + int expectedResult = 0; + assertThat(calculatedResult).describedAs("Choose one Figure").isEqualTo(expectedResult); + } } From b44787ed31016343844878e5339a22bb6a1fe2ab Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 1 Feb 2022 09:10:52 +0100 Subject: [PATCH 2/4] Turn checkChoose into Parameterized Test --- src/main/java/Player.java | 11 +++++++++-- src/test/java/PlayerTest.java | 25 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 091eb59..242bb02 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -57,7 +57,14 @@ public class Player { public int choose() { scanner = new Scanner(System.in); System.out.println("Eingabe:"); - int input = scanner.nextInt(); - return input; + try{ + int input = scanner.nextInt(); + if (input > 4 || input < 1) { + return -1; + } + return input; + } catch (Exception e) { + return -1; + } } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index d63631d..62ced73 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -177,17 +177,28 @@ public class PlayerTest { ); } - @Test - void checkChooseMethod() { - + @ParameterizedTest + @MethodSource("checkChooseMethodData") + void checkChoose(String testName, String input, int expectedResult) { InputStream stdin = System.in; - System.setIn(new ByteArrayInputStream("0\n".getBytes())); + System.setIn(new ByteArrayInputStream(input.getBytes())); int calculatedResult = p.choose(); - System.setIn(stdin); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); + } - int expectedResult = 0; - assertThat(calculatedResult).describedAs("Choose one Figure").isEqualTo(expectedResult); + static Stream 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) + ); } } From fb39982e6ed05ecf87aa5017d65fb7a563b8bc9a Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 1 Feb 2022 09:13:49 +0100 Subject: [PATCH 3/4] Refactor choose Method --- src/main/java/Player.java | 1 - src/test/java/PlayerTest.java | 6 ------ 2 files changed, 7 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 242bb02..d26e0f3 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,4 +1,3 @@ -import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index 62ced73..c419115 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -7,9 +7,6 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -180,11 +177,8 @@ public class PlayerTest { @ParameterizedTest @MethodSource("checkChooseMethodData") void checkChoose(String testName, String input, int expectedResult) { - InputStream stdin = System.in; System.setIn(new ByteArrayInputStream(input.getBytes())); - int calculatedResult = p.choose(); - assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); } From c2f98917f50b2b524e4f5b9941b41c1c4a076fca Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 1 Feb 2022 09:20:48 +0100 Subject: [PATCH 4/4] Refactor choose Method console output --- src/main/java/Player.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index d26e0f3..c143347 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -55,14 +55,18 @@ public class Player { public int choose() { scanner = new Scanner(System.in); - System.out.println("Eingabe:"); + 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; } }