From 459a35fe2d378267eb166ab9e34b830f11de6fc2 Mon Sep 17 00:00:00 2001 From: FelixKrull Date: Wed, 9 Feb 2022 10:50:33 +0100 Subject: [PATCH] Change Choose method to use List of Figures --- src/main/java/Player.java | 14 ++++++-------- src/test/java/PlayerTest.java | 24 +++++++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/Player.java b/src/main/java/Player.java index a276573..2180cb4 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,6 +1,4 @@ -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; public class Player { @@ -56,20 +54,20 @@ public class Player { return count; } - public int choose() { + public int choose(ArrayList usableFigures) { scanner = new Scanner(System.in); - System.out.print("Wählen Sie eine Figur(1-4): "); + System.out.print("Wählen Sie eine Figur " + usableFigures.toString() + ": "); try{ int input = scanner.nextInt(); - if (input > 4 || input < 1) { + if (input > Collections.max(usableFigures) || input < Collections.min(usableFigures)) { System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" + - "Bitte nur Zahlen von 1 bis 4 eingeben."); + "Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " 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."); + "Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben."); return -1; } } diff --git a/src/test/java/PlayerTest.java b/src/test/java/PlayerTest.java index cab2608..093e870 100644 --- a/src/test/java/PlayerTest.java +++ b/src/test/java/PlayerTest.java @@ -176,23 +176,25 @@ public class PlayerTest { @ParameterizedTest @MethodSource("checkChooseMethodData") - void checkChoose(String testName, String input, int expectedResult) { + void checkChoose(String testName, String input,ArrayList usableFiugres, int expectedResult) { System.setIn(new ByteArrayInputStream(input.getBytes())); - int calculatedResult = p.choose(); + int calculatedResult = p.choose(usableFiugres); assertThat(calculatedResult).describedAs(testName).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) + Arguments.of("Figure 1 choosen", "1\n", new ArrayList<>(Arrays.asList(1,2,3,4)), 1), + Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(1,2,3,4)), 2), + Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(1,2,3,4)),3), + Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(1,2,3,4)),4), + Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), + Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), + Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), + Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), + Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1), + Arguments.of("Only one Figure available", "1\n", new ArrayList<>(Arrays.asList(1)),1), + Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1) ); } }