private bool AddRecord() { bool dataIsValid = false; do { Console.Write("Enter the date of the quiz: "); string dateOfQuiz = Console.ReadLine(); Console.Write("The name of the quiz: "); string quizName = Console.ReadLine(); Console.Write("What is the question number: "); string questionNumber = Console.ReadLine(); // Now let's convert our input into the datatypes try { // TODO Should we declare outside the loop so we can show default values on error? QuestionData questionData = new QuestionData(); questionData.QuizDate = DateTime.Parse(dateOfQuiz); questionData.QuestionNumber = int.Parse(questionNumber); questionData.QuizTitle = quizName; Logger.WriteRecord(questionData); dataIsValid = true; } catch (Exception e) { // TODO Let's check if it is just the date, and fix it to save the data Console.WriteLine("Sorry, the data is invalid, please try again."); dataIsValid = false; } } while (!dataIsValid); return(dataIsValid); }
public static bool WriteRecord(QuestionData questionData) { bool success = false; string directory = Environment.CurrentDirectory; string fullPath = Path.Combine(directory, FILENAME); try { using (StreamWriter sw = new StreamWriter(fullPath, true)) { sw.WriteLine(questionData.PrepData()); } success = true; } catch (Exception e) { success = false; } return(success); }