Skip to content

Commit

Permalink
Merge pull request #318 from Khenus/Khenus
Browse files Browse the repository at this point in the history
Add further checks for adding modules
  • Loading branch information
jerroldlam authored Nov 9, 2020
2 parents 20e6027 + 4ca36cf commit 095319a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import seedu.duke.apps.academicplanner.commons.AddUtils;
import seedu.duke.apps.academicplanner.commons.ModuleValidator;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.getAllOccurrencesOfModule;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.getLatestSemester;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.printAllOccurrencesOfModule;
import seedu.duke.apps.academicplanner.exceptions.AcademicException;
import seedu.duke.apps.moduleloader.ModuleLoader;
import seedu.duke.global.Command;
Expand All @@ -23,6 +20,10 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static seedu.duke.apps.academicplanner.commons.SharedUtils.getAllOccurrencesOfModule;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.getLatestSemesterModule;
import static seedu.duke.apps.academicplanner.commons.SharedUtils.printAllOccurrencesOfModule;

//@@author jerroldlam
/**
* Class representing an add module command from the academic planner.
Expand All @@ -32,6 +33,8 @@ public class AddModuleCommand extends Command {
private static final String ERROR_INVALID_SEMESTER_INDEX = "INVALID SEMESTER INDEX";
private static final String ERROR_INVALID_GRADE = "INVALID GRADE VALUE";
private static final String ERROR_NOT_OFFERED = " IS NOT OFFERED BY NUS";
private static final String INVALID_RETAKE_GRADE
= "Entered grade must be a fail grade as selected semester is not the latest semester!";
private static final String ERROR_DUPLICATE_MOD
= "You already have this mod on your calendar and you cannot retake!";
private static final String VALID_GRADES = "Valid grades are:\n"
Expand Down Expand Up @@ -89,7 +92,7 @@ public void execute() throws AcademicException, IOException {
boolean isRetake = validateModuleCode();

int semesterValue = getSemesterValue(isRetake);
String gradeValue = getGradeValue();
String gradeValue = getGradeValue(isRetake);
int moduleCredit = addUtils.getModuleCreditForModule(moduleCode);

assertInputs(semesterValue, moduleCredit);
Expand All @@ -108,10 +111,15 @@ public void execute() throws AcademicException, IOException {
* @return Valid Grade value
* @throws AcademicException when invalid grade value is given
*/
private String getGradeValue() throws AcademicException {
private String getGradeValue(boolean isRetake) throws AcademicException {
promptUserToEnterGrade();
String gradeValue = in.nextLine().trim().toUpperCase();
validateInputGrade(gradeValue);

if (isRetake && !moduleValidator.isRetakeGrade(gradeValue)) {
throw new AcademicException(INVALID_RETAKE_GRADE);
}

return gradeValue;
}

Expand Down Expand Up @@ -273,11 +281,13 @@ private void validateRetakeParameters(int semesterValue) throws AcademicExceptio
ArrayList<PartialModule> modulesAddedList = currentPerson.getModulesList();
ArrayList<Integer> indexArrayList = currentPerson.getModulesAddedMap().get(moduleCode);

int latestSemester = getLatestSemester(modulesAddedList, indexArrayList);
PartialModule latestSemesterModule = getLatestSemesterModule(modulesAddedList, indexArrayList);

boolean isLatestRetake = moduleValidator.isRetakeGrade(latestSemesterModule.getGrade());

if (semesterValue <= latestSemester) {
if (!isLatestRetake && semesterValue <= latestSemesterModule.getSemesterIndex()) {
fh.close();
throw new AcademicException(INVALID_RETAKE_SEMESTER_LESS + latestSemester + "!");
throw new AcademicException(INVALID_RETAKE_SEMESTER_LESS + latestSemesterModule.getSemesterIndex() + "!");
}
checkValidityRetakeSemester(semesterValue, modulesAddedList);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package seedu.duke.apps.academicplanner.commons;

import static java.lang.Integer.parseInt;
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 All @@ -22,6 +10,15 @@
import java.util.HashMap;
import java.util.Scanner;

import static java.lang.Integer.parseInt;
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;


//@@author harryleecp
/**
* Class representing edit module utilities from the edit module command.
Expand Down Expand Up @@ -53,6 +50,7 @@ public EditUtils(ModuleLoader allModules, Person currentPerson) {
private static final String PROMPT_NEW_GRADE = "Enter the new grade: ";
private static final String ERROR_INVALID_SEMESTER_INDEX = "INVALID SEMESTER INDEX";
private static final String ERROR_INVALID_GRADE = "INVALID GRADE VALUE";
private static final String ERROR_SEMESTER_NOT_A_NUMBER = "Semester entered must be a number!";
private static final String ERROR_ILLEGAL_FORWARD
= "This module cannot be shifted to a later semester as "
+ "you are not allowed to retake a module that you have passed!";
Expand Down Expand Up @@ -143,21 +141,28 @@ public void editModuleSemester(Scanner in, String moduleCode) throws AcademicExc
System.out.println(VALID_SEMESTERS);
String newValue = in.nextLine().trim();

if (!ModuleValidator.isValidSemester(parseInt(newValue))) {
Integer newSemester;
try {
newSemester = parseInt(newValue);
} catch (Exception e) {
throw new AcademicException(ERROR_SEMESTER_NOT_A_NUMBER);
}

if (!ModuleValidator.isValidSemester(newSemester)) {
throw new AcademicException(ERROR_INVALID_SEMESTER_INDEX);
}

verifyRepeatedSemester(parseInt(newValue), currentPerson, moduleCode, modulesList);
verifyRepeatedSemester(newSemester, currentPerson, moduleCode, modulesList);
ArrayList<Integer> moduleIndexList = modulesAddedMap.get(moduleCode);
PartialModule currentSemesterModule = modulesList.get(moduleIndexList.get(indexToUpdate));

if (parseInt(newValue) > currentSemesterModule.getSemesterIndex() && moduleIndexList.size() > 1) {
if (notAllowedSemesterUpdateForward(parseInt(newValue), modulesList, moduleCode)
if (newSemester > currentSemesterModule.getSemesterIndex() && moduleIndexList.size() > 1) {
if (notAllowedSemesterUpdateForward(newSemester, modulesList, moduleCode)
&& modChecker.isRetakeGrade(currentSemesterModule.getGrade())) {
throw new AcademicException(ERROR_ILLEGAL_FORWARD);
}
} else if (moduleIndexList.size() > 1) {
if (notAllowedSemesterUpdateBackward(parseInt(newValue), modulesList, moduleCode)
if (notAllowedSemesterUpdateBackward(newSemester, modulesList, moduleCode)
&& !modChecker.isRetakeGrade(currentSemesterModule.getGrade())) {
throw new AcademicException(ERROR_ILLEGAL_BACKWARD);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package seedu.duke.apps.academicplanner.commons;

import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import seedu.duke.apps.academicplanner.exceptions.AcademicException;
import seedu.duke.global.objects.PartialModule;
import seedu.duke.global.objects.Person;

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

import static java.lang.Integer.parseInt;

//@@author Khenus

/**
Expand Down Expand Up @@ -211,7 +213,9 @@ public static boolean fromFailingToPass(String previousGrade, String newGrade) {
* Returns the latest semester taken for the module.
*
* @param modulesAddedList List of modules added
* @return latestSemester
* @param indexArrayList List of indexes for all occurrences of current module
*
* @return latestSemester the latest semester
*/
public static int getLatestSemester(ArrayList<PartialModule> modulesAddedList, ArrayList<Integer> indexArrayList) {
int latestSemester = modulesAddedList.get(indexArrayList.get(0)).getSemesterIndex();
Expand All @@ -227,6 +231,29 @@ public static int getLatestSemester(ArrayList<PartialModule> modulesAddedList, A
return latestSemester;
}

/**
* Returns the occurrence of the module taken in the latest semester.
*
* @param modulesAddedList List of modules added
* @param indexArrayList List of indexes for all occurrences of current module
*
* @return latestSemester the occurrence of the module taken in the latest semester
*/
public static PartialModule getLatestSemesterModule(ArrayList<PartialModule> modulesAddedList,
ArrayList<Integer> indexArrayList) {
PartialModule latestSemesterModule = modulesAddedList.get(indexArrayList.get(0));

for (int index = 0; index < indexArrayList.size(); index++) {
Integer currentIndexForModule = indexArrayList.get(index);
PartialModule currentSemesterModule = modulesAddedList.get(currentIndexForModule);

if (currentSemesterModule.getSemesterIndex() > latestSemesterModule.getSemesterIndex()) {
latestSemesterModule = currentSemesterModule;
}
}
return latestSemesterModule;
}

/**
* Returns semester index of latest failed module.
*
Expand Down

0 comments on commit 095319a

Please sign in to comment.