diff --git a/新闻网爬虫/HelpCommand.java b/新闻网爬虫/HelpCommand.java new file mode 100644 index 0000000..3ea161b --- /dev/null +++ b/新闻网爬虫/HelpCommand.java @@ -0,0 +1,30 @@ +package com.cctv.news.command; + +import com.cctv.news.view.OutputView; + +public class HelpCommand implements Command { + private final OutputView view; + + public HelpCommand(OutputView view) { + this.view = view; + } + + @Override + public String getName() { + return "help"; + } + + @Override + public String getHelp() { + return "help - 显示帮助信息"; + } + + @Override + public void execute(String[] args) { + view.showMessage("可用命令:"); + view.showMessage(" help - 显示帮助信息"); + view.showMessage(" list - 显示已抓取的文章列表"); + view.showMessage(" crawl - 爬取央视新闻"); + view.showMessage(" exit - 退出程序"); + } +} diff --git a/新闻网爬虫/ListCommand.java b/新闻网爬虫/ListCommand.java new file mode 100644 index 0000000..07a01cf --- /dev/null +++ b/新闻网爬虫/ListCommand.java @@ -0,0 +1,36 @@ +package com.cctv.news.command; + +import com.cctv.news.view.OutputView; +import java.util.List; + +public class ListCommand implements Command { + private final OutputView view; + private final List articles; + + public ListCommand(OutputView view, List articles) { + this.view = view; + this.articles = articles; + } + + @Override + public String getName() { + return "list"; + } + + @Override + public String getHelp() { + return "list - 显示已抓取的文章列表"; + } + + @Override + public void execute(String[] args) { + if (articles.isEmpty()) { + view.showMessage("目前没有已抓取的文章,请先使用 crawl 命令。"); + } else { + view.showMessage("已抓取的文章列表:"); + for (int i = 0; i < articles.size(); i++) { + view.showArticles((i + 1) + ". " + articles.get(i)); + } + } + } +}