package com.example.datacollect.controller; import com.example.datacollect.command.*; import com.example.datacollect.model.Article; import com.example.datacollect.model.DataService; import com.example.datacollect.view.ConsoleView; import com.example.datacollect.service.HistoryService; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CrawlerController { private final Map commands = new HashMap<>(); private final ConsoleView view; private final DataService dataService; private final HistoryService historyService; public CrawlerController(ConsoleView view, DataService dataService,HistoryService historyService) { this.view = view; this.dataService =dataService; this.historyService=historyService; register(new HelpCommand(view)); register(new ListCommand(view)); register(new CrawlCommand(view)); register(new ExitCommand(view)); //注册新命令 register(new SearchCommand(view)); register(new DeleteCommand(view)); register(new HistoryCommand(view,historyService)); } private void register(Command command) { commands.put(command.getName(), command); } public void handle(String input) { String text = input == null ? "" : input.trim(); if (text.isEmpty()) { return; } String[] args = text.split("\\s+"); String cmdName = args[0].toLowerCase(); Command command = commands.get(cmdName); if (command == null) { view.printError("Unknown command: " + cmdName); return; } command.execute(args, dataService); historyService.add(text); } }