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

Edit Parser exception message to improve clarity #176

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Marks the module that has been taken and update its grade.
<br>
<br>

Format:`[MODULE CODE] g/[GRADE]`
Format:`mark [MODULE CODE] g/[GRADE]`

<br>
<br>
Expand Down
57 changes: 37 additions & 20 deletions src/main/java/seedu/penus/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.penus.logic.parser.CliSyntax.REMOVE;
import static seedu.penus.logic.parser.CliSyntax.PLAN;
import static seedu.penus.logic.parser.CliSyntax.TAKEN;

import static seedu.penus.logic.parser.CliSyntax.DETAILS;
import static seedu.penus.logic.parser.CliSyntax.HELP;
import static seedu.penus.logic.parser.CliSyntax.INIT;
Expand Down Expand Up @@ -100,7 +101,7 @@ public Command planParser(String args) throws PenusException {
}
String[] planDetails = args.split(" y/| s/", 3);
if (planDetails.length != 3 || planDetails[1].length() == 0 || planDetails[2].length() == 0) {
throw new InvalidFormatException("Try again in the format: PLAN CODE y/YEAR s/SEM");
throw new InvalidFormatException("Try again in the format: plan MODULE_CODE y/YEAR s/SEM");
}

String moduleCode = planDetails[0].toUpperCase().trim();
Expand Down Expand Up @@ -133,7 +134,7 @@ public Command planParser(String args) throws PenusException {
public Command takenParser(String args) throws PenusException {
String[] takenDetails = args.split(" y/| s/| g/", 4);
if (takenDetails.length != 4) {
throw new InvalidFormatException("Try again in the format: PLAN CODE y/YEAR s/SEM g/GRADE");
throw new InvalidFormatException("Try again in the format: taken MODULE_CODE y/YEAR s/SEM g/GRADE");
}
if (takenDetails[1].length() == 0 || takenDetails[2].length() == 0 || takenDetails[3].length() == 0) {
throw new InvalidFormatException("Try again, y/ s/ g/ cannot be empty");
Expand Down Expand Up @@ -163,15 +164,16 @@ public Command takenParser(String args) throws PenusException {
}

/**
* Parses the given {@code String} of arguments in the context of the MarkCommand
* and returns an MarkCommand object for execution.
* Parses the given {@code String} of arguments in the context of the
* MarkCommand and returns an MarkCommand object for execution.
*
* @param args string
* @return command object
* @throws PenusException if the user input does not conform the expected format
*/
public Command markParser(String args) throws PenusException {
if (!args.contains("g/")) {
throw new InvalidFormatException("Try again in the format: mark MODULECODE g/GRADE");
throw new InvalidFormatException("\tTry again in the format: mark MODULE_CODE g/GRADE");
}
String[] details = args.split(" g/");
if (!Grade.isValid(details[1].trim())) {
Expand All @@ -183,48 +185,62 @@ public Command markParser(String args) throws PenusException {
return new MarkCommand(moduleCode, grade);
}

/**
* Parses the given {@code String} of arguments in the context of the
* ListCommand and returns an ListCommand object for execution.
*
* @param args string
* @return Command object
* @throws PenusException if the user input does not conform the expected format
*/
public Command listParser(String args) throws PenusException {
if (args.equals("") || args.equals(" ")) {
//list command with all modules
// list command with all modules
return new ListCommand();
}

if (args.contains("s/") && !args.contains("y/")) { // Semester specified but year not specified
throw new InvalidFormatException(
"\tTry again, y/ must not be empty if s/ is not empty. " +
"To show modules for that semester, please specify the year of study.");
}

String[] details = args.split("y/| s/",3);
String[] details = args.split("y/| s/", 3);
int year = 0;
int semester = 0;
try {
year = Integer.parseInt(details[1].trim());
} catch (NumberFormatException e) {
throw new InvalidFormatException("Must be specified as an integer!");
} catch (NumberFormatException nfe) {
throw new InvalidFormatException("\tYear must be specified as an integer!");
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidFormatException("\tTry again in the format: list y/YEAR s/SEM or list y/YEAR or list");
}
if (year < 1 || year > 4) {
throw new InvalidFormatException("Year must be 1 to 4. Please try again.");
throw new InvalidFormatException("\tYear must be 1 to 4. Please try again.");
}

if (details.length == 2) {
return new ListCommand(year,0);
return new ListCommand(year, 0);
} else if (details.length == 3) {
if (args.contains("s/")) {
try {
semester = Integer.parseInt(details[2].trim());
} catch (NumberFormatException e) {
throw new InvalidFormatException("Semester must be specified as an integer!");
} catch (NumberFormatException nfe) {
throw new InvalidFormatException("\tSemester must be specified as an integer!");
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidFormatException(
"\tTry again in the format: list y/YEAR s/SEM or list y/YEAR or list");
}
}

if (semester != 1 && semester != 2) {
throw new InvalidFormatException("Semester must be 1 or 2!");
throw new InvalidFormatException("\tSemester must be 1 or 2!");
}
return new ListCommand(year, semester);
} else {
throw new InvalidFormatException("Try again in the format: list y/YEAR s/SEM or list y/YEAR or list");
throw new InvalidFormatException("\tTry again in the format: list y/YEAR s/SEM or list y/YEAR or list");
}
}
}

/**
* Parses the given {@code String} of arguments in the context of the RemoveCommand
Expand All @@ -239,7 +255,7 @@ public Command removeParser(String args) throws PenusException {
throw new InvalidModuleException(args);
}
if (args.equals(" ")) {
throw new InvalidModuleException("Please specify a module");
throw new InvalidModuleException("\tPlease specify a module");
}

String moduleCode = args.toUpperCase().trim();
Expand Down Expand Up @@ -297,6 +313,7 @@ public Command initParser (String args) throws PenusException {
/**
* Parses the given {@code String} of arguments in the context of the ClearCommand
* and returns an ClearCommand object for execution.
*
* @param args arguments
* @return command object
* @throws PenusException if the user input does not conform the expected format
Expand All @@ -322,7 +339,7 @@ public Command clearParser(String args) throws PenusException{

try {
year = Integer.parseInt(details[1].trim());
} catch (NumberFormatException e) {
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
throw new InvalidFormatException("Must be specified as an integer!");
}
//Invalid year
Expand All @@ -338,7 +355,7 @@ public Command clearParser(String args) throws PenusException{
if (args.contains("s/")) {
try {
semester = Integer.parseInt(details[2].trim());
} catch (NumberFormatException e) {
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
throw new InvalidFormatException("Semester must be specified as an integer!");
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/penus/logic/utils/DetailsCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static String getDetails(String module) {
}
return title + "\n" + description + "\n" + prereqs + "\n" + credits + "\n" + suStatusDescription;
} catch (Exception e) {
return "This information is not available";
return ": This information is not available";
}
}

}
29 changes: 17 additions & 12 deletions src/main/java/seedu/penus/logic/utils/Grade.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
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
*/
public static double getGradePoint(String grade) throws InvalidGradeException {
double gradePoint;
switch(grade.toUpperCase()) {
switch (grade.toUpperCase()) {
case "A+":
case "A":
gradePoint = 5.0;
Expand Down Expand Up @@ -65,26 +66,26 @@ 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
*/
public static Boolean isValid(String grade) {
List<String> validGrades = Arrays.asList(
"A+", "A", "A-",
"B+", "B", "B-",
"C+", "C",
"D+", "D",
"F", "S", "U"
);
"A+", "A", "A-",
"B+", "B", "B-",
"C+", "C",
"D+", "D",
"F", "S", "U");

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
Expand All @@ -109,9 +110,12 @@ public static double calculateOverallCAP(List<Module> moduleList) throws Invalid
}

/**
* For every module taken in a semester, calculate weighted score = number of MC * grade
* Sum up weighted score for all mods and divide by total MCs taken in the semester
* For every module taken in a semester, calculate weighted score = number of MC
* * grade
* 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
Expand Down Expand Up @@ -139,6 +143,7 @@ public static double calculateSemCAP(List<String[]> semArray) throws InvalidGrad

/**
* 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 @@ -156,6 +161,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 All @@ -166,9 +172,8 @@ public static String getSemCAP(List<String[]> semArray) throws InvalidGradeExcep
semCapMessage = "Semester CAP : 0.00\n";
} else {
Double semCAP = calculateSemCAP(semArray);
semCapMessage = String.format("Semester CAP : %.2f\n", semCAP);
semCapMessage = String.format("Semester CAP : %.2f\n", semCAP);
}
return semCapMessage;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void getDetailsForCS2113() {
void getDetailsForInvalidModule() {
String moduleCode = "INVALID MODULE";
String actualOutput = details.getDetails(moduleCode);
String expectedOutput = "This information is not available";
String expectedOutput = ": This information is not available";

assertEquals(expectedOutput, actualOutput);
}
Expand All @@ -35,7 +35,7 @@ void getDetailsForInvalidModule() {
void getDetailsForNull() {
String moduleCode = null;
String actualOutput = details.getDetails(moduleCode);
String expectedOutput = "This information is not available";
String expectedOutput = ": This information is not available";

assertEquals(expectedOutput, actualOutput);
}
Expand Down