From 41ca2586dc0061cf75e12610986be74712e855f1 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Fri, 19 Feb 2021 04:33:27 +0000 Subject: [PATCH] Finished Pattern Matching with switch --- gradebook/src/GradeBook/Book.cs | 29 ++++++++++++++------- gradebook/src/GradeBook/Statistics.cs | 1 + gradebook/test/GradeBook.Tests/BookTests.cs | 1 + 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/gradebook/src/GradeBook/Book.cs b/gradebook/src/GradeBook/Book.cs index caee2f5..45bca2b 100644 --- a/gradebook/src/GradeBook/Book.cs +++ b/gradebook/src/GradeBook/Book.cs @@ -52,16 +52,27 @@ namespace GradeBook result.Average += grade; } - // var index = 0; - // do - // { - // result.lowGrade = Math.Min(grades[index], result.lowGrade); - // result.highGrade = Math.Max(grades[index], result.highGrade); - // result.Average += grades[index]; - // index += 1; - // } while (index < grades.Count); - result.Average /= grades.Count; + + switch (result.Average) + { + case var d when d >= 90.0: + result.Letter = 'A'; + break; + case var d when d >= 80.0: + result.Letter = 'B'; + break; + case var d when d >= 70.0: + result.Letter = 'C'; + break; + case var d when d >= 60.0: + result.Letter = 'D'; + break; + default: + result.Letter = 'F'; + break; + } + return result; } } diff --git a/gradebook/src/GradeBook/Statistics.cs b/gradebook/src/GradeBook/Statistics.cs index 4e6af93..c743dd0 100644 --- a/gradebook/src/GradeBook/Statistics.cs +++ b/gradebook/src/GradeBook/Statistics.cs @@ -5,5 +5,6 @@ namespace GradeBook public double Average; public double highGrade; public double lowGrade; + public char Letter; } } \ No newline at end of file diff --git a/gradebook/test/GradeBook.Tests/BookTests.cs b/gradebook/test/GradeBook.Tests/BookTests.cs index 285d9b4..f329f85 100644 --- a/gradebook/test/GradeBook.Tests/BookTests.cs +++ b/gradebook/test/GradeBook.Tests/BookTests.cs @@ -21,6 +21,7 @@ namespace GradeBook.Tests Assert.Equal(85.6, result.Average, 1); Assert.Equal(90.5, result.highGrade); Assert.Equal(77.3, result.lowGrade); + Assert.Equal('B', result.Letter); } } }