package meganetpo;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Burtovoy Semen
*/
class PlayersRepository {
private List<Player> playerList = new ArrayList<Player>();
Player get(String nick) {
Player pl = new Player();
pl.setName(nick);
playerList.add(pl);
return pl;
}
public void remove(String nick) {
for (Player player : playerList) {
if (player.getName().equals(nick)) {
playerList.remove(player);
}
}
}
public void remove(Player player) {
playerList.remove(player);
}
public List<Player> getPlayerList() {
return playerList;
}
public void setPlayerList(List<Player> playerList) {
this.playerList = playerList;
}
public void save(String fileName) throws FileNotFoundException {
FileOutputStream out = new FileOutputStream(fileName);
XMLEncoder xmlEncoder = new XMLEncoder(out);
xmlEncoder.writeObject(playerList);
xmlEncoder.flush();
xmlEncoder.close();
}
public void load(String fileName) throws FileNotFoundException {
FileInputStream in = new FileInputStream(fileName);
XMLDecoder xmlDecoder = new XMLDecoder(in);
playerList = (List<Player>) xmlDecoder.readObject();
xmlDecoder.close();
}
}