diff --git a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java index e9dc852..c65fa4f 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java @@ -4,21 +4,34 @@ import org.ntlab.citrusserver.entities.Book; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.regex.*; public class PublicBookManager implements IBookListener{ private final BookManager bookManager; + private HashSet publicBooks = new HashSet<>(); + public PublicBookManager(BookManager bookManager) { this.bookManager = bookManager; + this.bookManager.addListener(this); } public ArrayList getAllPublicBooks(){ - return null; + return new ArrayList<>(this.publicBooks); } public ArrayList searchBooksByTitle(String title){ - return null; + ArrayList books = new ArrayList<>(); + var ptn = Pattern.compile(title); + for(Book book : this.publicBooks){ + if(ptn.matcher(book.getTitle()).find()){ + books.add(book); + } + } + return books; } public ArrayList searchBooksByTitle(String title, int sortBy){ @@ -26,7 +39,13 @@ } public ArrayList searchBooksByAccount(String accountId){ - return null; + ArrayList books = new ArrayList<>(); + for(Book book : this.publicBooks){ + if(book.getAccountId().equals(accountId)){ + books.add(book); + } + } + return books; } public ArrayList searchBooksByAccount(String accountId, int sortBy){ @@ -34,13 +53,21 @@ } public ArrayList searchBooksByTitleAndAccount(String title, String accountId){ - return null; + ArrayList books = searchBooksByTitle(title); + for(Book book : books){ + if(book.getAccountId().equals(accountId)){ + books.add(book); + } + } + return books; } @Override public void bookChanged(Account account, Book book) { + if(book.getPublicity()){ + this.publicBooks.add(book); + } else{ + this.publicBooks.remove(book); + } } - - - }