Last active
June 8, 2021 20:40
-
-
Save Deneas/d5b89f2047dd360b47de8436f49dc620 to your computer and use it in GitHub Desktop.
Student Scoring Example
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 characters
| // 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.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 characters
| // 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 characters
| // 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment