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); + } +}