diff --git a/gradebook/.gitignore b/gradebook/.gitignore new file mode 100644 index 0000000..38cf8cc --- /dev/null +++ b/gradebook/.gitignore @@ -0,0 +1 @@ +*Book.txt \ No newline at end of file diff --git a/gradebook/src/GradeBook/Book.cs b/gradebook/src/GradeBook/Book.cs index 58a0e03..186e120 100644 --- a/gradebook/src/GradeBook/Book.cs +++ b/gradebook/src/GradeBook/Book.cs @@ -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; diff --git a/gradebook/src/GradeBook/Program.cs b/gradebook/src/GradeBook/Program.cs index 96c4ad9..04e4f8e 100644 --- a/gradebook/src/GradeBook/Program.cs +++ b/gradebook/src/GradeBook/Program.cs @@ -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; diff --git a/gradebook/src/GradeBook/Statistics.cs b/gradebook/src/GradeBook/Statistics.cs index c743dd0..f800bb9 100644 --- a/gradebook/src/GradeBook/Statistics.cs +++ b/gradebook/src/GradeBook/Statistics.cs @@ -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; + } } } \ No newline at end of file