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

Update Sort Command #125

Merged
merged 1 commit into from
Apr 14, 2024
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
7 changes: 3 additions & 4 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Objects.requireNonNull;

import java.util.Comparator;
import java.util.Optional;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand Down Expand Up @@ -52,9 +51,9 @@ public SortCommand(TestNameEqualsKeywordPredicate predicate, boolean isReverse)
public CommandResult execute(Model model) {
requireNonNull(model);
Comparator<Student> gradeComparator = (student1, student2) -> {
String grade1 = Optional.ofNullable(student1.getGradeForTest(predicate.keyword)).orElse("0");
String grade2 = Optional.ofNullable(student2.getGradeForTest(predicate.keyword)).orElse("0");
return grade1.compareTo(grade2);
int grade1 = student1.getGradeForTest(predicate.keyword);
int grade2 = student2.getGradeForTest(predicate.keyword);
return Integer.compare(grade1, grade2);
};

if (isReverse) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/model/grade/Grade.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Grade {

public final String testAndGrade;
public final String testName;
public final String grade;
public final int grade;

/**
* Constructs an {@code Grade}.
Expand All @@ -45,7 +45,7 @@ public Grade(String testAndGrade) {
checkArgument(parts[0].trim().matches(TEST_NAME_VALIDATION_REGEX), MESSAGE_CONSTRAINTS);
checkArgument(parts[1].trim().matches(GRADE_VALIDATION_REGEX), MESSAGE_CONSTRAINTS);
this.testName = parts[0].trim();
this.grade = parts[1].trim();
this.grade = Integer.parseInt(parts[1].trim());
this.testAndGrade = testAndGrade;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public boolean equals(Object other) {
}

Grade otherGrade = (Grade) other;
return testName.equals(otherGrade.testName) && grade.equals(otherGrade.grade);
return testName.equals(otherGrade.testName) && grade == otherGrade.grade;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/student/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public Set<Grade> getGrades() {
* @param testName The name of the test to find the grade for.
* @return The grade for the specified test as a String, or null if the student does not have a grade for that test.
*/
public String getGradeForTest(String testName) {
public int getGradeForTest(String testName) {
for (Grade grade : grades) {
if (grade.testName.equals(testName)) {
return grade.grade;
}
}
return null;
return 0;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public TestNameEqualsKeywordPredicate(String keyword) {
@Override
public boolean test(Student student) {
return student.getGrades().stream()
.anyMatch(grades -> grades.testName.equalsIgnoreCase(keyword));
.anyMatch(grades -> grades.testName.equals(keyword));
}

@Override
Expand Down
21 changes: 10 additions & 11 deletions src/test/java/seedu/address/logic/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -61,9 +60,9 @@ public void execute_zeroKeywords_nostudentFound() {
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 0);
TestNameEqualsKeywordPredicate predicate = preparePredicate(" ");
Comparator<Student> gradeComparator = (student1, student2) -> {
String grade1 = Optional.ofNullable(student1.getGradeForTest(predicate.keyword)).orElse("0");
String grade2 = Optional.ofNullable(student2.getGradeForTest(predicate.keyword)).orElse("0");
return grade1.compareTo(grade2);
int grade1 = student1.getGradeForTest(predicate.keyword);
int grade2 = student2.getGradeForTest(predicate.keyword);
return Integer.compare(grade1, grade2);
};
SortCommand command = new SortCommand(predicate, false);
expectedModel.sortFilteredStudentList(gradeComparator, predicate);
Expand All @@ -74,11 +73,11 @@ public void execute_zeroKeywords_nostudentFound() {
@Test
public void execute_multipleKeywords_multiplestudentsFound() {
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 2);
TestNameEqualsKeywordPredicate predicate = preparePredicate("Ca1");
TestNameEqualsKeywordPredicate predicate = preparePredicate("ca1");
Comparator<Student> gradeComparator = (student1, student2) -> {
String grade1 = Optional.ofNullable(student1.getGradeForTest(predicate.keyword)).orElse("0");
String grade2 = Optional.ofNullable(student2.getGradeForTest(predicate.keyword)).orElse("0");
return grade1.compareTo(grade2);
int grade1 = student1.getGradeForTest(predicate.keyword);
int grade2 = student2.getGradeForTest(predicate.keyword);
return Integer.compare(grade1, grade2);
};
SortCommand command = new SortCommand(predicate, false);
expectedModel.sortFilteredStudentList(gradeComparator, predicate);
Expand All @@ -90,9 +89,9 @@ public void execute_multipleKeywords_multiplestudentsFound() {
public void toStringMethod() {
TestNameEqualsKeywordPredicate predicate = new TestNameEqualsKeywordPredicate("keyword");
Comparator<Student> gradeComparator = (student1, student2) -> {
String grade1 = Optional.ofNullable(student1.getGradeForTest(predicate.keyword)).orElse("0");
String grade2 = Optional.ofNullable(student2.getGradeForTest(predicate.keyword)).orElse("0");
return grade1.compareTo(grade2);
int grade1 = student1.getGradeForTest(predicate.keyword);
int grade2 = student2.getGradeForTest(predicate.keyword);
return Integer.compare(grade1, grade2);
};
SortCommand sortCommand = new SortCommand(predicate, false);
String expected = SortCommand.class.getCanonicalName() + "{predicate=" + predicate + ", "
Expand Down
Loading