Skip to content

Commit ba23fa9

Browse files
authored
Merge pull request AY2324S2-CS2103T-F12-2#57 from bennyLCK/branch-b-SortArticlesByDate
Add support for parsing sort article command
2 parents d401f41 + cdd93c6 commit ba23fa9

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package seedu.address.logic.commands.articlecommands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import seedu.address.logic.Messages;
6+
import seedu.address.logic.commands.CommandResult;
7+
import seedu.address.logic.commands.exceptions.CommandException;
8+
import seedu.address.logic.parser.SortArticleCommandParser;
9+
import seedu.address.model.Model;
10+
11+
/**
12+
* Sorts all articles in the article book by an attribute of articles.
13+
*/
14+
public class SortArticleCommand extends ArticleCommand {
15+
16+
public static final String COMMAND_WORD = "sort";
17+
18+
public static final String COMMAND_PREFIX = "-a";
19+
20+
public static final String MESSAGE_SUCCESS = "sorted all articles by date";
21+
22+
public static final String MESSAGE_USAGE = COMMAND_WORD + " " + COMMAND_PREFIX
23+
+ ": sorts articles according to publication date and displays the sorted article list.\n"
24+
+ "Parameters: D/ (corresponds to prefix for article's publication date attribute)\n"
25+
+ "Example: " + COMMAND_WORD + " " + COMMAND_PREFIX + " D/";
26+
27+
private final String prefix;
28+
29+
/**
30+
* @param prefix referring to an attribute of articles to sort by
31+
*/
32+
public SortArticleCommand(String prefix) {
33+
requireNonNull(prefix);
34+
this.prefix = prefix;
35+
}
36+
37+
@Override
38+
public CommandResult execute(Model model) throws CommandException {
39+
requireNonNull(model);
40+
41+
if (!SortArticleCommandParser.isAllowedPrefix(prefix)) {
42+
throw new CommandException(Messages.MESSAGE_INVALID_SORTING_PREFIX);
43+
}
44+
45+
// model.sortArticleBook(prefix);
46+
47+
return new CommandResult(MESSAGE_SUCCESS);
48+
}
49+
}

src/main/java/seedu/address/logic/parser/ArticleBookParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import seedu.address.logic.commands.articlecommands.EditArticleCommand;
1616
import seedu.address.logic.commands.articlecommands.FindArticleCommand;
1717
import seedu.address.logic.commands.articlecommands.ListArticleCommand;
18+
import seedu.address.logic.commands.articlecommands.SortArticleCommand;
1819
import seedu.address.logic.parser.exceptions.ParseException;
1920

2021
/**
@@ -66,6 +67,9 @@ public static Command parseCommand(String userInput) throws ParseException {
6667
case ListArticleCommand.COMMAND_WORD:
6768
return new ListArticleCommand();
6869

70+
case SortArticleCommand.COMMAND_WORD:
71+
return new SortArticleCommandParser().parse(arguments);
72+
6973
default:
7074
logger.finer("This user input caused a ParseException: " + userInput);
7175
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_PUBLICATION_DATE;
5+
6+
import java.util.ArrayList;
7+
8+
import seedu.address.logic.commands.articlecommands.SortArticleCommand;
9+
import seedu.address.logic.parser.exceptions.ParseException;
10+
11+
/**
12+
* Parses input arguments and creates a new SortArticleCommand object
13+
*/
14+
public class SortArticleCommandParser implements Parser<SortArticleCommand> {
15+
16+
private static final ArrayList<String> AllowedPrefixes = new ArrayList<>() {
17+
{
18+
add(PREFIX_PUBLICATION_DATE.getPrefix());
19+
}
20+
};
21+
22+
/**
23+
* Checks if the given prefix is allowed in choosing an attribute for sorting.
24+
*/
25+
public static boolean isAllowedPrefix(String prefix) {
26+
return AllowedPrefixes.contains(prefix);
27+
}
28+
29+
/**
30+
* Parses the given {@code String} of arguments in the context of the SortArticleCommand
31+
* and returns an SortArticleCommand object for execution.
32+
* @throws ParseException if the user input does not conform the expected format
33+
*/
34+
public SortArticleCommand parse(String args) throws ParseException {
35+
String prefix = args.trim();
36+
if (prefix.isEmpty() || !PREFIX_PUBLICATION_DATE.getPrefix().equals(prefix)) {
37+
throw new ParseException(
38+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortArticleCommand.MESSAGE_USAGE));
39+
}
40+
41+
return new SortArticleCommand(prefix);
42+
}
43+
}

0 commit comments

Comments
 (0)