Skip to content

Commit

Permalink
Merge pull request #314 from jerroldlam/WIP-Jerrold
Browse files Browse the repository at this point in the history
Fix bugs when shifting modules
  • Loading branch information
Khenus authored Nov 9, 2020
2 parents d21c7fd + 3520878 commit 2776ce8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import static seedu.duke.apps.academicplanner.commons.SharedUtils.fromFailingToPass;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.getEntryToBeEdited;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.getLatestSemester;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.notAllowedSemesterUpdateBackward;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.notAllowedSemesterUpdateForward;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.verifyRepeatedSemester;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import seedu.duke.apps.academicplanner.exceptions.AcademicException;
import seedu.duke.apps.capcalculator.commons.CalculatorUtils;
import seedu.duke.apps.moduleloader.ModuleLoader;
Expand Down Expand Up @@ -146,11 +152,11 @@ public void editModuleSemester(Scanner in, String moduleCode) throws AcademicExc
PartialModule currentSemesterModule = modulesList.get(moduleIndexList.get(indexToUpdate));

if (parseInt(newValue) > currentSemesterModule.getSemesterIndex() && moduleIndexList.size() > 1) {
if (notAllowedSemesterUpdateForward(parseInt(newValue), moduleIndexList, modulesList)) {
if (notAllowedSemesterUpdateForward(parseInt(newValue), modulesList, moduleCode)) {
throw new AcademicException(ERROR_ILLEGAL_FORWARD);
}
} else if (moduleIndexList.size() > 1) {
if (!modChecker.isRetakeGrade(currentSemesterModule.getGrade())) {
if (notAllowedSemesterUpdateBackward(parseInt(newValue), modulesList, moduleCode)) {
throw new AcademicException(ERROR_ILLEGAL_BACKWARD);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,40 @@ public static void verifyRepeatedSemester(int semesterValue, Person currentPerso
}

/**
* Returns true if semester is not allowed to be shifted forward,
* else returns flase.
* Returns true if module is not allowed to be shifted forward,
* else returns false.
*
* @param newSemester semester index to be shifted into
* @param moduleIndexList list of user's modules indexes
* @param userModuleList list of user's modules
* @param newSemester semester to shift to
* @param userModuleList user's module list
* @param moduleCode module code
* @return boolean
*/
public static boolean notAllowedSemesterUpdateForward(int newSemester,
ArrayList<Integer> moduleIndexList, ArrayList<PartialModule> userModuleList) {
public static boolean notAllowedSemesterUpdateForward(int newSemester, ArrayList<PartialModule> userModuleList,
String moduleCode) {
int latestPassSemester = getLatestPassSemester(userModuleList, moduleCode);

ModuleValidator validator = new ModuleValidator();
int latestSemester = getLatestSemester(userModuleList, moduleIndexList);
PartialModule latestSemesterModule = userModuleList.get(moduleIndexList.get(latestSemester - 1));

if (newSemester > latestSemester && !validator.isRetakeGrade(latestSemesterModule.getGrade())) {
if (newSemester > latestPassSemester) {
return true;
}
return false;
}

/**
* Returns true if module is not allowed to be shifted backward,
* else returns false.
*
* @param newSemester semester to shift to
* @param userModuleList user's module list
* @param moduleCode module code
* @return boolean
*/
public static boolean notAllowedSemesterUpdateBackward(int newSemester, ArrayList<PartialModule> userModuleList,
String moduleCode) {
int latestFailSemester = getLatestFailSemester(userModuleList, moduleCode);

if (newSemester < latestFailSemester) {
return true;
}
return false;
}

Expand Down Expand Up @@ -209,10 +224,47 @@ public static int getLatestSemester(ArrayList<PartialModule> modulesAddedList, A
latestSemester = currentSemester;
}
}

return latestSemester;
}

/**
* Returns semester index of latest failed module.
*
* @param modulesAddedList user list of module
* @param moduleCode module code
* @return latest fail semester
*/
public static int getLatestFailSemester(ArrayList<PartialModule> modulesAddedList, String moduleCode) {
ModuleValidator validator = new ModuleValidator();
int latestFailSemester = 1;
for (PartialModule m : modulesAddedList) {
if (m.getModuleCode().equalsIgnoreCase(moduleCode) && validator.isRetakeGrade(m.getGrade())
&& m.getSemesterIndex() > latestFailSemester) {
latestFailSemester = m.getSemesterIndex();
}
}
return latestFailSemester;
}

/**
* Returns semester index of latest pass module.
*
* @param modulesAddedList user list of module
* @param moduleCode module code
* @return latest pass semester
*/
public static int getLatestPassSemester(ArrayList<PartialModule> modulesAddedList, String moduleCode) {
ModuleValidator validator = new ModuleValidator();
int latestPassSemester = 1;
for (PartialModule m : modulesAddedList) {
if (m.getModuleCode().equalsIgnoreCase(moduleCode) && !validator.isRetakeGrade(m.getGrade())
&& m.getSemesterIndex() > latestPassSemester) {
latestPassSemester = m.getSemesterIndex();
}
}
return latestPassSemester;
}

/**
* Gets the module with the largest cap for the current person given a certain module code.
*
Expand Down

0 comments on commit 2776ce8

Please sign in to comment.