Skip to content

Commit

Permalink
Merge branch 'master' into branch-b-ArticleTestUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyLCK committed Mar 27, 2024
2 parents ed59c7a + 585ed77 commit 8d366dd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(Messages.MESSAGE_INVALID_SORTING_PREFIX);
}

// model.sortArticleBook(prefix);
model.sortArticleBook(prefix);

return new CommandResult(MESSAGE_SUCCESS);
}
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
6 changes: 5 additions & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,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);

}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ public void setArticle(Article target, Article editedArticle) {
articleBook.setArticle(target, editedArticle);
}

@Override
public void sortArticleBook(String prefix) {
articleBook.sortArticleBook(prefix);
}

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

/**
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
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/util/SampleArticleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ public static ReadOnlyArticleBook getSampleArticleBook() {
}
return sampleAb;
}

// Run this to generate sample article data stored in articlebook.json before running PressPlaanner.

/*public static void main(String[] args) {
ReadOnlyArticleBook initialArticleData;
initialArticleData = SampleArticleDataUtil.getSampleArticleBook();
Storage storage = new StorageManager(new JsonAddressBookStorage(Path.of("data/addressbook.json")),
new JsonUserPrefsStorage(Path.of("data/userprefs.json")),
new JsonArticleBookStorage(Path.of("data/articlebook.json")));
try {
storage.saveArticleBook(initialArticleData);
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ public boolean hasArticle(Article article) {
public void setArticle(Article target, Article editedArticle) {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortArticleBook(String prefix) {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down

0 comments on commit 8d366dd

Please sign in to comment.