Добавил команду execute_script

This commit is contained in:
Ivan Zinchenko 2024-04-02 04:04:37 +03:00
parent 8746322d07
commit fc52f6ec8e
Signed by: zinch
GPG Key ID: 6D45AA2C8FD6A37E
8 changed files with 108 additions and 6 deletions

4
.gitignore vendored
View File

@ -31,4 +31,6 @@ bin/
.DS_Store
### Build ###
target
target
*.scr

View File

@ -1,4 +0,0 @@
### Этапы работы программы
1. Проверка файла с базой данных
2. Пользовательский ввод

View File

@ -37,6 +37,7 @@ public class CommandManager {
registerCommand(new AddIfMin());
registerCommand(new RemoveLower());
registerCommand(new Save());
registerCommand(new ExecuteScript());
registerCommand(new Help());
}

View File

@ -0,0 +1,58 @@
package me.zinch.commands;
import me.zinch.console.Console;
import me.zinch.files.ScriptLoader;
import me.zinch.wrapper.ProductCollection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class ExecuteScript extends Command {
private static final List<String> executeStack = new ArrayList<>();
private void addFileToStack(String filePath) {
executeStack.add(filePath);
}
private void clearStack() {
executeStack.clear();
}
private boolean checkFileInStack(String filePath) {
return executeStack.contains(filePath);
}
public ExecuteScript() {
super("execute_script file_name", "считать и исполнить скрипт из указанного файла. В скрипте содержатся команды в таком же виде, в котором их вводит пользователь в интерактивном режиме", Pattern.compile("^execute_script .+"));
}
@Override
public void action(ProductCollection productCollection) {
try {
var scriptFile = Console.getLastCommand().split(" ")[1];
addFileToStack(scriptFile);
var commandList = ScriptLoader.loadList(scriptFile);
for (var input: commandList) {
input = input.trim();
var command = CommandManager.getCommandByInput(input);
if (command.isPresent()) {
Console.appendHistory(input);
if (command.get() instanceof ExecuteScript) {
var nextFile = input.split(" ")[0];
if (checkFileInStack(nextFile)) continue;
addFileToStack(nextFile);
}
command.get().action(productCollection);
} else {
new UnknownCommand().action(productCollection);
}
}
clearStack();
System.out.format("Скрипт %s завершил работу.%n", scriptFile);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

View File

@ -0,0 +1,14 @@
package me.zinch.commands;
import me.zinch.wrapper.ProductCollection;
public class UnknownCommand extends Command {
public UnknownCommand() {
super("", "");
}
@Override
public void action(ProductCollection productCollection) {
System.out.println("Команда не распознана.");
}
}

View File

@ -23,7 +23,7 @@ public class Console {
private static boolean isRunning = true;
private static final List<String> history = new ArrayList<>();
private static void appendHistory(String input) {
public static void appendHistory(String input) {
history.add(input);
}

View File

@ -48,6 +48,7 @@ public class DbController {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path));
Products collection = xmlMapper.readValue(bufferedInputStream, Products.class);
bufferedInputStream.close();
return collection.getProducts();
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Не удалось найти файл " + path);
@ -62,6 +63,7 @@ public class DbController {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path));
xmlMapper.writeValue(outputStreamWriter, new Products(list));
outputStreamWriter.close();
} catch (FileNotFoundException e) {
throw new FileNotFoundException(e.getMessage());
} catch (IOException e) {

View File

@ -0,0 +1,29 @@
package me.zinch.files;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ScriptLoader {
public static List<String> loadList(String path) throws IOException {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path));
Scanner scanner = new Scanner(bufferedInputStream);
List<String> list = new ArrayList<>();
while (scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
bufferedInputStream.close();
scanner.close();
return list;
} catch (FileNotFoundException e) {
throw new FileNotFoundException(String.format("Файл %s не найден.", path));
} catch (IOException e) {
throw new IOException("Произошла неожиданная ошибка во время работы с файлом!");
}
}
}