Finished Refactoring Statistics
parent
578bebe7b1
commit
d1e8dfa9f4
|
@ -0,0 +1 @@
|
|||
*Book.txt
|
|
@ -56,7 +56,7 @@ namespace GradeBook
|
|||
{
|
||||
using(var writer = File.AppendText($"{Name}.txt"))
|
||||
{
|
||||
writer.WriteLine("Test");
|
||||
writer.WriteLine(grade);
|
||||
if (GradeAdded != null)
|
||||
{
|
||||
GradeAdded(this, new EventArgs());
|
||||
|
@ -66,7 +66,19 @@ namespace GradeBook
|
|||
|
||||
public override Statistics GetStatistics()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var result = new Statistics();
|
||||
using (var reader = File.OpenText($"{Name}.txt"))
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
while(line != null)
|
||||
{
|
||||
var number = double.Parse(line);
|
||||
result.Add(number);
|
||||
line = reader.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public class InMemoryBook : Book, IBook
|
||||
|
@ -124,36 +136,10 @@ namespace GradeBook
|
|||
public override Statistics GetStatistics()
|
||||
{
|
||||
var result = new Statistics();
|
||||
result.Average = 0.0;
|
||||
result.highGrade = double.MinValue;
|
||||
result.lowGrade = double.MaxValue;
|
||||
|
||||
foreach (var grade in this.grades)
|
||||
{
|
||||
result.lowGrade = Math.Min(grade, result.lowGrade);
|
||||
result.highGrade = Math.Max(grade, result.highGrade);
|
||||
result.Average += grade;
|
||||
}
|
||||
|
||||
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;
|
||||
result.Add(grade);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace GradeBook
|
|||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
var book = new InMemoryBook("Jason's Grade Book");
|
||||
var book = new DiskBook("Jason's Grade Book");
|
||||
book.GradeAdded += OnGradeAdded;
|
||||
|
||||
string input;
|
||||
|
|
|
@ -1,10 +1,54 @@
|
|||
using System;
|
||||
|
||||
namespace GradeBook
|
||||
{
|
||||
public class Statistics
|
||||
{
|
||||
public double Average;
|
||||
public double Average
|
||||
{
|
||||
get
|
||||
{
|
||||
return Sum / Count;
|
||||
}
|
||||
}
|
||||
public double highGrade;
|
||||
public double lowGrade;
|
||||
public char Letter;
|
||||
public char Letter
|
||||
{
|
||||
get{
|
||||
switch (Average)
|
||||
{
|
||||
case var d when d >= 90.0:
|
||||
return 'A';
|
||||
case var d when d >= 80.0:
|
||||
return 'B';
|
||||
case var d when d >= 70.0:
|
||||
return 'C';
|
||||
case var d when d >= 60.0:
|
||||
return 'D';
|
||||
default:
|
||||
return 'F';
|
||||
}
|
||||
}
|
||||
}
|
||||
public double Sum;
|
||||
public int Count;
|
||||
|
||||
public void Add(double number)
|
||||
{
|
||||
Sum += number;
|
||||
Count += 1;
|
||||
|
||||
lowGrade = Math.Min(number, lowGrade);
|
||||
highGrade = Math.Max(number, highGrade);
|
||||
}
|
||||
|
||||
public Statistics()
|
||||
{
|
||||
Count = 0;
|
||||
Sum = 0.0;
|
||||
highGrade = double.MinValue;
|
||||
lowGrade = double.MaxValue;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue