Skip to content

Commit

Permalink
Merge branch 'master' into branch-AllRefactors
Browse files Browse the repository at this point in the history
  • Loading branch information
H4mes authored Mar 27, 2024
2 parents dbe9002 + 585ed77 commit 44c843d
Show file tree
Hide file tree
Showing 23 changed files with 545 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Messages {
public static final String MESSAGE_ARTICLES_LISTED_OVERVIEW = "%1$d articles listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_INVALID_SORTING_PREFIX = "Invalid prefix given for sorting";

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -75,6 +76,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case SortCommand.COMMAND_WORD:
return new SortCommandParser().parse(arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.logic.commands.articlecommands.EditArticleCommand;
import seedu.address.logic.commands.articlecommands.FindArticleCommand;
import seedu.address.logic.commands.articlecommands.ListArticleCommand;
import seedu.address.logic.commands.articlecommands.SortArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

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

case SortArticleCommand.COMMAND_WORD:
return new SortArticleCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
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 src/main/java/seedu/address/logic/parser/SortCommandParser.java
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);
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public void removePerson(Person key) {
persons.remove(key);
}

/**
* Sorts the persons in the address book by the attribute derived from a given prefix.
*/
public void sortAddressBook(String prefix) {
persons.sortPersons(prefix);
}

//// util methods

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/ArticleBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public void removeArticle(Article key) {
articles.remove(key);
}

/**
* Sorts the article book by the attribute represented by the given prefix.
*/
public void sortArticleBook(String prefix) {
articles.sortArticles(prefix);
}

//// util methods

@Override
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public interface Model {
*/
void setPerson(Person target, Person editedPerson);

/**
* Sorts the address book by the attribute represented by the given prefix.
*/
void sortAddressBook(String prefix);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();

Expand Down Expand Up @@ -127,10 +132,14 @@ public interface Model {
*/
void deleteArticle(Article target);

/**
* Sorts the article book by the attribute represented by the given prefix.
*/
void sortArticleBook(String prefix);

/**
* Updates the filter of the filtered article list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredArticleList(Predicate<Article> predicate);

}
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public void setPerson(Person target, Person editedPerson) {
addressBook.setPerson(target, editedPerson);
}

@Override
public void sortAddressBook(String prefix) {
addressBook.sortAddressBook(prefix);
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand Down Expand Up @@ -169,7 +174,12 @@ public void setArticle(Article target, Article editedArticle) {
articleBook.setArticle(target, editedArticle);
}

//=========== Filtered Person List Accessors =============================================================
@Override
public void sortArticleBook(String prefix) {
articleBook.sortArticleBook(prefix);
}

//=========== Filtered Article List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Article} backed by the internal list of
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/article/UniqueArticleList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PUBLICATION_DATE;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -100,6 +102,19 @@ public void setArticles(List<Article> articles) {
internalList.setAll(articles);
}

/**
* Sorts the list of articles by the attribute represented by the given prefix.
*/
public void sortArticles(String prefix) {
requireNonNull(prefix);
if (PREFIX_PUBLICATION_DATE.getPrefix().equals(prefix)) {
// Sort by publication date and display most recent articles first.
internalList.sort(Comparator.comparing(Article::getPublicationDate, Comparator.reverseOrder()));
} else {
throw new IllegalArgumentException("Invalid prefix supplied.");
}
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class Name {
public class Name implements Comparable<Name> {

public static final String MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
Expand Down Expand Up @@ -44,6 +44,11 @@ public String toString() {
return fullName;
}

@Override
public int compareTo(Name other) {
return this.fullName.compareTo(other.fullName);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Loading

0 comments on commit 44c843d

Please sign in to comment.