forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to filter by card difficulty with list command (#271)
* add performance tests * Remove use of dummy Performance * add card performance matching predicate * edit list command to take in and filter by performance arguments * add comments * fix style * fix style * add listcommand tests * update user guide
- Loading branch information
Showing
11 changed files
with
300 additions
and
7 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
32 changes: 32 additions & 0 deletions
32
src/main/java/seedu/address/logic/parser/ListCommandParser.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,32 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.logic.commands.ListCommand; | ||
import seedu.address.model.deck.CardPerformanceMatchesPerformancesPredicate; | ||
import seedu.address.model.deck.Performance; | ||
|
||
/** | ||
* Parses input elements and creates a new ListCommand object | ||
*/ | ||
public class ListCommandParser implements ParserInterface<ListCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the ListCommand | ||
* and returns an ListCommand object for execution. | ||
*/ | ||
public ListCommand parse(String args) { | ||
String trimmedArgs = args.trim(); | ||
if (!trimmedArgs.isEmpty()) { | ||
String[] keywords = trimmedArgs.split("\\s+"); | ||
Set<Performance> performanceSet = | ||
Arrays.stream(keywords).filter(Performance::isValidPerformance) | ||
.map(Performance::type).collect(Collectors.toSet()); | ||
if (!performanceSet.isEmpty()) { | ||
return new ListCommand(new CardPerformanceMatchesPerformancesPredicate(performanceSet)); | ||
} | ||
} | ||
return new ListCommand(); | ||
} | ||
} |
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/deck/CardPerformanceMatchesPerformancesPredicate.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.deck; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Tests that a {@code Card}'s {@code Performance} matches any of the Performances given. | ||
*/ | ||
public class CardPerformanceMatchesPerformancesPredicate implements Predicate<Card> { | ||
private final Set<Performance> performances; | ||
|
||
public CardPerformanceMatchesPerformancesPredicate(Set<Performance> performances) { | ||
this.performances = performances; | ||
} | ||
|
||
/** | ||
* Returns the constituent performances as a list of strings | ||
* | ||
* @return | ||
*/ | ||
public List<String> performancesAsStrings() { | ||
return performances.stream().map(Performance::toString).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public boolean test(Card card) { | ||
return performances.contains(card.getPerformance()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} else if (!(other instanceof CardPerformanceMatchesPerformancesPredicate)) { | ||
return false; | ||
} | ||
CardPerformanceMatchesPerformancesPredicate predicate = (CardPerformanceMatchesPerformancesPredicate) other; | ||
return performances.size() == predicate.performances.size() | ||
&& performances.containsAll(predicate.performances); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/test/java/seedu/address/model/deck/CardPerformanceMatchesPerformancesTest.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,54 @@ | ||
package seedu.address.model.deck; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
|
||
import seedu.address.testutil.CardBuilder; | ||
|
||
public class CardPerformanceMatchesPerformancesTest { | ||
private Set<Performance> hardPerformanceSet = Set.of(Performance.HARD); | ||
private Set<Performance> easyHardPerformancesSet = Set.of(Performance.HARD, Performance.EASY); | ||
private Set<Performance> hardEasyPerformancesSet = Set.of(Performance.EASY, Performance.HARD); | ||
private Set<Performance> allPerformancesSet = Set.of(Performance.values()); | ||
private CardPerformanceMatchesPerformancesPredicate hardPerformancesPredicate = new | ||
CardPerformanceMatchesPerformancesPredicate(hardPerformanceSet); | ||
private CardPerformanceMatchesPerformancesPredicate easyHardPerformancesPredicate = new | ||
CardPerformanceMatchesPerformancesPredicate(easyHardPerformancesSet); | ||
private CardPerformanceMatchesPerformancesPredicate hardEasyPerformancesPredicate = new | ||
CardPerformanceMatchesPerformancesPredicate(hardEasyPerformancesSet); | ||
private CardPerformanceMatchesPerformancesPredicate allPerformanecsPredicate = new | ||
CardPerformanceMatchesPerformancesPredicate(allPerformancesSet); | ||
|
||
private Card easyCard = new CardBuilder().withPerformance(Performance.EASY).build(); | ||
private Card normalCard = new CardBuilder().withPerformance(Performance.NORMAL).build(); | ||
private Card hardCard = new CardBuilder().withPerformance(Performance.HARD).build(); | ||
|
||
@Test | ||
public void equals() { | ||
assertEquals(easyHardPerformancesPredicate, hardEasyPerformancesPredicate); | ||
assertEquals(allPerformanecsPredicate, allPerformanecsPredicate); | ||
assertNotEquals(easyHardPerformancesPredicate, hardPerformancesPredicate); | ||
} | ||
|
||
@Test | ||
public void matchingPerformance_returnsTrue() { | ||
assertTrue(hardPerformancesPredicate.test(hardCard)); | ||
assertTrue(easyHardPerformancesPredicate.test(easyCard)); | ||
assertTrue(easyHardPerformancesPredicate.test(hardCard)); | ||
assertTrue(allPerformanecsPredicate.test(easyCard)); | ||
assertTrue(allPerformanecsPredicate.test(normalCard)); | ||
assertTrue(allPerformanecsPredicate.test(hardCard)); | ||
} | ||
|
||
@Test | ||
public void notMatchingPerformance_returnsFalse() { | ||
assertFalse(hardPerformancesPredicate.test(normalCard)); | ||
assertFalse(easyHardPerformancesPredicate.test(normalCard)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/seedu/address/model/deck/PerformanceTest.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,41 @@ | ||
package seedu.address.model.deck; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
import seedu.address.testutil.Assert; | ||
|
||
public class PerformanceTest { | ||
private static final String INVALID_PERFORMANCE_STRING = "tough"; | ||
private static final Performance VALID_PERFORMANCE = Performance.HARD; | ||
private static final String VALID_PERFORMANCE_STRING = "hard"; | ||
private static final String VALID_PERFORMANCE_STRING_MIXED_CASE = "hArD"; | ||
|
||
@Test | ||
public void type_validPerformanceToString_returnsPerformance() { | ||
Performance expectedPerformance = Performance.HARD; | ||
Performance actualPerformance = Performance.type(expectedPerformance.toString()); | ||
assertEquals(expectedPerformance, actualPerformance); | ||
} | ||
|
||
@Test | ||
public void type_validPerformanceString_returnsPerformance() { | ||
assertEquals(VALID_PERFORMANCE, Performance.type(VALID_PERFORMANCE_STRING)); | ||
} | ||
|
||
@Test | ||
public void type_validPerformanceStringMixedCase_returnsPerformance() { | ||
assertEquals(VALID_PERFORMANCE, Performance.type(VALID_PERFORMANCE_STRING_MIXED_CASE)); | ||
} | ||
|
||
@Test | ||
public void type_invalidPerformanceString_throwsIllegalArgumentException() { | ||
Assert.assertThrows(IllegalArgumentException.class, () -> Performance.type(INVALID_PERFORMANCE_STRING)); | ||
} | ||
|
||
@Test | ||
public void type_nullString_throwsNullPointerException() { | ||
Assert.assertThrows(NullPointerException.class, () -> Performance.type(null)); | ||
} | ||
} |
Oops, something went wrong.