From 4d18b80bf62fe23c93abca49eafda08658c772d6 Mon Sep 17 00:00:00 2001 From: ELI JOSEPH BRADLEY Date: Sun, 1 Oct 2017 08:20:45 -0500 Subject: [PATCH 1/4] Refactor GPAViewController.swift Use [Int].reduce where we were using a for loop, and make a dedicated method to change label color. --- GPACalculator/GPAViewController.swift | 53 ++++++++++----------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/GPACalculator/GPAViewController.swift b/GPACalculator/GPAViewController.swift index 0b71800..845558c 100644 --- a/GPACalculator/GPAViewController.swift +++ b/GPACalculator/GPAViewController.swift @@ -32,15 +32,9 @@ class GPAViewController: UIViewController { super.viewWillAppear(animated) Information.initializeArray() - var totalWestlakeGrades = 0.0; - var totalStandardCredit = 0.0; - var totalStandardGrades = 0.0; - - for classAndGrade in Information.classesAndGrades { - totalWestlakeGrades += Double(classAndGrade.grade) * classAndGrade.multiplier - totalStandardGrades += Double(classAndGrade.grade) * classAndGrade.credits - totalStandardCredit += classAndGrade.credits - } + let totalWestlakeGrades = Information.classesAndGrades.map({ Double(classAndGrade.grade) * classAndGrade.multiplier }).reduce(0, { (sum, new) in sum + new }) + let totalStandardCredit = Information.classesAndGrades.map({ Double(classAndGrade.grade) * classAndGrade.credits }).reduce(0, { (sum, new) in sum + new }) + let totalStandardGrades = Information.classesAndGrades.map({ classAndGrade.credits }).reduce(0, { (sum, new) in sum + new }) guard totalStandardCredit != 0, Information.classesAndGrades.count != 0 else { print("Division by zero in GPA calculation.") @@ -60,63 +54,51 @@ class GPAViewController: UIViewController { var letter: String = "" switch (standardGPA) { case 0..<65: - standardGPALabel.textColor = .black - letterGradeLabel.textColor = .black + setColor(.black) fourPointScale = 0 letter = "E/F" case 65...66: - standardGPALabel.textColor = .red - letterGradeLabel.textColor = .red + setColor(.red) fourPointScale = 1 letter = "D" case 67...69: - standardGPALabel.textColor = .red - letterGradeLabel.textColor = .red + setColor(.red) fourPointScale = 1.3 letter = "D+" case 70...72: - standardGPALabel.textColor = .orange - letterGradeLabel.textColor = .orange + setColor(.orange) fourPointScale = 1.7 letter = "C-" case 73...76: - standardGPALabel.textColor = .orange - letterGradeLabel.textColor = .orange + setColor(.orange) fourPointScale = 2.0 letter = "C" case 77...79: - standardGPALabel.textColor = .orange - letterGradeLabel.textColor = .orange + setColor(.orange) fourPointScale = 2.3 letter = "C+" case 80...82: - standardGPALabel.textColor = .yellow - letterGradeLabel.textColor = .yellow + setColor(.yellow) fourPointScale = 2.7 letter = "B-" case 83...86: - standardGPALabel.textColor = .yellow - letterGradeLabel.textColor = .yellow + setColor(.yellow) fourPointScale = 3.0 letter = "B" case 87...89: - standardGPALabel.textColor = .yellow - letterGradeLabel.textColor = .yellow + setColor(.yellow) fourPointScale = 3.3 letter = "B+" case 90...92: - standardGPALabel.textColor = .green - letterGradeLabel.textColor = .green + setColor(.green) fourPointScale = 3.7 letter = "A-" case 93...96: - standardGPALabel.textColor = .green - letterGradeLabel.textColor = .green + setColor(.green) fourPointScale = 4.0 letter = "A" case 97...100: - standardGPALabel.textColor = .green - letterGradeLabel.textColor = .green + setColor(.green) fourPointScale = 4.0 letter = "A+" default: @@ -136,6 +118,11 @@ class GPAViewController: UIViewController { letterGradeLabel.text = "\(letter)" } + func setColor(_ color: UIColor) { + standardGPALabel.textColor = color + letterGradeLabel.textColor = color + } + override func viewDidLoad() { super.viewDidLoad() } From cff8263be012f1b04f34c7265ffd588990c290b5 Mon Sep 17 00:00:00 2001 From: ELI JOSEPH BRADLEY Date: Sun, 1 Oct 2017 14:18:08 -0500 Subject: [PATCH 2/4] Update GPAViewController.swift --- GPACalculator/GPAViewController.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/GPACalculator/GPAViewController.swift b/GPACalculator/GPAViewController.swift index 845558c..faa5444 100644 --- a/GPACalculator/GPAViewController.swift +++ b/GPACalculator/GPAViewController.swift @@ -32,9 +32,9 @@ class GPAViewController: UIViewController { super.viewWillAppear(animated) Information.initializeArray() - let totalWestlakeGrades = Information.classesAndGrades.map({ Double(classAndGrade.grade) * classAndGrade.multiplier }).reduce(0, { (sum, new) in sum + new }) - let totalStandardCredit = Information.classesAndGrades.map({ Double(classAndGrade.grade) * classAndGrade.credits }).reduce(0, { (sum, new) in sum + new }) - let totalStandardGrades = Information.classesAndGrades.map({ classAndGrade.credits }).reduce(0, { (sum, new) in sum + new }) + let totalWestlakeGrades = Information.classesAndGrades.map({ Double($0.grade) * $0.multiplier }).reduce(0, { (sum, grade) in sum + credits }) + let totalStandardCredit = Information.classesAndGrades.map({ Double($0.grade) * $0.credits }).reduce(0, { (sum, grade) in sum + credits }) + let totalStandardGrades = Information.classesAndGrades.map({ $0.credits }).reduce(0, { (sum, credits) in sum + credits }) guard totalStandardCredit != 0, Information.classesAndGrades.count != 0 else { print("Division by zero in GPA calculation.") From d5e1965ea8c54a905cbbfcbf43e0e6f1fb4e8b9b Mon Sep 17 00:00:00 2001 From: ELI JOSEPH BRADLEY Date: Sun, 1 Oct 2017 14:18:33 -0500 Subject: [PATCH 3/4] Update GPAViewController.swift --- GPACalculator/GPAViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GPACalculator/GPAViewController.swift b/GPACalculator/GPAViewController.swift index faa5444..93e26c5 100644 --- a/GPACalculator/GPAViewController.swift +++ b/GPACalculator/GPAViewController.swift @@ -32,8 +32,8 @@ class GPAViewController: UIViewController { super.viewWillAppear(animated) Information.initializeArray() - let totalWestlakeGrades = Information.classesAndGrades.map({ Double($0.grade) * $0.multiplier }).reduce(0, { (sum, grade) in sum + credits }) - let totalStandardCredit = Information.classesAndGrades.map({ Double($0.grade) * $0.credits }).reduce(0, { (sum, grade) in sum + credits }) + let totalWestlakeGrades = Information.classesAndGrades.map({ Double($0.grade) * $0.multiplier }).reduce(0, { (sum, grade) in sum + grade }) + let totalStandardCredit = Information.classesAndGrades.map({ Double($0.grade) * $0.credits }).reduce(0, { (sum, grade) in sum + grade }) let totalStandardGrades = Information.classesAndGrades.map({ $0.credits }).reduce(0, { (sum, credits) in sum + credits }) guard totalStandardCredit != 0, Information.classesAndGrades.count != 0 else { From 55c0e563524893da40bc9f0b79be72121f711149 Mon Sep 17 00:00:00 2001 From: ELI JOSEPH BRADLEY Date: Sun, 1 Oct 2017 14:19:24 -0500 Subject: [PATCH 4/4] Update GPAViewController.swift --- GPACalculator/GPAViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GPACalculator/GPAViewController.swift b/GPACalculator/GPAViewController.swift index 93e26c5..e83579d 100644 --- a/GPACalculator/GPAViewController.swift +++ b/GPACalculator/GPAViewController.swift @@ -33,8 +33,8 @@ class GPAViewController: UIViewController { Information.initializeArray() let totalWestlakeGrades = Information.classesAndGrades.map({ Double($0.grade) * $0.multiplier }).reduce(0, { (sum, grade) in sum + grade }) - let totalStandardCredit = Information.classesAndGrades.map({ Double($0.grade) * $0.credits }).reduce(0, { (sum, grade) in sum + grade }) - let totalStandardGrades = Information.classesAndGrades.map({ $0.credits }).reduce(0, { (sum, credits) in sum + credits }) + let totalStandardGrades = Information.classesAndGrades.map({ Double($0.grade) * $0.credits }).reduce(0, { (sum, grade) in sum + grade }) + let totalStandardCredit = Information.classesAndGrades.map({ $0.credits }).reduce(0, { (sum, credits) in sum + credits }) guard totalStandardCredit != 0, Information.classesAndGrades.count != 0 else { print("Division by zero in GPA calculation.")