Skip to content

Commit

Permalink
Merge pull request #181 from chiayuxuan/code-review
Browse files Browse the repository at this point in the history
Fix bugs for incorrect CAP displayed
  • Loading branch information
chiayuxuan committed Apr 4, 2023
2 parents bb543c3 + 8c887b4 commit 06c1127
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 48 deletions.
68 changes: 47 additions & 21 deletions src/main/java/seedu/penus/logic/utils/Grade.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.util.List;
import java.util.Arrays;

import seedu.penus.common.exceptions.InvalidGradeException;
import seedu.penus.model.Module;

public class Grade {
/**
* Converts a {@code String} grade into its associated gradepoint
*
*
* @param grade string
* @return {@code double} corresponding value of the grade
* @throws InvalidGradeException invalid grade
Expand Down Expand Up @@ -66,7 +67,7 @@ public static double getGradePoint(String grade) throws InvalidGradeException {

/**
* Checks if a grade {@code String} is valid and within expected inputs
*
*
* @param grade string
* @return Boolean true if grade is valid
*/
Expand All @@ -76,37 +77,53 @@ public static Boolean isValid(String grade) {
"B+", "B", "B-",
"C+", "C",
"D+", "D",
"F", "S", "U");

"F", "S", "U",
"CS", "CU"
);
return validGrades.contains(grade.toUpperCase());
}

/**
* For every module taken, calculate weighted score = number of MC * grade
* Sum up weighted score for all mods and divide by total MCs taken thus far
* S/U grades are not calculated for in Overall CAP
*
*
* @param moduleList the list containing all modules taken
* @return total CAP for all mods taken thus far
* @throws InvalidGradeException if there exists an unidentified Grade type
*/
public static double calculateOverallCAP(List<Module> moduleList) throws InvalidGradeException {
double cap;
int numOfSUMods = 0;
int numOfModsTaken = 0;
int numOfModsPlanned = 0;
double totalScore = 0.0;
double totalMC = 0.0;
String numberOfMCs;
for (Module module : moduleList) {
if (!module.getGrade().matches("[SU]") && module.getStatus().equals("Taken")) {
numberOfMCs = ModuleRetriever.getModuleCredit2122(module.getCode());
double weightedScore = Double.parseDouble(numberOfMCs) * module.getGradePoint();
totalScore += weightedScore;
totalMC += Double.parseDouble(numberOfMCs);
if (module.getStatus().equals("Taken")) {
numOfModsTaken++;
if (module.getGrade().matches("S|U|CS|CU")) {
numOfSUMods++;
} else {
numberOfMCs = ModuleRetriever.getModuleCredit2122(module.getCode());
double weightedScore = Double.parseDouble(numberOfMCs) *
module.getGradePoint();
totalScore += weightedScore;
totalMC += Double.parseDouble(numberOfMCs);
}
} else {
numOfModsPlanned++;
}
}
if ((totalScore == 0.0) || (totalMC == 0.0)) {
return 0.0;
if (numOfModsPlanned == moduleList.size()) {//all mods are planned
cap = 0.0;
} else if (numOfSUMods == numOfModsTaken) { //all mods are SU
cap = 5.0;
} else {
return (totalScore / totalMC);
cap = totalScore / totalMC;
}
return cap;
}

/**
Expand All @@ -115,35 +132,44 @@ public static double calculateOverallCAP(List<Module> moduleList) throws Invalid
* Sum up weighted score for all mods and divide by total MCs taken in the
* semester
* S/U grades are not calculated for in Semester CAP
*
*
* @param semArray list of String array containing moduleCode and moduleGrade
* @return total CAP for a particular semester
* @throws InvalidGradeException if there exists an unidentified Grade type
*/
public static double calculateSemCAP(List<String[]> semArray) throws InvalidGradeException {
double cap;
int numOfSUMods = 0;
double totalScore = 0.0;
double totalMC = 0.0;
String numberOfMCs;
for (String[] module : semArray) {
String moduleCode = module[0];
String moduleGrade = module[1];
if (!moduleGrade.matches("[SU]") && !moduleGrade.equals("")) {
if (moduleGrade.matches("S|U|CS|CU")) {
numOfSUMods++;
}
if (!moduleGrade.matches("S|U|CS|CU") && !moduleGrade.equals("")) {
numberOfMCs = ModuleRetriever.getModuleCredit2223(moduleCode);
double weightedScore = Double.parseDouble(numberOfMCs) * Grade.getGradePoint(moduleGrade);
double weightedScore = Double.parseDouble(numberOfMCs) *
Grade.getGradePoint(moduleGrade);
totalScore += weightedScore;
totalMC += Double.parseDouble(numberOfMCs);
}
}
if ((totalScore == 0.0) || (totalMC == 0.0)) {
return 0.0;
if (numOfSUMods == semArray.size()) {//all mods in sem are SU-ed
cap = 5.0;
} else if ((totalScore == 0.0) || (totalMC == 0.0)) {//all mods are planned
cap = 0.0;
} else {
return (totalScore / totalMC);
cap = totalScore / totalMC;
}
return cap;
}

/**
* Calls calculateOverallCAP and prints the overall CAP to 2 decimal places
*
*
* @param moduleList the list containing all modules taken
* @return capMessage String
* @throws InvalidGradeException if there exists an unidentified Grade
Expand All @@ -161,7 +187,7 @@ public static String getOverallCAP(List<Module> moduleList) throws InvalidGradeE

/**
* Calls calculateSemCAP and prints the semester CAP to 2 decimal places
*
*
* @param semArray list of String array containing moduleCode and moduleGrade
* @return semCapMessage String
* @throws InvalidGradeException if there exists an unidentified Grade
Expand Down
Loading

0 comments on commit 06c1127

Please sign in to comment.