Finished Defining an Abstract Class

oop
Jason Zhu 2021-02-22 12:26:08 +00:00
parent f7ef36beab
commit fe71127f3a
4 changed files with 41 additions and 19 deletions

View File

@ -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<double> 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<double>();
}
public void AddGrade(double grade)
public override void AddGrade(double grade)
{
if (grade <= 100 && grade >= 0)
{

View File

@ -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,7 +33,8 @@ namespace GradeBook
{
Console.WriteLine("Receiving termination signal; Termiate program");
break;
} else
}
else
{
try
{
@ -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)

View File

@ -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);

View File

@ -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);
}
}
}