This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

  1. /*
  2. * SD2x Homework #8
  3. * This class represents the Logic Tier in the three-tier architecture.
  4. * Implement the appropriate methods for this tier below.
  5. */
  6. import java.util.List;
  7. import java.util.LinkedList;
  8. import java.util.Set;
  9. public class LogicTier {
  10. private DataTier dataTier; // link to the Data Tier
  11. public LogicTier(DataTier dataTier) {
  12. this.dataTier = dataTier;
  13. }
  14. private boolean includes(String author, String search){
  15. for(int i=0; i <= author.length() - search.length(); i++){
  16. if(author.substring(i, i + search.length()).equals(search)) return true;
  17. }
  18. return false;
  19. }
  20. public List<String> findBookTitlesByAuthor(String author){
  21. Set<Book> books = dataTier.getAllBooks();
  22. List<String> matchingTitles = new LinkedList<String>();
  23. for(Book b : books){
  24. if(includes(b.getAuthor(), author)) matchingTitles.add(b.getTitle());
  25. }
  26. return matchingTitles;
  27. }
  28. public long findNumberOfBooksInYear(int year){
  29. int bookNum = 0;
  30. Set<Book> books = dataTier.getAllBooks();
  31. for(Book b : books){
  32. if(b.getPublicationYear() == year) bookNum += 1;
  33. }
  34. return bookNum;
  35. }
  36. }