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.
63 lines
2.1 KiB
63 lines
2.1 KiB
package com.crawler;
|
|
|
|
import com.crawler.command.*;
|
|
import com.crawler.controller.CrawlerController;
|
|
import com.crawler.repository.ArticleRepository;
|
|
import com.crawler.repository.InMemoryArticleRepository;
|
|
import com.crawler.view.ConsoleView;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
public class App {
|
|
private final Map<String, Command> commands = new HashMap<>();
|
|
private final ConsoleView view;
|
|
private final AtomicBoolean running = new AtomicBoolean(true);
|
|
|
|
public App() {
|
|
view = new ConsoleView();
|
|
ArticleRepository repository = new InMemoryArticleRepository();
|
|
CrawlerController controller = new CrawlerController(repository, view);
|
|
|
|
commands.put("crawl", new CrawlCommand(controller, view));
|
|
commands.put("list", new ListCommand(controller));
|
|
commands.put("save", new SaveCommand(controller));
|
|
commands.put("load", new LoadCommand(controller));
|
|
commands.put("help", new HelpCommand(view));
|
|
commands.put("exit", new ExitCommand(view, () -> running.set(false)));
|
|
}
|
|
|
|
public void run() {
|
|
view.displayWelcome();
|
|
view.displayHelp();
|
|
|
|
while (running.get()) {
|
|
try {
|
|
String input = view.readInput();
|
|
if (input.isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
String[] parts = input.split("\\s+", 3);
|
|
String commandName = parts[0].toLowerCase();
|
|
String[] args = parts.length > 1 ? java.util.Arrays.copyOfRange(parts, 1, parts.length) : new String[0];
|
|
|
|
Command command = commands.get(commandName);
|
|
if (command != null) {
|
|
command.execute(args);
|
|
} else {
|
|
view.displayError("Unknown command: " + commandName);
|
|
view.displayInfo("Type 'help' for available commands");
|
|
}
|
|
} catch (Exception e) {
|
|
view.displayError("Error: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
App app = new App();
|
|
app.run();
|
|
}
|
|
}
|
|
|