diff --git a/w10/ArticleRepository.java b/w10/ArticleRepository.java new file mode 100644 index 0000000..fcf9ab0 --- /dev/null +++ b/w10/ArticleRepository.java @@ -0,0 +1,57 @@ +package com.rental.crawler.repository; + +import com.rental.crawler.model.Book; +import java.util.ArrayList; +import java.util.List; + +public class ArticleRepository { + private List articles; + + public ArticleRepository() { + this.articles = new ArrayList<>(); + } + + public ArticleRepository(List articles) { + this.articles = articles != null ? new ArrayList<>(articles) : new ArrayList<>(); + } + + public void add(Book article) { + if (article != null) { + this.articles.add(article); + } + } + + public void addAll(List newArticles) { + if (newArticles == null) { + return; + } + for (Book article : newArticles) { + if (article != null) { + this.articles.add(article); + } + } + } + + public List findAll() { + return new ArrayList<>(this.articles); + } + + public Book findByIndex(int index) { + if (index >= 0 && index < this.articles.size()) { + return this.articles.get(index); + } + return null; + } + + public int size() { + return this.articles.size(); + } + + public void clear() { + this.articles.clear(); + } + + public boolean isEmpty() { + return this.articles.isEmpty(); + } +} \ No newline at end of file diff --git a/w10/Test.java b/w10/Test.java new file mode 100644 index 0000000..1193577 --- /dev/null +++ b/w10/Test.java @@ -0,0 +1,56 @@ +package com.rental.crawler; + +import com.rental.crawler.command.AnalyzeCommand; +import com.rental.crawler.model.Book; +import com.rental.crawler.repository.ArticleRepository; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + public static void main(String[] args) { + System.out.println("========== 测试 ArticleRepository addAll =========="); + ArticleRepository repository = new ArticleRepository(); + + List books1 = new ArrayList<>(); + books1.add(createBook("书1", "作者1", "出版社A", 8.5)); + books1.add(createBook("书2", "作者2", "出版社B", 7.5)); + books1.add(null); + repository.addAll(books1); + System.out.println("添加book1后的数量: " + repository.size()); + + List books2 = null; + repository.addAll(books2); + System.out.println("添加null后的数量: " + repository.size()); + + repository.add(null); + System.out.println("添加单个null后的数量: " + repository.size()); + + List books3 = new ArrayList<>(); + books3.add(null); + books3.add(createBook("书3", "作者3", "出版社C", 9.0)); + repository.addAll(books3); + System.out.println("添加包含null的列表后的数量: " + repository.size()); + + System.out.println("\n========== 测试 AnalyzeCommand =========="); + List testBooks = new ArrayList<>(); + testBooks.add(createBook("红楼梦", "曹雪芹", "人民文学出版社", 9.0)); + testBooks.add(createBook("水浒传", "施耐庵", "上海译文出版社", 8.5)); + testBooks.add(createBook("书3", "作者3", "未知出版社", 6.5)); + testBooks.add(null); + + AnalyzeCommand analyzeCommand = new AnalyzeCommand(); + analyzeCommand.execute(testBooks); + + System.out.println("\n测试完成!"); + } + + private static Book createBook(String title, String author, String publisher, double rating) { + Book book = new Book(); + book.setTitle(title); + book.setAuthors(author); + book.setPublisher(publisher); + book.setRating(rating); + return book; + } +} \ No newline at end of file