Finished Refactoring Statistics

jason.zhu 2021-02-23 00:41:44 +00:00
parent 578bebe7b1
commit d1e8dfa9f4
4 changed files with 63 additions and 32 deletions

1
gradebook/.gitignore vendored 100644
View File

@ -0,0 +1 @@
*Book.txt

View File

@ -56,7 +56,7 @@ namespace GradeBook
{ {
using(var writer = File.AppendText($"{Name}.txt")) using(var writer = File.AppendText($"{Name}.txt"))
{ {
writer.WriteLine("Test"); writer.WriteLine(grade);
if (GradeAdded != null) if (GradeAdded != null)
{ {
GradeAdded(this, new EventArgs()); GradeAdded(this, new EventArgs());
@ -66,7 +66,19 @@ namespace GradeBook
public override Statistics GetStatistics() 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 public class InMemoryBook : Book, IBook
@ -124,36 +136,10 @@ namespace GradeBook
public override Statistics GetStatistics() public override Statistics GetStatistics()
{ {
var result = new Statistics(); var result = new Statistics();
result.Average = 0.0;
result.highGrade = double.MinValue;
result.lowGrade = double.MaxValue;
foreach (var grade in this.grades) foreach (var grade in this.grades)
{ {
result.lowGrade = Math.Min(grade, result.lowGrade); result.Add(grade);
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;
} }
return result; return result;

View File

@ -8,7 +8,7 @@ namespace GradeBook
static void Main(string[] args) static void Main(string[] args)
{ {
var book = new InMemoryBook("Jason's Grade Book"); var book = new DiskBook("Jason's Grade Book");
book.GradeAdded += OnGradeAdded; book.GradeAdded += OnGradeAdded;
string input; string input;

View File

@ -1,10 +1,54 @@
using System;
namespace GradeBook namespace GradeBook
{ {
public class Statistics public class Statistics
{ {
public double Average; public double Average
{
get
{
return Sum / Count;
}
}
public double highGrade; public double highGrade;
public double lowGrade; 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;
}
} }
} }