/// <summary> /// Saves scores in JSON format /// /// Don't need a try catch because the async will marshal the exception to the UI thread, which already has a try-catch /// </summary> /// <param name="filePath"></param> /// <param name="studentScores"></param> private static void SaveStudentScores(string filePath, StudentScores studentScores) { var jsonSerializer = new JsonSerializer(); using (var streamWriter = File.CreateText(filePath)) { jsonSerializer.Serialize(streamWriter, studentScores); } }
/// <summary> /// Resets the scores by setting it to null and then updates the UI /// </summary> private void ResetScores() { try { studentScores = null; DataContext = studentScores; UpdateUI(); } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex); } }
/// <summary> /// Creates the Students and Assignments arrays when the submit counts button is clicked /// puts out error labels for invalid values /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSubmitCounts_Click(object sender, RoutedEventArgs e) { try { // Holds the length of the Students and Assignment arrays int numStudents = 0; int numAssignments = 0; // Get the values from the UI and print out error label if user enters unwanted values if ( ToggleErrorLabel(!int.TryParse(boxNumStudents.Text, out numStudents), "Couldn't Parse Number of Students to an integer.") || ToggleErrorLabel(!int.TryParse(boxNumAssignments.Text, out numAssignments), "Couldn't Parse Number of Assignments to an integer.") || ToggleErrorLabel(numStudents > 10 || numStudents < 1, "Number of students must be greater than 0 and less than 10.") || ToggleErrorLabel(numAssignments > 99 || numAssignments < 1, "Number of assignments must be greater than 0 and less than 99.") ) { return; } // Create the new student scores model with the inputed values studentScores = new StudentScores(numStudents, numAssignments); // Default the students and assignments for (int index = 0; index < numStudents; index++) { studentScores.Students[index] = "Student #" + (index + 1); } for (int index = 0; index < studentScores.Assignments.GetLength(0); index++) { for (int index2 = 0; index2 < studentScores.Assignments.GetLength(1); index2++) { studentScores.Assignments[index, index2] = 0; } } UpdateUI(); } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex); } }
/// <summary> /// Modify this program so that a file can be loaded which contains the student and assignment data. /// The data should be loaded from the file back into the arrays used to store the student’s name and grades. /// The format of the file is up to you. A suggestion would be to use an XML file. /// The assignments must be loaded on a separate thread from the UI. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void LoadScoresFromFileButton_Click(object sender, RoutedEventArgs e) { try { studentScores = await InputOutput.LoadStudentScoresAsync(LoadFileNameTextBox.Text); if (studentScores == null) { return; } DataContext = studentScores; UpdateUI(); } catch (Exception ex) { HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex); } }
/// <summary> /// Saves scores Asynchronously in JSON format /// /// Don't need a try catch because the async will marshal the exception to the UI thread, which already has a try-catch /// </summary> /// <param name="filePath"></param> /// <param name="studentScores"></param> /// <returns></returns> internal static Task SaveStudentScoresAsync(string filePath, StudentScores studentScores) { return(Task.Run(() => SaveStudentScores(filePath, studentScores))); }