Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AddModuleCommand and AddUtils #260

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -167,22 +167,26 @@ private boolean validateModuleCode() throws AcademicException {

if (moduleValidator.isRetakeGrade(module.getGrade())) {
isRetake = true;
ArrayList<PartialModule> allOccurrencesOfModule
= getAllOccurrencesOfModule(currentPerson, moduleCode);

System.out.println(RETAKE_MOD);
printAllOccurrencesOfModule(allOccurrencesOfModule);
System.out.println(WARNING);
printRetakeHelp();
} else {
logger.log(Level.WARNING, "Module entered is duplicated.");
fh.close();
throw new AcademicException(ERROR_DUPLICATE_MOD);
}
}

return isRetake;
}

/**
* Prints help information for retaking modules.
*/
private void printRetakeHelp() {
ArrayList<PartialModule> allOccurrencesOfModule = getAllOccurrencesOfModule(currentPerson, moduleCode);
System.out.println(RETAKE_MOD);
printAllOccurrencesOfModule(allOccurrencesOfModule);
System.out.println(WARNING);
}

/**
* Returns partial module object of module code.
*
@@ -191,22 +195,36 @@ private boolean validateModuleCode() throws AcademicException {
private PartialModule getPartialModule() {
HashMap<String, ArrayList<Integer>> modulesAddedMap = currentPerson.getModulesAddedMap();
ArrayList<PartialModule> modulesAddedList = currentPerson.getModulesList();

ArrayList<Integer> moduleIndexList = modulesAddedMap.get(moduleCode);

int highestCapIndex = moduleIndexList.get(0);
double highestCap = modulesAddedList.get(highestCapIndex).getCap();

for (int i = 0; i < moduleIndexList.size(); i++) {
double currentCap = modulesAddedList.get(moduleIndexList.get(i)).getCap();
highestCapIndex = getHighestCapModuleIndex(modulesAddedList, moduleIndexList, highestCapIndex, highestCap);

PartialModule module = modulesAddedList.get(highestCapIndex);
return module;
}

/**
* Returns the index of the highest scoring grade of the module.
*
* @param modulesAddedList List of PartialModule
* @param moduleIndexList List of index of module
* @param highestCapIndex Index of the current highest grade
* @param highestCap highest grade value
* @return index of the highest cap
*/
private int getHighestCapModuleIndex(ArrayList<PartialModule> modulesAddedList, ArrayList<Integer> moduleIndexList,
int highestCapIndex, double highestCap) {
for (int index = 0; index < moduleIndexList.size(); index++) {
double currentCap = modulesAddedList.get(moduleIndexList.get(index)).getCap();
if (currentCap > highestCap) {
highestCapIndex = moduleIndexList.get(i);
highestCapIndex = moduleIndexList.get(index);
highestCap = currentCap;
}
}

PartialModule module = modulesAddedList.get(highestCapIndex);
return module;
return highestCapIndex;
}

/**
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
*/
public class AddUtils {
private static final int FROM_ADD = 1;
private static final int EMPTY = 0;

private final ModuleLoader allModules;
private final ArrayList<PartialModule> modulesList;
@@ -62,7 +63,7 @@ private void populate(String moduleCode, PartialModule newModuleToAdd) {
modulesList.add(newModuleToAdd);
ArrayList<Integer> addedModuleIndex = modulesAddedMap.get(moduleCode);

if (addedModuleIndex == null || addedModuleIndex.size() == 0) {
if (addedModuleIndex == null || addedModuleIndex.size() == EMPTY) {
ArrayList<Integer> integerArrayToAdd = new ArrayList<>();
integerArrayToAdd.add(modulesList.size() - 1);
modulesAddedMap.put(moduleCode, integerArrayToAdd);