From 0626a832d3c307deb932550472325e364f367ca0 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Tue, 16 Feb 2021 22:34:31 +0000 Subject: [PATCH] Encapsulate all methods within Book class --- gradebook/src/GradeBook/Book.cs | 21 +++++++++++++++++++++ gradebook/src/GradeBook/Program.cs | 21 ++------------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/gradebook/src/GradeBook/Book.cs b/gradebook/src/GradeBook/Book.cs index 911cc88..bfe7932 100644 --- a/gradebook/src/GradeBook/Book.cs +++ b/gradebook/src/GradeBook/Book.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace GradeBook @@ -17,6 +18,26 @@ namespace GradeBook { this.grades.Add(grade); } + + public void ShowStaticstics() + { + + double result = 0; + var highGrade = double.MinValue; + var lowGrade = double.MaxValue; + + foreach (var number in this.grades) + { + highGrade = Math.Max(number, highGrade); + lowGrade = Math.Min(number, lowGrade); + result += number; + } + 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}"); + } } } \ No newline at end of file diff --git a/gradebook/src/GradeBook/Program.cs b/gradebook/src/GradeBook/Program.cs index 3682124..912241c 100644 --- a/gradebook/src/GradeBook/Program.cs +++ b/gradebook/src/GradeBook/Program.cs @@ -11,25 +11,8 @@ namespace GradeBook var book = new Book("Scott's Grade Book"); book.AddGrade(89.1); book.AddGrade(90.5); - - List grades = new List() {12.7, 10.3, 6.11, 4.1}; - grades.Add(56.1); - double result = 0; - var highGrade = double.MinValue; - var lowGrade = double.MaxValue; - - foreach (var number in grades) - { - highGrade = Math.Max(number, highGrade); - lowGrade = Math.Min(number, lowGrade); - - result += number; - } - 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}"); + book.AddGrade(87.2); + book.ShowStaticstics(); } } }