Browse Source

player_management: loadPlayers reads player from csv-file

player_management
Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
b4b4fcfe39
  1. 19
      src/main/java/de/tims/gameexplorer/Player.java
  2. 2
      src/main/java/de/tims/player_management/Player.java
  3. 45
      src/main/java/de/tims/player_management/PlayerManager.java
  4. 1
      src/main/java/resources/player_data.csv
  5. 32
      src/test/java/de/tims/gameexplorer/PlayerTest.java
  6. 3
      src/test/java/de/tims/player_management/PlayerManagerTest.java

19
src/main/java/de/tims/gameexplorer/Player.java

@ -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;
}
}

2
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

45
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<Player> players;
private static final int PLAYER_ATTRIBUTES = 2;
public List<Player> getPlayers() {
return players;
@ -25,7 +30,45 @@ public class PlayerManager {
}
public void loadPlayers(String fileName) {
players = List.of();
players = new LinkedList<Player>();
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;
}
}
}

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

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

32
src/test/java/de/tims/gameexplorer/PlayerTest.java

@ -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<Arguments> 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));
}
}

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

@ -52,7 +52,8 @@ class PlayerManagerTest {
}
private static Stream<Arguments> 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))));
}
}
Loading…
Cancel
Save