Browse Source

player_management: loadPlayers reads list of players from csv-file

Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
d173c8c6d3
  1. 10
      src/main/java/de/tims/player_management/PlayerManager.java
  2. 1
      src/main/java/resources/player_data.csv
  3. 6
      src/test/java/de/tims/player_management/PlayerManagerTest.java
  4. 3
      src/test/java/resources/player_testdata.csv

10
src/main/java/de/tims/player_management/PlayerManager.java

@ -44,18 +44,17 @@ public class PlayerManager {
StringBuilder pointBuilder = new StringBuilder(); StringBuilder pointBuilder = new StringBuilder();
StringBuilder[] sb = {nameBuilder, pointBuilder}; StringBuilder[] sb = {nameBuilder, pointBuilder};
do {
for (int i = 0; i < PLAYER_ATTRIBUTES; i++) { for (int i = 0; i < PLAYER_ATTRIBUTES; i++) {
do { do {
c = fr.read(); c = fr.read();
if (c == ';' || c == '\n' || c == -1) {
break;
}
if (c != ';' && c != '\n' && c != -1) {
sb[i].append((char) c); sb[i].append((char) c);
}
} while (c != ';' && c != '\n' && c != -1); } while (c != ';' && c != '\n' && c != -1);
} }
if (!(nameBuilder.toString().equals("") || pointBuilder.toString().equals(""))) {
if (!nameBuilder.toString().equals("") && !pointBuilder.toString().equals("")) {
playerName = nameBuilder.toString(); playerName = nameBuilder.toString();
playerPoints = Integer.parseInt(pointBuilder.toString()); playerPoints = Integer.parseInt(pointBuilder.toString());
@ -64,6 +63,7 @@ public class PlayerManager {
nameBuilder.delete(0, nameBuilder.length()); nameBuilder.delete(0, nameBuilder.length());
pointBuilder.delete(0, pointBuilder.length()); pointBuilder.delete(0, pointBuilder.length());
} }
} while (c != -1);
fr.close(); fr.close();
} catch (IOException e) { } catch (IOException e) {

1
src/main/java/resources/player_data.csv

@ -1 +0,0 @@
Tobias;50

6
src/test/java/de/tims/player_management/PlayerManagerTest.java

@ -52,8 +52,10 @@ class PlayerManagerTest {
} }
private static Stream<Arguments> testCasesForLoadPlayers() { private static Stream<Arguments> 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))));
} }
} }

3
src/test/java/resources/player_testdata.csv

@ -0,0 +1,3 @@
Tobias;50
Lorenz;40
Steffen;60
Loading…
Cancel
Save