private void btn_addToHistory_Click(object sender, EventArgs e) { //Validate Data int credits; try { credits = int.Parse(txtBx_credits.Text); } catch { return; } string letterGrade = txtBx_grade.Text; Class newClass = new Class(txtBx_className.Text, credits, letterGrade); List<Class> historicClasses; try { historicClasses = DataIO.LoadHistoricClasses(); } catch { //Could not seralzie. New empty list historicClasses = new List<Class>(); } historicClasses.Add(newClass); DataIO.SaveHistoricClasses(historicClasses); txtBx_credits.Text = "0"; txtBx_grade.Text = ""; txtBx_className.Text = "Class Name"; }
private void calculateGPA() { List<string> creditsList = new List<string> { credits1.Text, credits2.Text, credits3.Text, credits4.Text, credits5.Text, credits6.Text }; List<string> gradeList = new List<string> { grade1.Text, grade2.Text, grade3.Text, grade4.Text, grade5.Text, grade6.Text }; List<string> nameList = new List<string> { name1.Text, name2.Text, name3.Text, name4.Text, name5.Text, name6.Text }; List<Class> currentClasses = new List<Class>(); for (int i = 0; i < 6; i++) { int credits; try { credits = int.Parse(creditsList[i]); } catch { break; } string letterGrade = gradeList[i]; Class newClass = new Class(nameList[i], credits, letterGrade); currentClasses.Add(newClass); } DataIO.SaveCurrentClasses(currentClasses); List<Class> allClasses = DataIO.LoadHistoricClasses(); //Start with historic classes allClasses.AddRange(currentClasses); //Adds current classes float semesterGPA = calculateGPA(currentClasses); float cumGPA = calculateGPA(allClasses); statusLbl_semesterGPA.Text = semesterGPA.ToString("F3"); statusLbl_cumGPA.Text = cumGPA.ToString("F3"); }
private void Calculate_Btn_Click(object sender, RoutedEventArgs e) { #region Declare, retrieve and validate input for credits for classes 1-6 int class1Credits; int class2Credits; int class3Credits; int class4Credits; int class5Credits; int class6Credits; if (validateCreditsInput(class1CreditsTxtBox.Text)) class1Credits = Convert.ToInt32(class1CreditsTxtBox.Text[0]); else { class1CreditsTxtBox.Text = ""; class1Credits = 0; } if (validateCreditsInput(class2CreditsTxtBox.Text)) class2Credits = Convert.ToInt32(class2CreditsTxtBox.Text[0]); else { class2CreditsTxtBox.Text = ""; class2Credits = 0; } if (validateCreditsInput(class3CreditsTxtBox.Text)) class3Credits = Convert.ToInt32(class3CreditsTxtBox.Text[0]); else { class3CreditsTxtBox.Text = ""; class3Credits = 0; } if (validateCreditsInput(class4CreditsTxtBox.Text)) class4Credits = Convert.ToInt32(class4CreditsTxtBox.Text[0]); else { class4CreditsTxtBox.Text = ""; class4Credits = 0; } if (validateCreditsInput(class5CreditsTxtBox.Text)) class5Credits = Convert.ToInt32(class5CreditsTxtBox.Text[0]); else { class5CreditsTxtBox.Text = ""; class5Credits = 0; } if (validateCreditsInput(class6CreditsTxtBox.Text)) class6Credits = Convert.ToInt32(class6CreditsTxtBox.Text[0]); else { class6CreditsTxtBox.Text = ""; class6Credits = 0; } #endregion #region Declare, retrieve and validate input for grades for classes 1-6 string class1Grade; string class2Grade; string class3Grade; string class4Grade; string class5Grade; string class6Grade; if (validateGradesInput(class1GradeTxtBox.Text)) class1Grade = class1GradeTxtBox.Text.ToUpper(); else { class1GradeTxtBox.Text = ""; class1Grade = ""; } if (validateGradesInput(class2GradeTxtBox.Text)) class2Grade = class2GradeTxtBox.Text.ToUpper(); else { class2GradeTxtBox.Text = ""; class2Grade = ""; } if (validateGradesInput(class3GradeTxtBox.Text)) class3Grade = class3GradeTxtBox.Text.ToUpper(); else { class3GradeTxtBox.Text = ""; class3Grade = ""; } if (validateGradesInput(class4GradeTxtBox.Text)) class4Grade = class4GradeTxtBox.Text.ToUpper(); else { class4GradeTxtBox.Text = ""; class4Grade = ""; } if (validateGradesInput(class5GradeTxtBox.Text)) class5Grade = class5GradeTxtBox.Text.ToUpper(); else { class5GradeTxtBox.Text = ""; class5Grade = ""; } if (validateGradesInput(class6GradeTxtBox.Text)) class6Grade = class6GradeTxtBox.Text.ToUpper(); else { class6GradeTxtBox.Text = ""; class6Grade = ""; } #endregion #region Put all of the classes into Class objects & put them in the array Class class1 = new Class() { credits = class1Credits, grade = class1Grade }; Class class2 = new Class() { credits = class2Credits, grade = class2Grade }; Class class3 = new Class() { credits = class3Credits, grade = class3Grade }; Class class4 = new Class() { credits = class4Credits, grade = class4Grade }; Class class5 = new Class() { credits = class5Credits, grade = class5Grade }; Class class6 = new Class() { credits = class6Credits, grade = class6Grade }; #endregion Class[] classes = { class1, class2, class3, class4, class5, class6 }; // an array of Class objects. Effectively, a class schedule. #region Iterate through class schedule and calculate GPA int totalCredits = 0; double totalGPAPoints = 0; double letterGradeAsDouble = 0; foreach (Class cl in classes) { /* If the class has a valid number of credits */ if (cl.credits.ToString() != "") { // Calculate the total GPA credits for the class by credits*grade switch (cl.grade) { case "A": letterGradeAsDouble = 4.0; break; case "A-": letterGradeAsDouble = 3.67; break; case "B+": letterGradeAsDouble = 3.33; break; case "B": letterGradeAsDouble = 3.0; break; case "B-": letterGradeAsDouble = 2.67; break; case "C+": letterGradeAsDouble = 2.33; break; case "C": letterGradeAsDouble = 2.0; break; case "C-": letterGradeAsDouble = 1.67; break; case "D+": letterGradeAsDouble = 1.33; break; case "D": letterGradeAsDouble = 1.0; break; case "D-": letterGradeAsDouble = .67; break; case "F": letterGradeAsDouble = 0.0; break; case "": cl.credits = 0; /* Invalid input case: Just don't count that class if invalid grade was entered */ break; default: letterGradeAsDouble = 0.0; break; } } totalGPAPoints += (letterGradeAsDouble) * (cl.credits); totalCredits += cl.credits; } double GPA = totalGPAPoints / totalCredits; if (GPA < 0 || GPA.ToString() == "NaN") GPA = 0; gpaTxtBlock.Text = "GPA: " + GPA.ToString("N2"); #endregion }