package nl.github.martijn9612.fishy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import nl.github.martijn9612.fishy.models.Score;

public class ScoreController {
	private static final String SCORE_FILE_PATH = "highscores.ser";

	private List<Score> scoreList = new ArrayList<Score>();
	
	public ScoreController() {
		File savedScores = new File(SCORE_FILE_PATH);
		if(savedScores.isFile()) {
			unserialize();
		}
	}

	public void addScore(Score score) {
		scoreList.add(score);
		Collections.sort(scoreList);
		serialize();
	}

	public void removeScore(Score score) {
		int index = scoreList.indexOf(score);
		if (index >= 0) {
			scoreList.remove(index);
			serialize();
		}
	}

	public List<Score> getScoreList() {
		return scoreList;
	}
	
	private void serialize() {
		try {
			FileOutputStream fileStream = new FileOutputStream(SCORE_FILE_PATH);
			ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
			objectStream.writeObject(scoreList);
			objectStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	private void unserialize() {
		try {
			FileInputStream fileInput = new FileInputStream(SCORE_FILE_PATH);
			ObjectInputStream objectInput = new ObjectInputStream(fileInput);
			scoreList = (ArrayList<Score>) objectInput.readObject();
			objectInput.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

@@ -384,16 +384,189 @@
 Score;%0A%0A
+/**%0A * The ScoreController handles the logic for maintaining a list of high%0A * scores and ensuring this list is persistently stored into a file.%0A * @author Leon Noordam%0A */%0A
 public c
@@ -653,17 +653,16 @@
 s.ser%22;%0A
-%0A
 %09private
@@ -717,462 +717,1120 @@
 %0A%09%0A%09
-public ScoreController() %7B%0A%09%09File savedScores = new File(SCORE_FILE_PATH);%0A%09%09if(savedScores.isFile()) %7B%0A%09%09%09unserialize();%0A%09%09%7D%0A%09%7D%0A%0A%09public void addScore(Score score) %7B%0A%09%09scoreList.add(score);%0A%09%09Collections.sort(scoreList);%0A%09%09serialize();%0A%09%7D%0A%0A%09public void removeScore(Score score) %7B%0A%09%09int index = scoreList.indexOf(score);%0A%09%09if (index %3E= 0) %7B%0A%09%09%09scoreList.remove(index);%0A%09%09%09serialize();%0A%09%09%7D%0A%09%7D%0A%0A%09public List%3CScore%3E getScoreList() %7B%0A%09%09return scoreList;%0A%09%7D%0A%09
+/**%0A%09 * Create a new instance of the ScoreController. The constructor%0A%09 * searches for the saved scores and will load them when found.%0A%09 */%0A%09public ScoreController() %7B%0A%09%09File savedScores = new File(SCORE_FILE_PATH);%0A%09%09if(savedScores.isFile()) %7B%0A%09%09%09unserialize();%0A%09%09%7D%0A%09%7D%0A%09%0A%09/**%0A%09 * Adds a new Score object to the list of scores. The list%0A%09 * with scores is automatically sorted and written to a file.%0A%09 * @param score instance of the Score class.%0A%09 */%0A%09public void addScore(Score score) %7B%0A%09%09scoreList.add(score);%0A%09%09Collections.sort(scoreList);%0A%09%09serialize();%0A%09%7D%0A%09%0A%09/**%0A%09 * Removes a Score object from the list with scores. Changes%0A%09 * made to the list are automatically written to a file.%0A%09 * @param score instance of the Score class.%0A%09 */%0A%09public void removeScore(Score score) %7B%0A%09%09int index = scoreList.indexOf(score);%0A%09%09if (index %3E= 0) %7B%0A%09%09%09scoreList.remove(index);%0A%09%09%09serialize();%0A%09%09%7D%0A%09%7D%0A%09%0A%09/**%0A%09 * Get the list of scores.%0A%09 * @return List%3CScore%3E object with scores.%0A%09 */%0A%09public List%3CScore%3E getScoreList() %7B%0A%09%09return scoreList;%0A%09%7D%0A%09%0A%09/**%0A%09 * Writes the arrayList defined as field in this class to a file.%0A%09 */
 %0A%09pr
@@ -2195,16 +2195,172 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Retrieves the arrayList defined as field in this class from a file.%0A%09 * The unchecked warning is suppressed because Java can't know the type.%0A%09 */%0A
 %09@Suppre

Add javadoc to ScoreController class <documentation