You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.5 KiB
57 lines
2.5 KiB
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
// ─────────────────────────────────────────────
|
|
// Controller
|
|
// ─────────────────────────────────────────────
|
|
class CrawlerController {
|
|
private final ConsoleView view;
|
|
private final ArticleRepository repo;
|
|
private final Map<String, Command> commands = new HashMap<>();
|
|
private final List<String> history = new ArrayList<>();
|
|
|
|
CrawlerController(ConsoleView view, ArticleRepository repo, StrategySelector selector) {
|
|
this.view = view;
|
|
this.repo = repo;
|
|
|
|
CrawlCommand crawl = new CrawlCommand(view, repo, selector);
|
|
reg(crawl.getName(), crawl);
|
|
reg(crawl.getAlias(), crawl);
|
|
reg(new ListCommand(view, repo));
|
|
reg(new AnalyzeCommand(view, repo, selector));
|
|
reg(new HistoryCommand(view, history));
|
|
reg(new HelpCommand(view));
|
|
reg(new ExitCommand(view));
|
|
}
|
|
|
|
private void reg(Command cmd) { commands.put(cmd.getName(), cmd); }
|
|
private void reg(String key, Command cmd) { commands.put(key, cmd); }
|
|
|
|
public void handle(String input) {
|
|
if (input == null || input.isBlank()) return;
|
|
history.add(input);
|
|
String[] parts = input.trim().split("\\s+");
|
|
Command cmd = commands.get(parts[0].toLowerCase());
|
|
if (cmd == null) view.printError("未知命令:" + parts[0] + "(输入 help)");
|
|
else cmd.execute(parts, repo.getAll());
|
|
}
|
|
}
|
|
|
|
// ─────────────────────────────────────────────
|
|
// Main 入口
|
|
// ─────────────────────────────────────────────
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
ConsoleView view = new ConsoleView();
|
|
ArticleRepository repo = new ArticleRepository();
|
|
StrategySelector selector = new StrategySelector();
|
|
CrawlerController ctrl = new CrawlerController(view, repo, selector);
|
|
|
|
view.printSuccess("Welcome to CLI Crawler W10!");
|
|
view.printInfo("输入 help 查看命令。");
|
|
|
|
while (true) ctrl.handle(view.readLine());
|
|
}
|
|
}
|
|
|