Skip to content

Commit

Permalink
[DigiTally] Output total max points, sum of points and unallocated po…
Browse files Browse the repository at this point in the history
…ints
  • Loading branch information
ermshiperete committed Jun 13, 2024
1 parent 4159912 commit 28fcfe6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
4 changes: 3 additions & 1 deletion DigiTallyTests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ 1. Dagobert Duck (4 points)
2. Mickey Mouse (3 points)
Donald Duck (2 points)
Daisy Duck (0 points)
(3 ballots, thereof 0 invalid; max 6 points)
(3 ballots, thereof 0 invalid)
(max 6 points per candidate)
(max 9 points total, sum 9 points, 0 unallocated)
Election2
---------
Expand Down
4 changes: 3 additions & 1 deletion DigiTallyTests/ReadBallotsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public void GetResultString_Valid_OnlyOneNominee()
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Mickey Mouse (1 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 1 points, 2 unallocated)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
Expand Down
12 changes: 9 additions & 3 deletions DigiTallyTests/ReadBallotsTests_Weighted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ 1. Dagobert Duck (2 points)
2. Mickey Mouse (1 points)
Daisy Duck (0 points)
Donald Duck (0 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 3 points, 0 unallocated)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
Expand Down Expand Up @@ -343,7 +345,9 @@ 1. Dagobert Duck (3 points)
1. Mickey Mouse (3 points)
Daisy Duck (0 points)
Donald Duck (0 points)
(2 ballots, thereof 0 invalid; max 4 points)
(2 ballots, thereof 0 invalid)
(max 4 points per candidate)
(max 6 points total, sum 6 points, 0 unallocated)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
Expand Down Expand Up @@ -375,7 +379,9 @@ Dagobert Duck (0 points)
Daisy Duck (0 points)
Donald Duck (0 points)
Mickey Mouse (0 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 0 points, 3 unallocated)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
Expand Down
23 changes: 22 additions & 1 deletion DigitaleBriefwahl/Model/WeightedElectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public override string GetResultString(Dictionary<string, CandidateResult> resul
var bldr = new StringBuilder();
var comparer = new RankComparer(results.ToList());
var orderedResults = results.OrderByDescending(kv => kv, comparer).ToList();
SumTotalPoints = 0;
foreach (var kv in orderedResults)
{
var candidate = kv.Key;
Expand All @@ -216,15 +217,35 @@ public override string GetResultString(Dictionary<string, CandidateResult> resul
var rank = GetRank(orderedResults, candidate);
var placing = rank <= Votes ? $"{rank}." : " ";
bldr.AppendLine($"{placing} {candidate} ({weightedResult.Points} points)");
SumTotalPoints += weightedResult.Points;
}

bldr.AppendLine(GetResultStringSummary(results));
return bldr.ToString();
}

private int MaxTotalPoints
{
get
{
var sum = 0;
for (var i = 1; i <= Votes; i++)
sum += i;
return sum * (BallotsProcessed - Invalid);
}
}

private int SumTotalPoints { get; set; }

private int UnallocatedPoints => MaxTotalPoints - SumTotalPoints;

protected override string GetResultStringSummary(Dictionary<string, CandidateResult> results)
{
return $"({base.GetResultStringSummary(results)}; max {(BallotsProcessed - Invalid) * Votes} points)";
var bldr = new StringBuilder();
bldr.AppendLine($"({base.GetResultStringSummary(results)})");
bldr.AppendLine($"(max {(BallotsProcessed - Invalid) * Votes} points per candidate)");
bldr.Append ($"(max {MaxTotalPoints} points total, sum {SumTotalPoints} points, {UnallocatedPoints} unallocated)");
return bldr.ToString();
}

public override bool SkipNominee(string name, int iVote)
Expand Down
16 changes: 12 additions & 4 deletions DigitaleBriefwahlTests/Model/WeightedElectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,30 @@ public void ReadVotesFromBallot(string ballot, string[] expectedResultStrings)
[TestCase(BallotDagobertMickey, ExpectedResult = @"1. Dagobert Duck (2 points)
2. Mickey Mouse (1 points)
Donald Duck (0 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 3 points, 0 unallocated)
")]
[TestCase(BallotDonaldDagobert, ExpectedResult = @"1. Donald Duck (2 points)
2. Dagobert Duck (1 points)
Mickey Mouse (0 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 3 points, 0 unallocated)
")]
[TestCase(BallotDonaldOnly, ExpectedResult = @"1. Donald Duck (2 points)
Dagobert Duck (0 points)
Mickey Mouse (0 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 2 points, 1 unallocated)
")]
[TestCase(BallotNone, ExpectedResult = @" Dagobert Duck (0 points)
Donald Duck (0 points)
Mickey Mouse (0 points)
(1 ballots, thereof 0 invalid; max 2 points)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 0 points, 3 unallocated)
")]
public string GetResultString(string ballot)
{
Expand Down

0 comments on commit 28fcfe6

Please sign in to comment.