package com.example.datacollect.controller; import com.example.datacollect.command.Command; import com.example.datacollect.command.ClearCommand; import com.example.datacollect.command.CrawlCommand; import com.example.datacollect.command.ExitCommand; import com.example.datacollect.command.HelpCommand; import com.example.datacollect.command.HistoryCommand; import com.example.datacollect.command.ListCommand; import com.example.datacollect.model.Article; import com.example.datacollect.view.ConsoleView; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ArrayList; public class CrawlerController { private final Map commands = new HashMap<>(); private final ConsoleView view; private final List
articles; private final List commandHistory = new ArrayList<>(); public CrawlerController(ConsoleView view, List
articles) { this.view = view; this.articles = articles; register(new HelpCommand(view)); register(new ListCommand(view)); register(new CrawlCommand(view)); register(new ExitCommand(view)); register(new ClearCommand(view)); register(new HistoryCommand(view, commandHistory)); } 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; } commandHistory.add(text); command.execute(args, articles); } }