Browse Source

leaderboard: getTop10Players returns first 10 Elements of sorted list

leaderboard
Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
1e724d8e80
  1. 6
      src/main/java/de/tims/leaderboard/Leaderboard.java
  2. 11
      src/test/java/de/tims/leaderboard/LeaderboardTest.java

6
src/main/java/de/tims/leaderboard/Leaderboard.java

@ -6,6 +6,8 @@ import de.tims.player_management.Player;
public class Leaderboard {
private static final int NUM_PLAYERS_ON_LEADERBOARD = 10;
private List<Player> allPlayers;
public Leaderboard(List<Player> allPlayers, String actualPlayer) {
@ -13,9 +15,9 @@ public class Leaderboard {
}
public Player[] getTop10Players() {
Player[] top10Players = new Player[10];
Player[] top10Players = new Player[NUM_PLAYERS_ON_LEADERBOARD];
for (int i = 0; i < allPlayers.size(); i++) {
for (int i = 0; i < allPlayers.size() && i < NUM_PLAYERS_ON_LEADERBOARD; i++) {
top10Players[i] = allPlayers.get(i);
}

11
src/test/java/de/tims/leaderboard/LeaderboardTest.java

@ -31,7 +31,16 @@ class LeaderboardTest {
new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10)),
"TestPlayer",
new Player[] {new Player("Eins", 80), new Player("Zwei", 70), new Player("Drei", 60), new Player("Vier", 50),
new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10), null, null}));
new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10), null, null}),
Arguments.of("SortedListWithMoreThan10ElementsReturnsFirst10ElementsAsArray",
List.of(new Player("Eins", 140), new Player("Zwei", 130), new Player("Drei", 120), new Player("Vier", 110),
new Player("Fuenf", 100), new Player("Sechs", 90), new Player("Sieben", 80), new Player("Acht", 70),
new Player("Neun", 60), new Player("Zehn", 50), new Player("Elf", 40), new Player("Zwoelf", 30),
new Player("Dreizehn", 20), new Player("Vierzehn", 10)),
"TestPlayer",
new Player[] {new Player("Eins", 140), new Player("Zwei", 130), new Player("Drei", 120), new Player("Vier", 110),
new Player("Fuenf", 100), new Player("Sechs", 90), new Player("Sieben", 80), new Player("Acht", 70),
new Player("Neun", 60), new Player("Zehn", 50)}));
}
}
Loading…
Cancel
Save