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.
56 lines
1.7 KiB
56 lines
1.7 KiB
package controller;
|
|
|
|
import command.Command;
|
|
import command.CrawlCommand;
|
|
import command.ListCommand;
|
|
import command.HelpCommand;
|
|
import command.ExitCommand;
|
|
import command.PlatformCommand;
|
|
import view.ConsoleView;
|
|
import repository.PaperRepository;
|
|
import strategy.StrategyFactory;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class CrawlerController {
|
|
private final ConsoleView view;
|
|
private final PaperRepository repository;
|
|
private final Map<String, Command> commands = new HashMap<>();
|
|
|
|
public CrawlerController(ConsoleView view, PaperRepository repository, StrategyFactory strategyFactory) {
|
|
this.view = view;
|
|
this.repository = repository;
|
|
|
|
register(new CrawlCommand(view, strategyFactory));
|
|
register(new ListCommand(view));
|
|
register(new PlatformCommand(view, strategyFactory));
|
|
register(new ExitCommand(view));
|
|
register(new HelpCommand(view, new ArrayList<>(commands.values())));
|
|
}
|
|
|
|
private void register(Command command) {
|
|
commands.put(command.getName(), command);
|
|
}
|
|
|
|
public void run() {
|
|
view.displayWelcome();
|
|
|
|
while (true) {
|
|
String input = view.getInput();
|
|
if (input.isEmpty()) continue;
|
|
|
|
String[] parts = input.split("\\s+");
|
|
String commandName = parts[0].toLowerCase();
|
|
|
|
if (!commands.containsKey(commandName)) {
|
|
view.showError("未知命令,请输入 help 查看可用命令");
|
|
continue;
|
|
}
|
|
|
|
Command command = commands.get(commandName);
|
|
command.execute(parts, repository);
|
|
}
|
|
}
|
|
}
|
|
|