forked from nus-cs2103-AY2324S2/tp
-
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.
Merge branch 'master' into branch-AllRefactors
- Loading branch information
Showing
23 changed files
with
545 additions
and
3 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
70 changes: 70 additions & 0 deletions
70
src/main/java/seedu/address/logic/commands/SortCommand.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,70 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.logic.parser.SortCommandParser; | ||
import seedu.address.model.Model; | ||
|
||
/** | ||
* Sorts all persons in the address book by an attribute of persons. | ||
*/ | ||
public class SortCommand extends PersonCommand { | ||
|
||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String MESSAGE_SUCCESS = "sorted all persons by name"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": sorts people according to an attribute and displays the sorted person list.\n" | ||
+ "Parameters: PREFIX (corresponds to each person's attribute e.g. n/ for Name)\n" | ||
+ "Example: " + COMMAND_WORD + " n/"; | ||
|
||
private final String prefix; | ||
|
||
/** | ||
* @param prefix referring to an attribute of persons to sort by | ||
*/ | ||
public SortCommand(String prefix) { | ||
requireNonNull(prefix); | ||
this.prefix = prefix; | ||
} | ||
|
||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (!SortCommandParser.isAllowedPrefix(prefix)) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_SORTING_PREFIX); | ||
} | ||
|
||
model.sortAddressBook(prefix); | ||
|
||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof SortCommand)) { | ||
return false; | ||
} | ||
|
||
SortCommand otherSortCommand = (SortCommand) other; | ||
return prefix.equals(otherSortCommand.prefix); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("prefix", prefix) | ||
.toString(); | ||
} | ||
} |
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
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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/seedu/address/logic/parser/SortCommandParser.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_NAME; | ||
|
||
import java.util.ArrayList; | ||
|
||
import seedu.address.logic.commands.SortCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new SortCommand object | ||
*/ | ||
public class SortCommandParser implements Parser<SortCommand> { | ||
|
||
private static final ArrayList<String> AllowedPrefixes = new ArrayList<>() { | ||
{ | ||
add(PREFIX_NAME.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 SortCommand | ||
* and returns an SortCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SortCommand parse(String args) throws ParseException { | ||
String prefix = args.trim(); | ||
if (prefix.isEmpty() || !PREFIX_NAME.getPrefix().equals(prefix)) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
return new SortCommand(prefix); | ||
} | ||
} |
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
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
Oops, something went wrong.