Browse Source

player_management: savePlayers writes player list to file

Tobias Krause 3 years ago
parent
commit
32c8acbd65
  1. 14
      src/main/java/de/tims/player_management/PlayerManager.java
  2. 7
      src/test/java/de/tims/player_management/PlayerManagerTest.java
  3. 2
      src/test/java/resources/player_testdata2.csv

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

@ -2,6 +2,7 @@ package de.tims.player_management;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@ -72,8 +73,19 @@ public class PlayerManager {
}
public void savePlayers(String fileName) {
// TODO Auto-generated method stub
File playerData = new File(fileName);
try {
FileWriter fw = new FileWriter(playerData, false);
for (Player elem : players) {
fw.write(elem.getName() + ";" + elem.getPoints() + "\n");
}
fw.close();
} catch (IOException e) {
return;
}
}
}

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

@ -53,7 +53,8 @@ class PlayerManagerTest {
private static Stream<Arguments> testCasesForLoadPlayers() {
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("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))));
}
@ -69,7 +70,9 @@ class PlayerManagerTest {
}
private static Stream<Arguments> testCasesForSavePlayers() {
return Stream.of(Arguments.of("EmptyListIsWrittenAsEmptyFile", "src/test/java/resources/player_testdata2.csv", List.of()));
return Stream.of(Arguments.of("EmptyListIsWrittenAsEmptyFile", "src/test/java/resources/player_testdata2.csv", List.of()),
Arguments.of("WriteElementsOfListToFile", "src/test/java/resources/player_testdata2.csv",
List.of(new Player("Tobias", 50), new Player("Steffen", 60))));
}
}

2
src/test/java/resources/player_testdata2.csv

@ -0,0 +1,2 @@
Tobias;50
Steffen;60
Loading…
Cancel
Save