Encapsulate all methods within Book class

jason.zhu 2021-02-16 22:34:31 +00:00
parent eedc0c5329
commit b88985d236
2 changed files with 23 additions and 19 deletions

View File

@ -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}");
}
}
}

View File

@ -11,25 +11,8 @@ namespace GradeBook
var book = new Book("Scott's Grade Book");
book.AddGrade(89.1);
book.AddGrade(90.5);
List<double> grades = new List<double>() {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();
}
}
}