forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from joelgoh1/EditSortCommand
Edit sort command
- Loading branch information
Showing
10 changed files
with
232 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/seedu/address/model/student/TestNameEqualsKeywordPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package seedu.address.model.student; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that any of {@code student}'s {@code Grades} matches any of the keywords given. | ||
*/ | ||
public class TestNameEqualsKeywordPredicate implements Predicate<Student> { | ||
public final String keyword; | ||
|
||
public TestNameEqualsKeywordPredicate(String keyword) { | ||
this.keyword = keyword; | ||
} | ||
|
||
@Override | ||
public boolean test(Student student) { | ||
return student.getGrades().stream() | ||
.anyMatch(grades -> grades.testName.equalsIgnoreCase(keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof TestNameEqualsKeywordPredicate)) { | ||
return false; | ||
} | ||
|
||
TestNameEqualsKeywordPredicate gradesContainsKeywordsPredicate = | ||
(TestNameEqualsKeywordPredicate) other; | ||
return keyword.equals(gradesContainsKeywordsPredicate.keyword); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keyword).toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/test/java/seedu/address/logic/commands/SortCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.logic.Messages.MESSAGE_STUDENTS_LISTED_OVERVIEW; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalStudents.ALICE; | ||
import static seedu.address.testutil.TypicalStudents.BENSON; | ||
import static seedu.address.testutil.TypicalStudents.getTypicalAddressBook; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.student.Student; | ||
import seedu.address.model.student.TestNameEqualsKeywordPredicate; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) for {@code FilterCommand}. | ||
*/ | ||
public class SortCommandTest { | ||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void equals() { | ||
TestNameEqualsKeywordPredicate firstPredicate = | ||
new TestNameEqualsKeywordPredicate("first"); | ||
TestNameEqualsKeywordPredicate secondPredicate = | ||
new TestNameEqualsKeywordPredicate("second"); | ||
|
||
SortCommand sortFirstCommand = new SortCommand(firstPredicate, false); | ||
SortCommand sortSecondCommand = new SortCommand(secondPredicate, false); | ||
|
||
// same object -> returns true | ||
assertTrue(sortFirstCommand.equals(sortFirstCommand)); | ||
|
||
// same values -> returns true | ||
SortCommand sortFirstCommandCopy = new SortCommand(firstPredicate, false); | ||
assertTrue(sortFirstCommand.equals(sortFirstCommandCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(sortFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(sortFirstCommand.equals(null)); | ||
|
||
// different student -> returns false | ||
assertFalse(sortFirstCommand.equals(sortSecondCommand)); | ||
} | ||
|
||
@Test | ||
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); | ||
}; | ||
SortCommand command = new SortCommand(predicate, false); | ||
expectedModel.sortFilteredStudentList(gradeComparator, predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Collections.emptyList(), model.getFilteredStudentList()); | ||
} | ||
|
||
@Test | ||
public void execute_multipleKeywords_multiplestudentsFound() { | ||
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 2); | ||
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); | ||
}; | ||
SortCommand command = new SortCommand(predicate, false); | ||
expectedModel.sortFilteredStudentList(gradeComparator, predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Arrays.asList(ALICE, BENSON), model.getFilteredStudentList()); | ||
} | ||
|
||
@Test | ||
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); | ||
}; | ||
SortCommand sortCommand = new SortCommand(predicate, false); | ||
String expected = SortCommand.class.getCanonicalName() + "{predicate=" + predicate + ", " | ||
+ "isReverse=" + false + "}"; | ||
assertEquals(expected, sortCommand.toString()); | ||
} | ||
|
||
/** | ||
* Parses {@code userInput} into a {@code TestNameEqualsKeywordPredicate}. | ||
*/ | ||
private TestNameEqualsKeywordPredicate preparePredicate(String userInput) { | ||
return new TestNameEqualsKeywordPredicate(userInput); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/seedu/address/logic/parser/SortCommandParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.SortCommand; | ||
import seedu.address.model.student.TestNameEqualsKeywordPredicate; | ||
|
||
public class SortCommandParserTest { | ||
|
||
private SortCommandParser parser = new SortCommandParser(); | ||
|
||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
assertParseFailure(parser, " ", | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
@Test | ||
public void parse_validArgs_returnsSortCommand() { | ||
// no leading and trailing whitespaces | ||
SortCommand expectedSortCommand = | ||
new SortCommand(new TestNameEqualsKeywordPredicate("ca1"), false); | ||
assertParseSuccess(parser, "ca1", expectedSortCommand); | ||
|
||
// multiple whitespaces between keywords | ||
assertParseSuccess(parser, " \n ca1 \t", expectedSortCommand); | ||
} | ||
|
||
} |