4 changed files with 74 additions and 0 deletions
@ -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<Article> articles) { |
|||
view.printSuccess("Bye!"); |
|||
System.exit(0); |
|||
} |
|||
} |
|||
@ -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<Article> articles) { |
|||
view.printInfo("可用命令:"); |
|||
view.printInfo(" crawl <url> 抓取指定 URL 的文章 (别名: c)"); |
|||
view.printInfo(" list 列出所有已抓取的文章"); |
|||
view.printInfo(" history 显示历史命令记录"); |
|||
view.printInfo(" help 显示帮助信息"); |
|||
view.printInfo(" exit 退出程序"); |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 作业2:HistoryCommand |
|||
* 记录并展示用户输入过的所有命令(用 List<String> 存储) |
|||
*/ |
|||
public class HistoryCommand implements Command { |
|||
private final ConsoleView view; |
|||
private final List<String> history; // 由 Controller 注入,共享同一个列表
|
|||
|
|||
public HistoryCommand(ConsoleView v, List<String> history) { |
|||
this.view = v; |
|||
this.history = history; |
|||
} |
|||
|
|||
@Override public String getName() { return "history"; } |
|||
|
|||
@Override |
|||
public void execute(String[] args, List<Article> 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))); |
|||
} |
|||
} |
|||
} |
|||
@ -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<Article> articles) { |
|||
view.display(articles); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue