Finished Testing Your Code, Refactoring for Testability
parent
41bf0f3824
commit
b6387e0e8c
|
@ -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<double> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
namespace GradeBook
|
||||
{
|
||||
public class Statistics
|
||||
{
|
||||
public double Average;
|
||||
public double highGrade;
|
||||
public double lowGrade;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue