From d173c8c6d39a8cd1ace7dd4615e1d1e229f9f54c Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Tue, 11 Jan 2022 19:41:03 +0100 Subject: [PATCH] player_management: loadPlayers reads list of players from csv-file --- .../tims/player_management/PlayerManager.java | 38 +++++++++---------- src/main/java/resources/player_data.csv | 1 - .../player_management/PlayerManagerTest.java | 6 ++- src/test/java/resources/player_testdata.csv | 3 ++ 4 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 src/test/java/resources/player_testdata.csv diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index 84a54b9..ce6ae39 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -44,26 +44,26 @@ public class PlayerManager { 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()); + do { + for (int i = 0; i < PLAYER_ATTRIBUTES; i++) { + do { + c = fr.read(); + if (c != ';' && c != '\n' && c != -1) { + sb[i].append((char) c); + } + } while (c != ';' && c != '\n' && c != -1); + } - players.add(new Player(playerName, playerPoints)); - - nameBuilder.delete(0, nameBuilder.length()); - pointBuilder.delete(0, pointBuilder.length()); - } + 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()); + } + } while (c != -1); fr.close(); } catch (IOException e) { diff --git a/src/main/java/resources/player_data.csv b/src/main/java/resources/player_data.csv index 8decaa8..e69de29 100644 --- a/src/main/java/resources/player_data.csv +++ b/src/main/java/resources/player_data.csv @@ -1 +0,0 @@ -Tobias;50 \ No newline at end of file diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 2b9d2a5..ec7a5a9 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -52,8 +52,10 @@ class PlayerManagerTest { } private static Stream testCasesForLoadPlayers() { - 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)))); + return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/test/java/resources/player_testdata.csv", List.of()), + Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/test/java/resources/player_testdata.csv", List.of(new Player("Tobias", 50))), + Arguments.of("MorePlayersInFileReturnLongerList", "Tobias;50\nLorenz;40\nSteffen;60", "src/test/java/resources/player_testdata.csv", + List.of(new Player("Tobias", 50), new Player("Lorenz", 40), new Player("Steffen", 60)))); } } diff --git a/src/test/java/resources/player_testdata.csv b/src/test/java/resources/player_testdata.csv new file mode 100644 index 0000000..5396826 --- /dev/null +++ b/src/test/java/resources/player_testdata.csv @@ -0,0 +1,3 @@ +Tobias;50 +Lorenz;40 +Steffen;60 \ No newline at end of file