diff --git a/gradebook/src/GradeBook/Book.cs b/gradebook/src/GradeBook/Book.cs index e2f704b..c4c438d 100644 --- a/gradebook/src/GradeBook/Book.cs +++ b/gradebook/src/GradeBook/Book.cs @@ -18,7 +18,20 @@ namespace GradeBook set; } } - public class Book : NamedObject + + public abstract class Book : NamedObject + { + // Abstract class used to act as entrypoint for other class, so output + // can change based on given input. At this level, we cannot provide implementation + // Let derived class to determine details. + + public Book(string name) : base(name) + { + + } + public abstract void AddGrade(double grade); + } + public class InMemoryBook : Book { private List grades; @@ -26,13 +39,13 @@ namespace GradeBook private string name; - public Book(string name) : base(name) + public InMemoryBook(string name) : base(name) { Name = name; grades = new List(); } - public void AddGrade(double grade) + public override void AddGrade(double grade) { if (grade <= 100 && grade >= 0) { diff --git a/gradebook/src/GradeBook/Program.cs b/gradebook/src/GradeBook/Program.cs index 2134bdd..96c4ad9 100644 --- a/gradebook/src/GradeBook/Program.cs +++ b/gradebook/src/GradeBook/Program.cs @@ -8,11 +8,23 @@ namespace GradeBook static void Main(string[] args) { - var book = new Book("Jason's Grade Book"); + var book = new InMemoryBook("Jason's Grade Book"); book.GradeAdded += OnGradeAdded; string input; + input = EnterGrades(book); + + var stats = book.GetStatistics(); + + Console.WriteLine($"The lowest grade is {stats.lowGrade}"); + Console.WriteLine($"The highesst grade is {stats.highGrade}"); + Console.WriteLine($"The average grade is {stats.Average:N1}"); + } + + private static string EnterGrades(Book book) + { + string input; while (true) { Console.WriteLine("Give input: "); @@ -21,12 +33,13 @@ namespace GradeBook { Console.WriteLine("Receiving termination signal; Termiate program"); break; - } else + } + else { try { Console.WriteLine($"Received: {input}"); - book.AddGrade(Convert.ToDouble(input)); + book.AddGrade(Convert.ToDouble(input)); // book.AddGrade('A'); } catch (ArgumentException ex) @@ -45,11 +58,7 @@ namespace GradeBook } } - var stats = book.GetStatistics(); - - Console.WriteLine($"The lowest grade is {stats.lowGrade}"); - Console.WriteLine($"The highesst grade is {stats.highGrade}"); - Console.WriteLine($"The average grade is {stats.Average:N1}"); + return input; } static void OnGradeAdded(object sender, EventArgs e) diff --git a/gradebook/test/GradeBook.Tests/BookTests.cs b/gradebook/test/GradeBook.Tests/BookTests.cs index f329f85..5d49b84 100644 --- a/gradebook/test/GradeBook.Tests/BookTests.cs +++ b/gradebook/test/GradeBook.Tests/BookTests.cs @@ -9,7 +9,7 @@ namespace GradeBook.Tests public void BookCalculateAnAverageGrade() { // arrange - var book = new Book(""); + var book = new InMemoryBook(""); book.AddGrade(89.1); book.AddGrade(90.5); book.AddGrade(77.3); diff --git a/gradebook/test/GradeBook.Tests/TypeTests.cs b/gradebook/test/GradeBook.Tests/TypeTests.cs index f98c9a1..de3a849 100644 --- a/gradebook/test/GradeBook.Tests/TypeTests.cs +++ b/gradebook/test/GradeBook.Tests/TypeTests.cs @@ -77,9 +77,9 @@ namespace GradeBook.Tests Assert.Equal(book1.Name, "New Name"); } - private void GetBookSetName(ref Book book, string name) + private void GetBookSetName(ref InMemoryBook book, string name) { - book = new Book(name); + book = new InMemoryBook(name); book.Name = name; } @@ -92,9 +92,9 @@ namespace GradeBook.Tests Assert.Equal(book1.Name, "Book1"); } - private void GetBookSetName(Book book, string name) + private void GetBookSetName(InMemoryBook book, string name) { - book = new Book(name); + book = new InMemoryBook(name); book.Name = name; } @@ -107,7 +107,7 @@ namespace GradeBook.Tests Assert.Equal(book1.Name, "New Name"); } - private void SetName(Book book, string name) + private void SetName(InMemoryBook book, string name) { book.Name = name; } @@ -133,9 +133,9 @@ namespace GradeBook.Tests } - private Book GetBook(string name) + private InMemoryBook GetBook(string name) { - return new Book(name); + return new InMemoryBook(name); } } }