Skip to content
Snippets Groups Projects

Added PercentageConverter and AverageMarkConverter with tests

Merged Fabrice Bosshard requested to merge 80_ValueConverters into master
6 files
+ 166
1
Compare changes
  • Side-by-side
  • Inline
Files
6
using StudiApp.Models;
namespace StudiApp.Services.Converters
{
public static class AverageMarkConverter
{
public static double MarksToAverage(List<Mark> marks)
{
if (marks.Any(m=> m.Value is > 6.0 or < 1.0))
{
throw new ArgumentException("A Value in the marks list is over 6.0 or under 1.0");
}
if (marks.Any(m => m.Percentage is > 1.0 or < 0.01))
{
throw new ArgumentException("A Percentage in the marks list is over 100% (1.0) or under 1% (0.01)");
}
var average = marks.Sum(mark => mark.Value * mark.Percentage);
return Math.Round(average, 2);
}
}
}