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. 40
      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

40
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());
players.add(new Player(playerName, playerPoints));
nameBuilder.delete(0, nameBuilder.length());
pointBuilder.delete(0, pointBuilder.length());
}
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);
}
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) {

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() {
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