ex-rep-02/Highscore/HighscoreProcessing.cs
github-classroom[bot] 962f0fd813
Initial commit
2024-09-12 13:40:11 +00:00

91 lines
No EOL
3.3 KiB
C#

namespace Highscore;
/// <summary>
/// Processing of gamer scores to determine an overall highscore
/// </summary>
public static class HighscoreProcessing
{
private const char Separator = ';';
/// <summary>
/// Executes the <see cref="HighscoreProcessing"/> program.
/// </summary>
public static void Run()
{
var scores = LoadScoresFromFile("Data/scores.csv");
var bestScores = FindBestScorePerUser(scores);
SortGameScores(bestScores);
WriteOutputFileAndPrint(bestScores, "Data/highscore.csv");
}
/// <summary>
/// Writes the provided <see cref="GameScore"/> instances to a file at the supplied path.
/// The CSV format is Score;Date;Player.
/// </summary>
/// <param name="highscore">Game scores to persist</param>
/// <param name="filePath">Path to write to</param>
public static void WriteOutputFileAndPrint(GameScore[] highscore, string filePath)
{
// TODO
}
/// <summary>
/// Loads the CSV file at the supplied path and attempts to parse its content to
/// create an array of <see cref="GameScore"/> instances utilizing <see cref="TryParseGameScore"/>.
/// </summary>
/// <param name="filePath">Path to read from</param>
/// <returns>Array of parsed items; empty array if file does not exist or is empty</returns>
public static GameScore[] LoadScoresFromFile(string filePath)
{
// TODO
return [];
}
/// <summary>
/// Attempts to parse a CSV formatted string to a <see cref="GameScore"/> object.
/// </summary>
/// <param name="line">String to parse</param>
/// <param name="gameScore">Successfully parsed object; null if parsing fails</param>
/// <returns>True if parsed successfully; false otherwise</returns>
public static bool TryParseGameScore(string line, out GameScore? gameScore)
{
// TODO
gameScore = null;
return false;
}
/// <summary>
/// Based on the provided full set of game scores for each user the best score is determined.
/// If a user has two or more entries with the same score the oldest (earliest) entry is considered to
/// be the highest.
/// </summary>
/// <param name="allScores">Collection of all scores; may contain multiple entries for one user</param>
/// <returns>An array containing the highest score of each user, based on passed scores</returns>
public static GameScore[] FindBestScorePerUser(GameScore[] allScores)
{
// TODO
return [];
}
/// <summary>
/// Sorts the provided <see cref="GameScore"/> instances so that those with the
/// highest score (older date is preferred if score is equal) are ranked first.
/// Sorting happens in-place.
/// </summary>
/// <param name="scores">Scores to sort</param>
public static void SortGameScores(GameScore[] scores)
{
// TODO
}
/// <summary>
/// Trims empty (null) entries from the provided array.
/// </summary>
/// <param name="arrayToTrim">The array to process</param>
/// <returns>A new array containing only the non-null entries from the passed array</returns>
public static GameScore[] TrimArray(GameScore?[] arrayToTrim)
{
// TODO
return [];
}
}