Skip to content

Commit

Permalink
[DigiTally] Show percentage of max points per candidate after each ca…
Browse files Browse the repository at this point in the history
…ndidate
  • Loading branch information
ermshiperete committed Jun 13, 2024
1 parent 28fcfe6 commit 7f07cab
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
8 changes: 4 additions & 4 deletions DigiTallyTests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public void EndToEnd()
Election1
---------
1. Dagobert Duck (4 points)
2. Mickey Mouse (3 points)
Donald Duck (2 points)
Daisy Duck (0 points)
1. Dagobert Duck (4 points, 66%)
2. Mickey Mouse (3 points, 50%)
Donald Duck (2 points, 33%)
Daisy Duck (0 points, 0%)
(3 ballots, thereof 0 invalid)
(max 6 points per candidate)
(max 9 points total, sum 9 points, 0 unallocated)
Expand Down
2 changes: 1 addition & 1 deletion DigiTallyTests/ReadBallotsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void GetResultString_Valid_OnlyOneNominee()
Assert.That(election1.Key.Invalid, Is.EqualTo(0));
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Mickey Mouse (1 points)
1. Mickey Mouse (1 points, 50%)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 1 points, 2 unallocated)
Expand Down
24 changes: 12 additions & 12 deletions DigiTallyTests/ReadBallotsTests_Weighted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ public void GetResultString()
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Dagobert Duck (2 points)
2. Mickey Mouse (1 points)
Daisy Duck (0 points)
Donald Duck (0 points)
1. Dagobert Duck (2 points, 100%)
2. Mickey Mouse (1 points, 50%)
Daisy Duck (0 points, 0%)
Donald Duck (0 points, 0%)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 3 points, 0 unallocated)
Expand Down Expand Up @@ -341,10 +341,10 @@ public void GetResultString_SameVotes()
Assert.That(sut.AddBallot(ballotFileName2), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Dagobert Duck (3 points)
1. Mickey Mouse (3 points)
Daisy Duck (0 points)
Donald Duck (0 points)
1. Dagobert Duck (3 points, 75%)
1. Mickey Mouse (3 points, 75%)
Daisy Duck (0 points, 0%)
Donald Duck (0 points, 0%)
(2 ballots, thereof 0 invalid)
(max 4 points per candidate)
(max 6 points total, sum 6 points, 0 unallocated)
Expand Down Expand Up @@ -375,10 +375,10 @@ public void GetResultString_Abstains()
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
Dagobert Duck (0 points)
Daisy Duck (0 points)
Donald Duck (0 points)
Mickey Mouse (0 points)
Dagobert Duck (0 points, 0%)
Daisy Duck (0 points, 0%)
Donald Duck (0 points, 0%)
Mickey Mouse (0 points, 0%)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 0 points, 3 unallocated)
Expand Down
7 changes: 5 additions & 2 deletions DigitaleBriefwahl/Model/WeightedElectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,17 @@ 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)");
bldr.AppendLine($"{placing} {candidate} ({weightedResult.Points} points, " +
$"{weightedResult.Points * 100 / MaxPointsPerCandidate}%)");
SumTotalPoints += weightedResult.Points;
}

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

private int MaxPointsPerCandidate => (BallotsProcessed - Invalid) * Votes;

private int MaxTotalPoints
{
get
Expand All @@ -243,7 +246,7 @@ protected override string GetResultStringSummary(Dictionary<string, CandidateRes
{
var bldr = new StringBuilder();
bldr.AppendLine($"({base.GetResultStringSummary(results)})");
bldr.AppendLine($"(max {(BallotsProcessed - Invalid) * Votes} points per candidate)");
bldr.AppendLine($"(max {MaxPointsPerCandidate} points per candidate)");
bldr.Append ($"(max {MaxTotalPoints} points total, sum {SumTotalPoints} points, {UnallocatedPoints} unallocated)");
return bldr.ToString();
}
Expand Down
24 changes: 12 additions & 12 deletions DigitaleBriefwahlTests/Model/WeightedElectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,30 +234,30 @@ public void ReadVotesFromBallot(string ballot, string[] expectedResultStrings)
Assert.That(results, Is.EqualTo(expectedResults));
}

[TestCase(BallotDagobertMickey, ExpectedResult = @"1. Dagobert Duck (2 points)
2. Mickey Mouse (1 points)
Donald Duck (0 points)
[TestCase(BallotDagobertMickey, ExpectedResult = @"1. Dagobert Duck (2 points, 100%)
2. Mickey Mouse (1 points, 50%)
Donald Duck (0 points, 0%)
(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)
[TestCase(BallotDonaldDagobert, ExpectedResult = @"1. Donald Duck (2 points, 100%)
2. Dagobert Duck (1 points, 50%)
Mickey Mouse (0 points, 0%)
(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)
[TestCase(BallotDonaldOnly, ExpectedResult = @"1. Donald Duck (2 points, 100%)
Dagobert Duck (0 points, 0%)
Mickey Mouse (0 points, 0%)
(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)
[TestCase(BallotNone, ExpectedResult = @" Dagobert Duck (0 points, 0%)
Donald Duck (0 points, 0%)
Mickey Mouse (0 points, 0%)
(1 ballots, thereof 0 invalid)
(max 2 points per candidate)
(max 3 points total, sum 0 points, 3 unallocated)
Expand Down

0 comments on commit 7f07cab

Please sign in to comment.