ex-rep-01/EM-Qual/QualData.cs
2024-09-12 18:48:31 +02:00

145 lines
No EOL
5.2 KiB
C#

namespace EMQual;
public static class QualData
{
/// <summary>
/// Executes the <see cref="QualData"/> program.
/// </summary>
public static void Run()
{
var matches = ReadMatchesFromFile("Data/matches.csv");
PrintMatches(matches);
Console.WriteLine();
var countryNames = ExtractUniqueCountryNames(matches);
var selCountry = AskForCountry(countryNames);
var countryMatches = FilterMatchesByCountry(matches, selCountry);
PrintMatches(countryMatches);
}
/// <summary>
/// Reads match information from a CSV file at the provided location.
/// If the file does not exist or does not contain relevant or some
/// corrupted data an empty array is returned.
/// </summary>
/// <param name="filePath">The relative path to the CSV file</param>
/// <returns>An array containing parsed match data records</returns>
public static Match[] ReadMatchesFromFile(string filePath)
{
// TODO
return [];
}
/// <summary>
/// Attempts to parse a line with semicolon separated data for a match.
/// </summary>
/// <param name="line">Line to parse</param>
/// <param name="match">Parsed match object; null if parsing fails</param>
/// <returns>True if the line could be parsed successfully; false otherwise</returns>
public static bool TryParseMatch(string line, out Match? match)
{
// TODO
match = null;
return false;
}
/// <summary>
/// Determines if the supplied string is contained in the provided array of strings.
/// Case is ignored.
/// </summary>
/// <param name="value">Value to check for</param>
/// <param name="possibleValues">Array of values to check in</param>
/// <returns>True if the value is found; false otherwise</returns>
public static bool IsContained(string value, string[] possibleValues)
{
foreach (var possibleValue in possibleValues)
{
if (possibleValue.ToLower() == value.ToLower())
{
return true;
}
}
return false;
}
/// <summary>
/// Returns an array containing the names of all countries which have participated
/// in at least one match exactly one time.
/// </summary>
/// <param name="matches">Array of all matches played</param>
/// <returns>An array of unique country names</returns>
public static string[] ExtractUniqueCountryNames(Match[] matches)
{
// TODO
return [];
}
/// <summary>
/// Returns a new array of matches, based on the supplied full collection, but
/// with filtered to only contain matches the provided country participated in.
/// </summary>
/// <param name="allMatches">Array of all matches played</param>
/// <param name="countryName">Name of the country to filter for</param>
/// <returns>Array of matches in which the supplied country participated</returns>
public static Match[] FilterMatchesByCountry(Match[] allMatches, string countryName)
{
Match[] filteredMatches = [];
foreach (var match in allMatches)
{
if (IsCountryMatch(match, countryName))
{
filteredMatches = filteredMatches.Append(match).ToArray();
}
}
return filteredMatches;
}
/// <summary>
/// Determines if the supplied match is one the provided country participated in.
/// </summary>
/// <param name="match">Match to check</param>
/// <param name="countryName">Country for which to check</param>
/// <returns>True if the country participated in the match; false otherwise</returns>
public static bool IsCountryMatch(Match match, string countryName)
{
TeamResult homeTeam = match.HomeTeam;
TeamResult guestTeam = match.GuestTeam;
return homeTeam.TeamName == countryName || guestTeam.TeamName == countryName;
}
/// <summary>
/// Prints the matches to the terminal.
/// </summary>
/// <param name="matches">Array of matches to print</param>
private static void PrintMatches(Match[] matches)
{
foreach (var match in matches)
{
TeamResult homeTeam = match.HomeTeam;
TeamResult guestTeam = match.GuestTeam;
Console.WriteLine($"{homeTeam.TeamName} {homeTeam.Goals} - {guestTeam.Goals} {guestTeam.TeamName}");
}
}
/// <summary>
/// Asks the user to enter a country to filter for.
/// The user can only enter one of the existing countries. Case is not relevant.
/// If the user enters something invalid the input request is retried until the user gets it right.
/// </summary>
/// <param name="possibleCountries">Array containing the names of possible countries to pick from</param>
/// <returns>The name of the country the user selected</returns>
private static string AskForCountry(string[] possibleCountries)
{
string country;
do
{
Console.Write("Enter country: ");
country = Console.ReadLine()!.ToLower();
} while (!IsContained(country!, possibleCountries));
return country;
}
}