From c38adb048dd3b5f3f14da30b71025680ae40d8c6 Mon Sep 17 00:00:00 2001 From: Chengwuyi <3394813085@qq.com> Date: Sat, 30 May 2026 22:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w9'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w9/ExitCommand.java | 14 ++++++++++++++ w9/HelpCommand.java | 18 ++++++++++++++++++ w9/HistoryCommand.java | 29 +++++++++++++++++++++++++++++ w9/ListCommand.java | 13 +++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 w9/ExitCommand.java create mode 100644 w9/HelpCommand.java create mode 100644 w9/HistoryCommand.java create mode 100644 w9/ListCommand.java diff --git a/w9/ExitCommand.java b/w9/ExitCommand.java new file mode 100644 index 0000000..544d801 --- /dev/null +++ b/w9/ExitCommand.java @@ -0,0 +1,14 @@ +import java.util.List; + +public class ExitCommand implements Command { + private final ConsoleView view; + public ExitCommand(ConsoleView v) { this.view = v; } + + @Override public String getName() { return "exit"; } + + @Override + public void execute(String[] args, List
articles) { + view.printSuccess("Bye!"); + System.exit(0); + } +} diff --git a/w9/HelpCommand.java b/w9/HelpCommand.java new file mode 100644 index 0000000..e053fa9 --- /dev/null +++ b/w9/HelpCommand.java @@ -0,0 +1,18 @@ +import java.util.List; + +public class HelpCommand implements Command { + private final ConsoleView view; + public HelpCommand(ConsoleView v) { this.view = v; } + + @Override public String getName() { return "help"; } + + @Override + public void execute(String[] args, List
articles) { + view.printInfo("可用命令:"); + view.printInfo(" crawl 抓取指定 URL 的文章 (别名: c)"); + view.printInfo(" list 列出所有已抓取的文章"); + view.printInfo(" history 显示历史命令记录"); + view.printInfo(" help 显示帮助信息"); + view.printInfo(" exit 退出程序"); + } +} diff --git a/w9/HistoryCommand.java b/w9/HistoryCommand.java new file mode 100644 index 0000000..a4ebd93 --- /dev/null +++ b/w9/HistoryCommand.java @@ -0,0 +1,29 @@ +import java.util.List; + +/** + * 作业2:HistoryCommand + * 记录并展示用户输入过的所有命令(用 List 存储) + */ +public class HistoryCommand implements Command { + private final ConsoleView view; + private final List history; // 由 Controller 注入,共享同一个列表 + + public HistoryCommand(ConsoleView v, List history) { + this.view = v; + this.history = history; + } + + @Override public String getName() { return "history"; } + + @Override + public void execute(String[] args, List
articles) { + if (history.isEmpty()) { + view.printInfo("暂无历史记录。"); + return; + } + view.printBold("── 历史命令 (" + history.size() + " 条) ──"); + for (int i = 0; i < history.size(); i++) { + view.printInfo(String.format("%3d %s", i + 1, history.get(i))); + } + } +} diff --git a/w9/ListCommand.java b/w9/ListCommand.java new file mode 100644 index 0000000..8adcc17 --- /dev/null +++ b/w9/ListCommand.java @@ -0,0 +1,13 @@ +import java.util.List; + +public class ListCommand implements Command { + private final ConsoleView view; + public ListCommand(ConsoleView v) { this.view = v; } + + @Override public String getName() { return "list"; } + + @Override + public void execute(String[] args, List
articles) { + view.display(articles); + } +}