70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace GradeBook
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
|
|
// var book = new Book("Scott's Grade Book");
|
|
// book.AddGrade(89.1);
|
|
// book.AddGrade(90.5);
|
|
// book.AddGrade(87.2);
|
|
|
|
var book = new Book("Jason's Grade Book");
|
|
book.GradeAdded += OnGradeAdded;
|
|
book.GradeAdded += OnGradeAdded;
|
|
book.GradeAdded -= OnGradeAdded;
|
|
book.GradeAdded -= OnGradeAdded;
|
|
|
|
string input;
|
|
|
|
while (true)
|
|
{
|
|
Console.WriteLine("Give input: ");
|
|
input = Console.ReadLine();
|
|
if (input == "Q" || input == "q")
|
|
{
|
|
Console.WriteLine("Receiving termination signal; Termiate program");
|
|
break;
|
|
} else
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"Received: {input}");
|
|
book.AddGrade(Convert.ToDouble(input));
|
|
// book.AddGrade('A');
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
catch (FormatException ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
// Will always run for all catch scenario
|
|
Console.WriteLine("Always Done");
|
|
}
|
|
}
|
|
}
|
|
|
|
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}");
|
|
}
|
|
|
|
static void OnGradeAdded(object sender, EventArgs e)
|
|
{
|
|
// Main is static, so it can only call static methods, hence return type is static
|
|
Console.WriteLine("A line has added");
|
|
}
|
|
}
|
|
}
|