成绩+点名
This commit is contained in:
@@ -6,34 +6,45 @@ public static class GradeCalculator
|
||||
{
|
||||
public static bool AreWeightsValid(
|
||||
decimal regularWeight,
|
||||
decimal midtermWeight,
|
||||
decimal finalWeight) =>
|
||||
decimal finalWeight,
|
||||
IEnumerable<GradeItem> items) =>
|
||||
regularWeight is >= 0 and <= 100 &&
|
||||
midtermWeight is >= 0 and <= 100 &&
|
||||
finalWeight is >= 0 and <= 100 &&
|
||||
regularWeight + midtermWeight + finalWeight == 100;
|
||||
items.All(item => item.Weight is >= 0 and <= 100) &&
|
||||
regularWeight + finalWeight + items.Sum(item => item.Weight) == 100;
|
||||
|
||||
public static decimal? CalculateTotal(
|
||||
decimal? regularScore,
|
||||
decimal? midtermScore,
|
||||
decimal? finalScore,
|
||||
IReadOnlyCollection<GradeItemScore> itemScores,
|
||||
decimal regularWeight,
|
||||
decimal midtermWeight,
|
||||
decimal finalWeight,
|
||||
IReadOnlyCollection<GradeItem> items,
|
||||
GradeExamStatus examStatus)
|
||||
{
|
||||
if (examStatus != GradeExamStatus.Normal ||
|
||||
regularWeight > 0 && !regularScore.HasValue ||
|
||||
midtermWeight > 0 && !midtermScore.HasValue ||
|
||||
finalWeight > 0 && !finalScore.HasValue)
|
||||
{
|
||||
if (examStatus != GradeExamStatus.Normal)
|
||||
return null;
|
||||
|
||||
if (regularWeight > 0 && !regularScore.HasValue)
|
||||
return null;
|
||||
if (finalWeight > 0 && !finalScore.HasValue)
|
||||
return null;
|
||||
|
||||
var itemWeightById = items.ToDictionary(item => item.Id, item => item.Weight);
|
||||
foreach (var itemScore in itemScores)
|
||||
{
|
||||
if (itemWeightById.TryGetValue(itemScore.GradeItemId, out var weight) &&
|
||||
weight > 0 && !itemScore.Score.HasValue)
|
||||
return null;
|
||||
}
|
||||
|
||||
var total =
|
||||
(regularScore ?? 0) * regularWeight / 100 +
|
||||
(midtermScore ?? 0) * midtermWeight / 100 +
|
||||
(finalScore ?? 0) * finalWeight / 100;
|
||||
var total = (regularScore ?? 0) * regularWeight / 100;
|
||||
foreach (var itemScore in itemScores)
|
||||
{
|
||||
if (itemWeightById.TryGetValue(itemScore.GradeItemId, out var weight))
|
||||
total += (itemScore.Score ?? 0) * weight / 100;
|
||||
}
|
||||
total += (finalScore ?? 0) * finalWeight / 100;
|
||||
return Math.Round(total, 1, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user