12 Commits
61e1035bb1
...
5fe12bc7ec
9 changed files with 334 additions and 23 deletions
-
77src/main/java/de/tims/gameexplorer/GameExplorer.java
-
19src/main/java/de/tims/gameexplorer/Player.java
-
45src/main/java/de/tims/player_management/Player.java
-
95src/main/java/de/tims/player_management/PlayerManager.java
-
0src/main/java/resources/player_data.csv
-
110src/test/java/de/tims/player_management/PlayerManagerTest.java
-
6src/test/java/de/tims/player_management/PlayerTest.java
-
3src/test/java/resources/player_testdata.csv
-
2src/test/java/resources/player_testdata2.csv
@ -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; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,45 @@ |
|||
package de.tims.player_management; |
|||
|
|||
public class Player { |
|||
|
|||
private String name; |
|||
private int points; |
|||
|
|||
public Player(String name, int points) { |
|||
this.name = name; |
|||
this.points = points; |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
public int getPoints() { |
|||
return this.points; |
|||
} |
|||
|
|||
public void addPoints(int pointsToAdd) { |
|||
this.points = (this.points + pointsToAdd > 0) ? this.points + pointsToAdd : 0; |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object o) { |
|||
if (o == this) { |
|||
return true; |
|||
} |
|||
|
|||
if ((o == null) || this.getClass() != o.getClass()) { |
|||
return false; |
|||
} |
|||
|
|||
Player player2 = (Player) o; |
|||
|
|||
return this.points == player2.points && this.name.equals(player2.getName()); |
|||
} |
|||
|
|||
@Override |
|||
public int hashCode() { |
|||
return points + name.hashCode(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,95 @@ |
|||
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; |
|||
|
|||
public class PlayerManager { |
|||
|
|||
private List<Player> players; |
|||
private static final int PLAYER_ATTRIBUTES = 2; |
|||
|
|||
public List<Player> getPlayers() { |
|||
return players; |
|||
} |
|||
|
|||
public void setPlayers(List<Player> players) { |
|||
this.players = players; |
|||
} |
|||
|
|||
public Player selectPlayer(String playerName) { |
|||
for (Player p : this.players) { |
|||
if (playerName.equals(p.getName())) { |
|||
return p; |
|||
} |
|||
} |
|||
|
|||
Player newPlayer = new Player(playerName, 0); |
|||
|
|||
players.add(newPlayer); |
|||
|
|||
return newPlayer; |
|||
} |
|||
|
|||
public void loadPlayers(String fileName) { |
|||
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}; |
|||
|
|||
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) { |
|||
return; |
|||
} |
|||
} |
|||
|
|||
public void savePlayers(String fileName) { |
|||
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; |
|||
} |
|||
} |
|||
|
|||
} |
@ -0,0 +1,110 @@ |
|||
package de.tims.player_management; |
|||
|
|||
import static org.assertj.core.api.Assertions.*; |
|||
|
|||
import java.io.File; |
|||
import java.io.FileWriter; |
|||
import java.io.IOException; |
|||
import java.util.LinkedList; |
|||
import java.util.List; |
|||
import java.util.stream.Stream; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.params.ParameterizedTest; |
|||
import org.junit.jupiter.params.provider.Arguments; |
|||
import org.junit.jupiter.params.provider.MethodSource; |
|||
|
|||
class PlayerManagerTest { |
|||
|
|||
PlayerManager manager = new PlayerManager(); |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testCasesForSelectPlayer") |
|||
void selectPlayerTest(String testName, List<Player> players, String playerName, Player expectedResult) { |
|||
manager.setPlayers(players); |
|||
Player calculatedResult = manager.selectPlayer(playerName); |
|||
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); |
|||
} |
|||
|
|||
private static Stream<Arguments> testCasesForSelectPlayer() { |
|||
return Stream.of(Arguments.of("NoPlayersYetReturnNewPlayer", new LinkedList<Player>(List.of()), |
|||
"Tobias", new Player("Tobias", 0)), |
|||
Arguments.of("NoPlayerWithNameInListReturnNewPlayer", new LinkedList<Player>(List.of(new Player("Steffen", 40), new Player("Lorenz", 60))), |
|||
"Tobias", new Player("Tobias", 0)), |
|||
Arguments.of("PlayerWithNameInListReturnPlayerInList", new LinkedList<Player>(List.of(new Player("Steffen", 40), new Player("Tobias", 50))), |
|||
"Tobias", new Player("Tobias", 50))); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testCasesForLoadPlayers") |
|||
void loadPlayersTest(String testName, String fileContent, String fileName, List<Player> expectedResult) { |
|||
File playerData = new File(fileName); |
|||
|
|||
try { |
|||
FileWriter fw = new FileWriter(playerData, false); |
|||
fw.write(fileContent); |
|||
fw.close(); |
|||
} catch (IOException e) { |
|||
fail("Cannot open file"); |
|||
} |
|||
|
|||
manager.loadPlayers(fileName); |
|||
List<Player> calculatedResult = manager.getPlayers(); |
|||
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); |
|||
} |
|||
|
|||
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("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)))); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("testCasesForSavePlayers") |
|||
void savePlayersTest(String testName, String fileName, List<Player> expectedResult) { |
|||
manager.setPlayers(expectedResult); |
|||
manager.savePlayers(fileName); |
|||
manager.loadPlayers(fileName); |
|||
List<Player> calculatedResult = manager.getPlayers(); |
|||
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); |
|||
} |
|||
|
|||
private static Stream<Arguments> testCasesForSavePlayers() { |
|||
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)))); |
|||
} |
|||
|
|||
@Test |
|||
void selectPlayerAddsNewPlayersToList() { |
|||
LinkedList<Player> players = new LinkedList<Player>(); |
|||
players.add(new Player("Steffen", 40)); |
|||
players.add(new Player("Max", 60)); |
|||
|
|||
manager.setPlayers(players); |
|||
manager.selectPlayer("Tobias"); |
|||
|
|||
List<Player> expectedResult = List.of(new Player("Steffen", 40), new Player("Max", 60), new Player("Tobias", 0)); |
|||
List<Player> calculatedResult = manager.getPlayers(); |
|||
|
|||
assertThat(calculatedResult).describedAs("SelectNewPlayerAddsNewPlayerToPlayersList").isEqualTo(expectedResult); |
|||
} |
|||
|
|||
@Test |
|||
void selectExistingPlayerDoesntChangeTheList() { |
|||
LinkedList<Player> players = new LinkedList<Player>(); |
|||
players.add(new Player("Steffen", 40)); |
|||
players.add(new Player("Max", 60)); |
|||
|
|||
manager.setPlayers(players); |
|||
manager.selectPlayer("Max"); |
|||
|
|||
List<Player> expectedResult = List.of(new Player("Steffen", 40), new Player("Max", 60)); |
|||
List<Player> calculatedResult = manager.getPlayers(); |
|||
|
|||
assertThat(calculatedResult).describedAs("SelectNewPlayerAddsNewPlayerToPlayersList").isEqualTo(expectedResult); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,3 @@ |
|||
Tobias;50 |
|||
Lorenz;40 |
|||
Steffen;60 |
@ -0,0 +1,2 @@ |
|||
Tobias;50 |
|||
Steffen;60 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue