From 4503e7f3249d9510905d19d66cd931d7234489d6 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Wed, 17 Feb 2021 12:33:05 +0000 Subject: [PATCH] Finished Testing Your Code, Refactoring for Testability --- gradebook/src/GradeBook/Book.cs | 30 ++++++++++++--------- gradebook/src/GradeBook/Program.cs | 7 ++++- gradebook/src/GradeBook/Statistics.cs | 9 +++++++ gradebook/test/GradeBook.Tests/BookTests.cs | 7 ++++- 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 gradebook/src/GradeBook/Statistics.cs diff --git a/gradebook/src/GradeBook/Book.cs b/gradebook/src/GradeBook/Book.cs index 01cee3d..04087f8 100644 --- a/gradebook/src/GradeBook/Book.cs +++ b/gradebook/src/GradeBook/Book.cs @@ -3,6 +3,12 @@ using System.Collections.Generic; namespace GradeBook { + // public class Statistics + // { + // public double Average; + // public double highGrade; + // public double lowGrade; + // } public class Book { private List grades; @@ -19,24 +25,22 @@ namespace GradeBook this.grades.Add(grade); } - public void ShowStaticstics() + public Statistics GetStatistics() { - - double result = 0; - var highGrade = double.MinValue; - var lowGrade = double.MaxValue; + var result = new Statistics(); + result.Average = 0.0; + result.highGrade = double.MinValue; + result.lowGrade = double.MaxValue; - foreach (var number in this.grades) + foreach (var grade in this.grades) { - highGrade = Math.Max(number, highGrade); - lowGrade = Math.Min(number, lowGrade); - result += number; + result.lowGrade = Math.Min(grade, result.lowGrade); + result.highGrade = Math.Max(grade, result.highGrade); + result.Average += grade; } - result /= grades.Count; - Console.WriteLine($"The average grade is {result:N1}"); - Console.WriteLine($"The highest grade is {highGrade:N1}"); - Console.WriteLine($"The lowest grade is {lowGrade:N1}"); + result.Average /= grades.Count; + return result; } } diff --git a/gradebook/src/GradeBook/Program.cs b/gradebook/src/GradeBook/Program.cs index 912241c..9ac665c 100644 --- a/gradebook/src/GradeBook/Program.cs +++ b/gradebook/src/GradeBook/Program.cs @@ -12,7 +12,12 @@ namespace GradeBook book.AddGrade(89.1); book.AddGrade(90.5); book.AddGrade(87.2); - book.ShowStaticstics(); + + var stats = book.GetStatistics(); + + Console.WriteLine($"The lowest grade is {stats.lowGrade}"); + Console.WriteLine($"The highest grade is {stats.highGrade}"); + Console.WriteLine($"The average grade is {stats.Average:N1}"); } } } diff --git a/gradebook/src/GradeBook/Statistics.cs b/gradebook/src/GradeBook/Statistics.cs new file mode 100644 index 0000000..4e6af93 --- /dev/null +++ b/gradebook/src/GradeBook/Statistics.cs @@ -0,0 +1,9 @@ +namespace GradeBook +{ + public class Statistics + { + public double Average; + public double highGrade; + public double lowGrade; + } +} \ No newline at end of file diff --git a/gradebook/test/GradeBook.Tests/BookTests.cs b/gradebook/test/GradeBook.Tests/BookTests.cs index 9687807..3d6e91e 100644 --- a/gradebook/test/GradeBook.Tests/BookTests.cs +++ b/gradebook/test/GradeBook.Tests/BookTests.cs @@ -16,7 +16,12 @@ namespace GradeBook.Tests book.AddGrade(77.3); // act - book.ShowStaticstics(); + var result = book.GetStatistics(); + + // assert + Assert.Equal(85.6, result.Average, 1); + Assert.Equal(90.5, result.highGrade); + Assert.Equal(77.3, result.lowGrade); } } }