/** Adding grades to the grade book*/ private static void EnterGrades(IBook book) { // Add grade to the grades book from user Console.WriteLine($"Enter the grades for {book.Name} grade book. Letter Grades allowed are A, B, C, D and F. Press Q to discontinue:"); do { try { var input = Console.ReadLine(); double grade; char letter; //Parses grade to double or character if (double.TryParse(input, out grade)) { book.AddGrade(grade); } else if (char.TryParse(input, out letter) && !char.Equals(letter, 'Q')) { book.AddGrade(letter); } // break the loop if user enters Q else if (char.TryParse(input, out letter) && char.Equals(letter, 'Q')) { break; } // requests user to enter a valid grade else { throw new FormatException($"Please enter a {nameof(grade)} of type double or char:"); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("_______________________________________________"); } } while (true); }
private static void AddGrade(IBook book, string input) { if (double.TryParse(input, out double grade)) { book.AddGrade(grade); } else if (input.Length == 1) { book.AddGrade(input[0]); } else { throw new ArgumentException($"ERROR: Invalid grade '{input}'. Enter a value between 0 and 100, or a letter from A to D, or F."); } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Enter a grade or 'q' to quit"); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("**"); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Please enter grade: (press 'q' when done)"); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } } }
private static void EnterGrades(IBook book) { while (true) { var input = Console.ReadLine(); if (input == "q") { break; } try { book.AddGrade(double.Parse(input)); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { // code runs everytime } } }
private static void EnterGrade(IBook book) { while (true) { Console.WriteLine("Podaj ocenę lub podaj 'q' aby wyjść: "); var userInput = Console.ReadLine(); if (userInput == "q") { break; } try //próbujemy wykonać kod zawarty w tym bloku w przypadku błędu wykorzystywana jest klauzula catch { var grade = double.Parse(userInput); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally //zawsze wykonywane niezależnie od wyjątku np gdy chcemy zamknąć plik { Console.WriteLine("**"); } } }
private static void EnterGrades(IBook book) { while (true) { System.Console.Write("Enter the grades (Press Q to quit): "); var input = Console.ReadLine(); if (input.ToUpper().Equals("Q")) { System.Console.WriteLine("=======================\n"); break; } try { var grade = double.Parse(input); book.AddGrade(grade); // System.Console.WriteLine($"Grade ({grade}) added to the book!\n"); } catch (ArgumentException ex) { System.Console.WriteLine(ex.Message); } catch (FormatException ex) { System.Console.WriteLine(ex.Message); } finally { System.Console.WriteLine("Enter another grade"); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Please enter a grade or q to quit and compute."); var userInput = Console.ReadLine(); if (userInput == "q") { break; } try { var grade = double.Parse(userInput); book.AddGrade(grade); } catch (ArgumentException invalidGradeException) { Console.WriteLine(invalidGradeException.Message); } catch (FormatException invalidFormatException) { Console.WriteLine(invalidFormatException.Message); } /*finally{ * Console.WriteLine("*****");//This will run no matter what * }*/ } ; }
private static string EnterGrades(IBook book) { string input; do { Console.WriteLine("Enter a new grade. To finish enter 'q'"); input = Console.ReadLine().ToLower(); try { var grade = double.Parse(input); book.AddGrade(grade); } catch (FormatException e) { Console.WriteLine(e.Message); if (input != "q") { Console.WriteLine($"You did not provide a proper grade: '{input}"); } } catch (ArgumentException) { Console.WriteLine($"The provided grade is not within an acceptable range(0-100) {input}"); } } while (input != "q"); return(input); }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("enter a grade or enter character q to quit"); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { // .. properly clean things up even if there was an exception. } } ; }
private static void EnterGrades(IBook book) { string inputGrade; do { Console.WriteLine("Type a grade or press \"Q\" to quit and show statistics"); inputGrade = Console.ReadLine().ToLower(); if (inputGrade == "q") { continue; } try { var grade = double.Parse(inputGrade); book.AddGrade(grade); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } } while (inputGrade != "q"); }
private static void EnterGrade(IBook book) { // grade input loop var done = false; while (!done) { Console.WriteLine("Enter a grade or 'q' to quit"); var input = Console.ReadLine(); if (input == "q") { done = true; continue; } try { double grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { // always runs after try block even with errors Console.WriteLine("**"); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Please Enter a grade or 'q' to quit:"); String grade = Console.ReadLine(); if (grade.Length == 0 || grade.Equals("q", StringComparison.InvariantCultureIgnoreCase)) { break; } try { double gd = double.Parse(grade); book.AddGrade(gd); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { // the block of code that needs to be executed regardless what happens in the try-catch blocks // usually things like closing the file or the DB connection are placed here Console.WriteLine("**"); } } }
private static void EnterGrade(IBook book) { while (true) { Console.WriteLine("Please enter the grade or press q to see the statistics"); var value = Console.ReadLine(); if (value == "q") { break; } try { book.AddGrade(double.Parse(value)) } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } } }
private static bool EnterGrade(IBook book, bool done) { while (!done) { Console.WriteLine("Enter a grade or enter char q to quit: "); var input = Console.ReadLine(); if (input == "q") { done = true; continue; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException err) { Console.WriteLine(err.Message); } catch (FormatException err) { Console.WriteLine(err.Message); } finally { Console.WriteLine("**"); } } return(done); }
private static void EnterGrades(IBook book) { while (true) { Console.Write("Please a Grade between 0 and 100 (or \"q\" to end entry) : "); string input = Console.ReadLine(); if (input.ToLower().Equals("q")) { break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } } }
// This is polymorphic and will change depending on the type of book we recieve private static bool EnterGrades(IBook book, bool done) { while (!done) { Console.Write("Enter grade or 'q' to quit-->"); var input = Console.ReadLine(); if (input == "q") { done = true; continue; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine($"Error was recieved {ex.Message}"); } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Runs after exceptions"); } } ; return(done); }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Please enter a grade or 'q' to quit"); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); // Exception can be logged and rethrown // throw; } catch (FormatException ex) { Console.WriteLine(ex.Message); } finally { // Any code here will be alwasy executed even if an exception is thrown // Close file or close network socket etc, even if an exception happened. Console.WriteLine("**"); } } }
private static void EnterGrades(IBook book) { var input = ""; while (input != "q") { Console.Write("Please enter a grade or q to quit: "); input = Console.ReadLine(); if (double.TryParse(input, out var result)) { try { book.AddGrade(result); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("***"); } } else { Console.WriteLine("Invalid input"); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Enter a grade or 'q' to quit"); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input); if (grade <= 100 && grade >= 0) { book.AddGrade(grade); } else { throw new ArgumentException($"Invalid {nameof(grade)}"); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Please enter a grade or hit 'q' to exit..."); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { System.Console.WriteLine(ex.Message); //throw; } catch (FormatException ex) { System.Console.WriteLine(ex.Message); } //this is if you want a block of code to ALWAYS execute finally { System.Console.WriteLine("I can do this! (determined!)"); } } // book.AddGrade(96.7); // book.AddGrade(74.7); // book.AddGrade(89.2); }
private static string EnterGrades(IBook book) { string input; while (true) { Console.WriteLine("Enter a grade or 'q' to quit..."); input = Console.ReadLine(); if (input == "q") { break; } try { book.AddGrade(double.Parse(input)); Console.WriteLine(book); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } finally { Console.WriteLine("**"); } } return(input); }
private static void EnterGrade(IBook book) { var done = false; while (!done) { Console.WriteLine("Enter a grade or 'q' to quit"); var input = Console.ReadLine(); if (input == "q") { done = true; continue; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } } }
private static void EnterGrades(IBook book) { Console.WriteLine("Please enter the grades. Press 'Q' to quit and display results."); while (true) { var input = Console.ReadLine(); if (input.Equals("Q")) { break; } try { var inputGrade = double.Parse(input); book.AddGrade(inputGrade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } //to rethrow the exception we can write throw; below Console.WriteLine statement, this could be done if we didn't know the exception type catch (FormatException ex) { Console.WriteLine(ex.Message); } finally //a piece of block which always executes irrespective of any exceptions { Console.WriteLine("*"); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Please put a grade, or \"q\" to exit."); var input = Console.ReadLine(); if (input == "q") { break; } try { // Will throw an exception when given different format // that cannot be converted to double type var grade = double.Parse(input); // WIll throw an exception when grade is invalid - see method for more info book.AddGrade(grade); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } } }
private static void EnterGrades(IBook book) { while (true) { System.Console.WriteLine("Enter a grade or q to quit."); var input = Console.ReadLine(); if (input == "q") { break; } try { var grade = double.Parse(input);; book.AddGrade(grade); } catch (ArgumentException ex) { System.Console.WriteLine(ex.Message); } catch (FormatException ex) { System.Console.WriteLine(ex.Message); } finally { //..could handle clean up etc } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Enter a grade or 'q' to quit:"); var response = Console.ReadLine(); if (response != null && !string.Equals(response.ToLower(), "q")) { try { var parsedResponse = double.Parse(response); book.AddGrade(parsedResponse); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("**"); } } else { break; } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Enter Grade or Enter q to quit:"); var input = Console.ReadLine(); if (input == "q") { break; } //Error Handling try { var grade = double.Parse(input); book.AddGrade(grade); } //Write this separate catch statements to be able to catch only specifi errors catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Enter a Grade or Enter a 'Q' to quit"); var input = Console.ReadLine(); if (input == "Q") { Console.WriteLine("You just Quit, Thank you"); break; } try { var grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (FormatException ex) { Console.WriteLine(ex.Message); } // finally // { // Console.WriteLine("**"); // } } }
private static void EnterGrades(IBook book) { while (true) { Console.WriteLine("Enter grade or enter \"q\" to finish"); var input = Console.ReadLine(); if (input != "q") { try { double grade = double.Parse(input); book.AddGrade(grade); } catch (ArgumentException e) { Console.WriteLine(e.Message); } catch (FormatException e) { Console.WriteLine(e.Message); } finally { // close a network socket or something if needed at this point } } else { break; } } }