From b4b4fcfe3941d37bdc1771b4943a360988485aad Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Tue, 11 Jan 2022 19:27:38 +0100 Subject: [PATCH] player_management: loadPlayers reads player from csv-file --- .../java/de/tims/gameexplorer/Player.java | 19 -------- .../de/tims/player_management/Player.java | 2 +- .../tims/player_management/PlayerManager.java | 45 ++++++++++++++++++- src/main/java/resources/player_data.csv | 1 + .../java/de/tims/gameexplorer/PlayerTest.java | 32 ------------- .../player_management/PlayerManagerTest.java | 3 +- 6 files changed, 48 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/de/tims/gameexplorer/Player.java create mode 100644 src/main/java/resources/player_data.csv delete mode 100644 src/test/java/de/tims/gameexplorer/PlayerTest.java diff --git a/src/main/java/de/tims/gameexplorer/Player.java b/src/main/java/de/tims/gameexplorer/Player.java deleted file mode 100644 index 0200dc8..0000000 --- a/src/main/java/de/tims/gameexplorer/Player.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.tims.gameexplorer; - -public class Player { - - private int points; - - public Player(int points) { - this.points = points; - } - - public void addPoints(int pointsToAdd) { - this.points = (this.points + pointsToAdd > 0) ? this.points + pointsToAdd : 0; - } - - public int getPoints() { - return this.points; - } - -} diff --git a/src/main/java/de/tims/player_management/Player.java b/src/main/java/de/tims/player_management/Player.java index 50f86b7..9bf11bd 100644 --- a/src/main/java/de/tims/player_management/Player.java +++ b/src/main/java/de/tims/player_management/Player.java @@ -34,7 +34,7 @@ public class Player { Player player2 = (Player) o; - return this.points == player2.points; + return this.points == player2.points && this.name.equals(player2.getName()); } @Override diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index b21fe73..84a54b9 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -1,10 +1,15 @@ package de.tims.player_management; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedList; import java.util.List; public class PlayerManager { private List players; + private static final int PLAYER_ATTRIBUTES = 2; public List getPlayers() { return players; @@ -25,7 +30,45 @@ public class PlayerManager { } public void loadPlayers(String fileName) { - players = List.of(); + players = new LinkedList(); + + File playerData = new File(fileName); + + try { + FileReader fr = new FileReader(playerData); + + int c = -1; + String playerName; + int playerPoints; + StringBuilder nameBuilder = new StringBuilder(); + StringBuilder pointBuilder = new StringBuilder(); + StringBuilder[] sb = {nameBuilder, pointBuilder}; + + for (int i = 0; i < PLAYER_ATTRIBUTES; i++) { + do { + c = fr.read(); + if (c == ';' || c == '\n' || c == -1) { + break; + } + + sb[i].append((char) c); + } while (c != ';' && c != '\n' && c != -1); + } + + if (!(nameBuilder.toString().equals("") || pointBuilder.toString().equals(""))) { + playerName = nameBuilder.toString(); + playerPoints = Integer.parseInt(pointBuilder.toString()); + + players.add(new Player(playerName, playerPoints)); + + nameBuilder.delete(0, nameBuilder.length()); + pointBuilder.delete(0, pointBuilder.length()); + } + + fr.close(); + } catch (IOException e) { + return; + } } } diff --git a/src/main/java/resources/player_data.csv b/src/main/java/resources/player_data.csv new file mode 100644 index 0000000..8decaa8 --- /dev/null +++ b/src/main/java/resources/player_data.csv @@ -0,0 +1 @@ +Tobias;50 \ No newline at end of file diff --git a/src/test/java/de/tims/gameexplorer/PlayerTest.java b/src/test/java/de/tims/gameexplorer/PlayerTest.java deleted file mode 100644 index 2d782d1..0000000 --- a/src/test/java/de/tims/gameexplorer/PlayerTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package de.tims.gameexplorer; - -import static org.assertj.core.api.Assertions.*; - -import java.util.stream.Stream; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -class PlayerTest { - - Player player; - - @ParameterizedTest - @MethodSource("testCasesForAddPoints") - void test(String testName, int pointsBefore, int pointsToAdd, int expectedResult) { - player = new Player(pointsBefore); - player.addPoints(pointsToAdd); - int calculatedResult = player.getPoints(); - assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); - } - - private static Stream testCasesForAddPoints() { - return Stream.of(Arguments.of("NoPointsBeforeGet0Points", 0, 0, 0), - Arguments.of("NoPointsBeforeGet10Points", 0, 10, 10), - Arguments.of("10PointsBeforeAdd10Points", 10, 10, 20), - Arguments.of("10PointsBeforeLose10Points", 10, -10, 0), - Arguments.of("LoseMorePointsThanYouHave", 10, -20, 0)); - } - -} diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 5def1ec..2b9d2a5 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -52,7 +52,8 @@ class PlayerManagerTest { } private static Stream testCasesForLoadPlayers() { - return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of())); + return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of()), + Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/main/java/resources/player_data.csv", List.of(new Player("Tobias", 50)))); } }