forked from AY2324S2-CS2103T-F12-2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 AY2324S2-CS2103T-F12-2#57 from bennyLCK/branch-b-S…
…ortArticlesByDate Add support for parsing sort article command
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/logic/commands/articlecommands/SortArticleCommand.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,49 @@ | ||
package seedu.address.logic.commands.articlecommands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.logic.parser.SortArticleCommandParser; | ||
import seedu.address.model.Model; | ||
|
||
/** | ||
* Sorts all articles in the article book by an attribute of articles. | ||
*/ | ||
public class SortArticleCommand extends ArticleCommand { | ||
|
||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String COMMAND_PREFIX = "-a"; | ||
|
||
public static final String MESSAGE_SUCCESS = "sorted all articles by date"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + " " + COMMAND_PREFIX | ||
+ ": sorts articles according to publication date and displays the sorted article list.\n" | ||
+ "Parameters: D/ (corresponds to prefix for article's publication date attribute)\n" | ||
+ "Example: " + COMMAND_WORD + " " + COMMAND_PREFIX + " D/"; | ||
|
||
private final String prefix; | ||
|
||
/** | ||
* @param prefix referring to an attribute of articles to sort by | ||
*/ | ||
public SortArticleCommand(String prefix) { | ||
requireNonNull(prefix); | ||
this.prefix = prefix; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (!SortArticleCommandParser.isAllowedPrefix(prefix)) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_SORTING_PREFIX); | ||
} | ||
|
||
// model.sortArticleBook(prefix); | ||
|
||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
} |
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/logic/parser/SortArticleCommandParser.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.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PUBLICATION_DATE; | ||
|
||
import java.util.ArrayList; | ||
|
||
import seedu.address.logic.commands.articlecommands.SortArticleCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new SortArticleCommand object | ||
*/ | ||
public class SortArticleCommandParser implements Parser<SortArticleCommand> { | ||
|
||
private static final ArrayList<String> AllowedPrefixes = new ArrayList<>() { | ||
{ | ||
add(PREFIX_PUBLICATION_DATE.getPrefix()); | ||
} | ||
}; | ||
|
||
/** | ||
* Checks if the given prefix is allowed in choosing an attribute for sorting. | ||
*/ | ||
public static boolean isAllowedPrefix(String prefix) { | ||
return AllowedPrefixes.contains(prefix); | ||
} | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SortArticleCommand | ||
* and returns an SortArticleCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SortArticleCommand parse(String args) throws ParseException { | ||
String prefix = args.trim(); | ||
if (prefix.isEmpty() || !PREFIX_PUBLICATION_DATE.getPrefix().equals(prefix)) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortArticleCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
return new SortArticleCommand(prefix); | ||
} | ||
} |