Skip to content

Commit

Permalink
Merge pull request #128 from RCPilot1604/branch-input-validation
Browse files Browse the repository at this point in the history
[bugfixes] Add input validation for Matric Number and change weightage from double to int
  • Loading branch information
jinzihan2002 authored Nov 6, 2024
2 parents f1a6e7b + 9947ea6 commit fd4c8ea
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ src/main/META-INF/

/data
/text-ui-test/data
text-ui-test/input.txt
8 changes: 4 additions & 4 deletions src/main/java/tutorlink/command/AddComponentCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public CommandResult execute(AppState appState, HashMap<String, String> hashmap)
throw new IllegalValueException(Commons.ERROR_NULL);
}

double weightage = convertWeightageToValidDouble(weightageNumber);
int weightage = convertWeightageToValidInt(weightageNumber);

double maxScore = convertMaxScoreToValidDouble(maxScoreNumber);
appState.components.addComponent(new Component(componentName, maxScore, weightage));
return new CommandResult(String.format(Commons.ADD_COMPONENT_SUCCESS,
componentName, weightageNumber, maxScoreNumber));
}

private static double convertWeightageToValidDouble(String weightageNumber) {
private static int convertWeightageToValidInt(String weightageNumber) {
try {
double weightage = Double.parseDouble(weightageNumber);
if (weightage < 0.0 || weightage > 1.0) {
int weightage =Integer.parseInt(weightageNumber);
if (weightage < 0 || weightage > 100) {
throw new IllegalValueException(Commons.ERROR_INVALID_WEIGHTAGE);
}
return weightage;
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/tutorlink/command/AddGradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import tutorlink.result.CommandResult;
import tutorlink.student.Student;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static tutorlink.lists.StudentList.STUDENT_NOT_FOUND;

Expand All @@ -31,6 +33,12 @@ public CommandResult execute(AppState appstate, HashMap<String, String> hashmap)
if (matricNumber == null || componentDescription == null || scoreNumber == null) {
throw new IllegalValueException(Commons.ERROR_NULL);
}
matricNumber = matricNumber.toUpperCase();
Pattern pattern = Pattern.compile(Commons.MATRIC_NUMBER_REGEX);
Matcher matcher = pattern.matcher(matricNumber);
if (!matcher.find()) {
throw new IllegalValueException(Commons.ERROR_ILLEGAL_MATRIC_NUMBER);
}

Component component = findComponentFromComponents(appstate, componentDescription);

Expand Down Expand Up @@ -83,7 +91,7 @@ private static Student findStudentFromStudents(AppState appstate, String matricN

private static Component findComponentFromComponents(AppState appstate, String componentDescription) {
//Get component object using String componentDescription
ComponentList componentFilteredList = appstate.components.findComponent(componentDescription.toUpperCase());
ComponentList componentFilteredList = appstate.components.findComponent(componentDescription);
Component component;
if (componentFilteredList.size() == 1) {
component = componentFilteredList.getComponentArrayList().get(0);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tutorlink/command/AddStudentCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import tutorlink.exceptions.IllegalValueException;
import tutorlink.exceptions.TutorLinkException;
import tutorlink.result.CommandResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddStudentCommand extends Command {

Expand All @@ -20,6 +22,12 @@ public CommandResult execute(AppState appState, HashMap<String, String> hashmap)
if (matricNumber == null || name == null) {
throw new IllegalValueException(Commons.ERROR_NULL);
}
matricNumber = matricNumber.toUpperCase();
Pattern pattern = Pattern.compile(Commons.MATRIC_NUMBER_REGEX);
Matcher matcher = pattern.matcher(matricNumber);
if (!matcher.find()) {
throw new IllegalValueException(Commons.ERROR_ILLEGAL_MATRIC_NUMBER);
}
appState.students.addStudent(matricNumber, name);
return new CommandResult(String.format(Commons.ADD_STUDENT_SUCCESS, name, matricNumber));
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/tutorlink/command/DeleteGradeCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tutorlink.command;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import tutorlink.appstate.AppState;
import tutorlink.commons.Commons;
Expand All @@ -24,7 +26,12 @@ public CommandResult execute(AppState appState, HashMap<String, String> hashmap)
if (matricNumber == null || componentDescription == null) {
throw new IllegalValueException(Commons.ERROR_NULL);
}

matricNumber = matricNumber.toUpperCase();
Pattern pattern = Pattern.compile(Commons.MATRIC_NUMBER_REGEX);
Matcher matcher = pattern.matcher(matricNumber);
if (!matcher.find()) {
throw new IllegalValueException(Commons.ERROR_ILLEGAL_MATRIC_NUMBER);
}
//Check number of students with matricNumber
StudentList filteredList = appState.students.findStudentByMatricNumber(matricNumber);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tutorlink/command/DeleteStudentCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tutorlink.command;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import tutorlink.appstate.AppState;
import tutorlink.commons.Commons;
Expand All @@ -19,6 +21,12 @@ public CommandResult execute(AppState appState, HashMap<String, String> hashmap)
if (matricNumber == null) {
throw new IllegalValueException(Commons.ERROR_NULL);
}
matricNumber = matricNumber.toUpperCase();
Pattern pattern = Pattern.compile(Commons.MATRIC_NUMBER_REGEX);
Matcher matcher = pattern.matcher(matricNumber);
if (!matcher.find()) {
throw new IllegalValueException(Commons.ERROR_ILLEGAL_MATRIC_NUMBER);
}
appState.students.deleteStudent(matricNumber);
appState.grades.deleteGradesByMatric(matricNumber);
return new CommandResult(String.format(Commons.DELETE_STUDENT_SUCCESS, matricNumber));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/tutorlink/commons/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public class Commons {
public static final String ERROR_NULL = "Error! Null parameter passed!";

//@@author RCPilot1604

//Input Validation
public static final String MATRIC_NUMBER_REGEX = "A\\d{7}[A-Z]";
public static final String ERROR_ILLEGAL_MATRIC_NUMBER = "Error! Ensure matric " +
"number is of the form A\\d{7}[A-Z] (case insensitive)";
//Student
public static final String ADD_STUDENT_SUCCESS = "Student %s (%s) added successfully!";
public static final String ERROR_DUPLICATE_STUDENT =
Expand All @@ -27,7 +32,7 @@ public class Commons {
public static final String DELETE_COMPONENT_SUCCESS = "Component %s successfully deleted";
public static final String ERROR_COMPONENT_NOT_FOUND = "Error! Component (Name %s) not found";
public static final String ERROR_DUPLICATE_COMPONENT = "Error! Component (Name %s) already exists in the list!";
public static final String ERROR_INVALID_WEIGHTAGE = "Error! Weightage must be double that is between 0 and 1!";
public static final String ERROR_INVALID_WEIGHTAGE = "Error! Weightage must be integer that is between 0 and 100!";
public static final String ERROR_INVALID_MAX_SCORE =
"Error! Max Score must be double that is more than or equal to 0!";

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tutorlink/component/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
public class Component {
private String name;
private double maxScore;
private double weight;
private int weight;

public Component(String name, double maxScore, double weight) {
public Component(String name, double maxScore, int weight) {
this.name = name;
this.maxScore = maxScore;
this.weight = weight;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/tutorlink/lists/ComponentList.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void deleteComponent(Component component) throws ComponentNotFoundExcepti

@Override
public String toString() {
return IntStream.range(0, componentArrayList.size())
return "\t" +
IntStream.range(0, componentArrayList.size())
.mapToObj(i -> (i + 1) + ": " + componentArrayList.get(i))
.collect(Collectors.joining("\n\t"));
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/tutorlink/lists/GradeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public GradeList(ArrayList<Grade> gradeArrayList) {
//@@author RCPilot1604
public void deleteGrade(String matricNumber, String componentDescription) throws GradeNotFoundException {
matricNumber = matricNumber.toUpperCase();
componentDescription = componentDescription.toUpperCase();
for (Grade grade : gradeArrayList) {
if (grade.getStudent().getMatricNumber().equals(matricNumber)
&& grade.getComponent().getName().toUpperCase().equals(componentDescription)) {
if (grade.getStudent().getMatricNumber().equals(matricNumber.toUpperCase())
&& grade.getComponent().getName().toUpperCase().equals(componentDescription.toUpperCase())) {
gradeArrayList.remove(grade);
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/tutorlink/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tutorlink.command.FindStudentCommand;
import tutorlink.command.InvalidCommand;
import tutorlink.command.ListComponentCommand;
import tutorlink.command.ListGradeCommand;
import tutorlink.command.ListStudentCommand;
import tutorlink.command.AddGradeCommand;

Expand Down Expand Up @@ -62,6 +63,9 @@ public Command getCommand(String line) {
case ListComponentCommand.COMMAND_WORD:
return new ListComponentCommand();

case ListGradeCommand.COMMAND_WORD:
return new ListGradeCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand(); // Lists all students

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tutorlink/storage/ComponentStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private Component getComponentFromFileLine(String fileLine) {
String[] stringParts = fileLine.split(READ_DELIMITER);
String name = stringParts[0];
double maxScore = Double.parseDouble(stringParts[1]);
double weight = Double.parseDouble(stringParts[2]);
int weight = Integer.parseInt(stringParts[2]);
return new Component(name, maxScore, weight);
}

Expand Down
17 changes: 9 additions & 8 deletions src/test/java/tutorlink/command/AddComponentCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tutorlink.appstate.AppState;
import tutorlink.commons.Commons;
import tutorlink.exceptions.IllegalValueException;
import tutorlink.result.CommandResult;

Expand All @@ -29,11 +30,11 @@ void setup() {
@Test
void execute_validArguments_componentAddedSuccessfully() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "0.3");
arguments.put("w/", "30");
arguments.put("m/", "100");
CommandResult result = command.execute(appState, arguments);
assertNotNull(result);
assertEquals("Component Quiz 1 of weight 0.3, with max score 100 added successfully!", result.toString());
assertEquals("Component Quiz 1 of weight 30, with max score 100 added successfully!", result.toString());
assertEquals(1, appState.components.getComponentArrayList().size());
}

Expand All @@ -59,13 +60,13 @@ void execute_missingWeightageArgument_throwsIllegalValueException() {
@Test
void execute_extraArgument_componentAddedSuccessfully() {
arguments.put("c/", "Quiz 1");
arguments.put("w/", "0.5");
arguments.put("w/", "50");
arguments.put("m/", "100");
arguments.put("extra/", "extra value");

CommandResult result = command.execute(appState, arguments);
assertNotNull(result);
assertEquals("Component Quiz 1 of weight 0.5, with max score 100 added successfully!", result.toString());
assertEquals("Component Quiz 1 of weight 50, with max score 100 added successfully!", result.toString());
assertEquals(1, appState.components.getComponentArrayList().size());
}

Expand All @@ -78,7 +79,7 @@ void execute_weightageOutOfRange_throwsIllegalValueException() {
IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Weightage must be double that is between 0 and 1!", exception.getMessage());
assertEquals(Commons.ERROR_INVALID_WEIGHTAGE, exception.getMessage());
}

@Test
Expand All @@ -90,7 +91,7 @@ void execute_negativeMaxScore_throwsIllegalValueException() {
IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Max Score must be double that is more than or equal to 0!", exception.getMessage());
assertEquals(Commons.ERROR_INVALID_WEIGHTAGE, exception.getMessage());
}

@Test
Expand All @@ -102,7 +103,7 @@ void execute_weightageNotDouble_throwsIllegalValueException() {
IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Weightage must be double that is between 0 and 1!", exception.getMessage());
assertEquals(Commons.ERROR_INVALID_WEIGHTAGE, exception.getMessage());
}

@Test
Expand All @@ -114,7 +115,7 @@ void execute_maxScoreNotDouble_throwsIllegalValueException() {
IllegalValueException exception = assertThrows(IllegalValueException.class, () -> {
command.execute(appState, arguments);
});
assertEquals("Error! Max Score must be double that is more than or equal to 0!", exception.getMessage());
assertEquals(Commons.ERROR_INVALID_WEIGHTAGE, exception.getMessage());
}
}
//@@author
14 changes: 7 additions & 7 deletions src/test/java/tutorlink/command/AddGradeCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void addGrade_allArgumentsComponent_successful() {
//Create component
String examName = "Exam Under Test";
double examMaxScore = 100.0;
double examWeight = 50.0;
int examWeight = 50;
Component exam = new Component(examName,examMaxScore, examWeight);

appState.components.addComponent(exam);
Expand Down Expand Up @@ -83,7 +83,7 @@ void addGrade_missingComponentDescriptionComponent_illegalValueExceptionThrown()
//Create component
String assignmentName = "Assignment Under Test";
double assignmentMaxScore = 100.0;
double assignmentWeight = 50.0;
int assignmentWeight = 50;
Component assignment = new Component(assignmentName,assignmentMaxScore, assignmentWeight);

appState.components.addComponent(assignment);
Expand Down Expand Up @@ -123,7 +123,7 @@ void addGrade_nonDoubleScoreComponent_illegalValueExceptionThrown() {
//Create component
String examName = "Exam Under Test";
double examMaxScore = 100.0;
double examWeight = 50.0;
int examWeight = 50;
Component exam = new Component(examName,examMaxScore, examWeight);

appState.components.addComponent(exam);
Expand Down Expand Up @@ -161,7 +161,7 @@ void addGrade_scoreMoreThanMax_throwIllegalValueException() {
//Create component
String examName = "Exam Under Test";
double examMaxScore = 100.0;
double examWeight = 50.0;
int examWeight = 50;
Component exam = new Component(examName,examMaxScore, examWeight);

appState.components.addComponent(exam);
Expand Down Expand Up @@ -199,7 +199,7 @@ void addGrade_scoreNegative_throwIllegalValueException() {
//Create component
String examName = "Exam Under Test";
double examMaxScore = 100.0;
double examWeight = 50.0;
int examWeight = 50;
Component exam = new Component(examName,examMaxScore, examWeight);

appState.components.addComponent(exam);
Expand Down Expand Up @@ -260,7 +260,7 @@ void addGrade_studentNotFoundComponent_throwStudentNotFoundException() {
//Create component
String assignmentName = "Assignment Under Test";
double assignmentMaxScore = 100.0;
double assignmentWeight = 50.0;
int assignmentWeight = 50;
Component assignment = new Component(assignmentName,assignmentMaxScore, assignmentWeight);

appState.components.addComponent(assignment);
Expand Down Expand Up @@ -298,7 +298,7 @@ void addGrade_duplicateGradeComponentDiffScores_throwDuplicateGradeException() {
//Create Component component
String examName = "Test";
double examMaxScore = 100.0;
double examWeight = 50.0;
int examWeight = 50;
Component exam = new Component(examName,examMaxScore, examWeight);

appState.components.addComponent(exam);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class DeleteComponentCommandTest {
@BeforeEach
void setup() {
appState = new AppState();
appState.components.addComponent(new Component("finals", 40.0, 0.4));
appState.components.addComponent(new Component("iP", 20.0, 0.1));
appState.components.addComponent(new Component("lectures", 10.0, 0.1));
appState.components.addComponent(new Component("finals", 40.0, 40));
appState.components.addComponent(new Component("iP", 20.0, 10));
appState.components.addComponent(new Component("lectures", 10.0, 10));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private GradeList addGradeForTest(Parser parser, AppState appState, String compo
//Create component
String examName = componentName;
double examMaxScore = 100.0;
double examWeight = 50.0;
int examWeight = 50;
Component exam = new Component(examName,examMaxScore, examWeight);

appState.components.addComponent(exam);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/tutorlink/command/DeleteStudentCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ void setup() {
appState.students.addStudent("A2468102C", "Arthur Mueller");

appState.grades.addGrade(new Grade(new Component("Take Home Quiz 1", 10.0,
0.2), new Student("A1234567X", "John Doe"), 7.0));
20), new Student("A1234567X", "John Doe"), 7.0));
appState.grades.addGrade(new Grade(new Component("Take Home Quiz 2", 10.0,
0.2), new Student("A1234567X", "John Doe"), 9.0));
20), new Student("A1234567X", "John Doe"), 9.0));
appState.grades.addGrade(new Grade(new Component("Take Home Quiz 1", 10.0,
0.2), new Student("A7654321B", "Jane Smith"), 9.0));
20), new Student("A7654321B", "Jane Smith"), 9.0));
}

@Test
Expand Down
Loading

0 comments on commit fd4c8ea

Please sign in to comment.