Last active
June 8, 2021 20:40
-
-
Save Deneas/d5b89f2047dd360b47de8436f49dc620 to your computer and use it in GitHub Desktop.
Revisions
-
Deneas revised this gist
Jun 8, 2021 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,7 @@ public void AddScore(Student student, int score) Scores.Add(new StudentScore(student, score)); return; } var existingScore = Scores.First(_ => _.Student.Equals(student)); existingScore.AddScore(score); } -
Deneas created this gist
Jun 6, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ // Licensed under the MIT License public class Scoreboard { public readonly List<StudentScore> Scores = new List<StudentScore>(); // inspired by AddItem method in https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/ApplicationCore/Entities/BasketAggregate/Basket.cs public void AddScore(Student student, int score) { if (!Scores.Any(_ => _.Student.Equals(student))) { Scores.Add(new StudentScore(student, score)); return; } // We cannot kill the mutation FirstOrDefault, because it actually is not valid. // The previous if statement ensures that the score exists, thus we will never get null. var existingScore = Scores.First(_ => _.Student.Equals(student)); existingScore.AddScore(score); } public int GetScore(Student student) { return Scores.Where(_ => _.Student.Equals(student)) .Select(_ => _.Score) .FirstOrDefault(); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ // Licensed under the MIT License public class Scoreboard { public readonly List<StudentScore> Scores = new List<StudentScore>(); // inspired by AddItem method in https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/ApplicationCore/Entities/BasketAggregate/Basket.cs public void AddScore(Student student, int score) { if (!Scores.Any(_ => _.Student.Equals(student))) { Scores.Add(new StudentScore(student, score)); return; } var existingScore = Scores.FirstOrDefault(_ => _.Student.Equals(student)); existingScore.AddScore(score); } public int GetScore(Student student) { return Scores.Where(_ => _.Student.Equals(student)) .Select(_ => _.Score) .FirstOrDefault(); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ // Licensed under the MIT License public class StudentScoringTest { [Fact] public void TwoScores_AreAddedUp() { var student = new Student(18, true); var firstScore = 5; var secondScore = 8; var expectedScore = 13; var scoreboard = new Scoreboard(); scoreboard.AddScore(student, firstScore); scoreboard.AddScore(student, secondScore); var finalScore = scoreboard.GetScore(student); Assert.Equal(expectedScore, finalScore); } [Fact] public void NoScores_Zero() { var student = new Student(18, true); var expectedScore = 0; var scoreboard = new Scoreboard(); var finalScore = scoreboard.GetScore(student); Assert.Equal(expectedScore, finalScore); } }