104 lines
2.6 KiB
C#
104 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace GradeBook
|
|
{
|
|
public delegate void GradeAddedDelegate(object sender, EventArgs args);
|
|
|
|
public class Book
|
|
{
|
|
private List<double> grades;
|
|
|
|
readonly string category;
|
|
|
|
private string name;
|
|
public string Name
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public Book(string name)
|
|
{
|
|
Name = name;
|
|
grades = new List<double>();
|
|
}
|
|
|
|
public void AddGrade(double grade)
|
|
{
|
|
if (grade <= 100 && grade >= 0)
|
|
{
|
|
this.grades.Add(grade);
|
|
|
|
if (GradeAdded != null)
|
|
{
|
|
// Somebody is listening/subscribing
|
|
GradeAdded(this, new EventArgs()); // sender = this, pass; Raising event
|
|
}
|
|
} else
|
|
{
|
|
throw new ArgumentException($"Invalid {nameof(grade)}");
|
|
}
|
|
}
|
|
|
|
public void AddGrade(char letter)
|
|
{
|
|
switch (letter)
|
|
{
|
|
case 'A':
|
|
AddGrade(90);
|
|
break;
|
|
case 'B':
|
|
AddGrade(80);
|
|
break;
|
|
case 'C':
|
|
AddGrade(70);
|
|
break;
|
|
default:
|
|
AddGrade(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public event GradeAddedDelegate GradeAdded;
|
|
|
|
public 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;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
} |