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