diff --git a/gradebook/src/GradeBook/Book.cs b/gradebook/src/GradeBook/Book.cs index 45bca2b..ada3f20 100644 --- a/gradebook/src/GradeBook/Book.cs +++ b/gradebook/src/GradeBook/Book.cs @@ -16,7 +16,13 @@ namespace GradeBook public void AddGrade(double grade) { - this.grades.Add(grade); + if (grade <= 100 && grade >= 0) + { + this.grades.Add(grade); + } else + { + throw new ArgumentException($"Invalid {nameof(grade)}"); + } } public void AddLetterGrade(char letter) diff --git a/gradebook/src/GradeBook/Program.cs b/gradebook/src/GradeBook/Program.cs index 935521e..9d61e82 100644 --- a/gradebook/src/GradeBook/Program.cs +++ b/gradebook/src/GradeBook/Program.cs @@ -16,8 +16,7 @@ namespace GradeBook var book = new Book("Jason's Grade Book"); string input; - bool if_continue = true; - while (if_continue) + while (true) { Console.WriteLine("Give input: "); input = Console.ReadLine(); @@ -27,8 +26,24 @@ namespace GradeBook break; } else { - Console.WriteLine($"Received: {input}"); - book.AddGrade(Convert.ToDouble(input)); + try + { + Console.WriteLine($"Received: {input}"); + book.AddGrade(Convert.ToDouble(input)); + } + 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"); + } } }