Добавил команду execute_script
This commit is contained in:
parent
8746322d07
commit
fc52f6ec8e
2
.gitignore
vendored
2
.gitignore
vendored
@ -32,3 +32,5 @@ bin/
|
||||
|
||||
### Build ###
|
||||
target
|
||||
|
||||
*.scr
|
||||
4
Notes.md
4
Notes.md
@ -1,4 +0,0 @@
|
||||
### Этапы работы программы
|
||||
|
||||
1. Проверка файла с базой данных
|
||||
2. Пользовательский ввод
|
||||
@ -37,6 +37,7 @@ public class CommandManager {
|
||||
registerCommand(new AddIfMin());
|
||||
registerCommand(new RemoveLower());
|
||||
registerCommand(new Save());
|
||||
registerCommand(new ExecuteScript());
|
||||
|
||||
registerCommand(new Help());
|
||||
}
|
||||
|
||||
58
src/main/java/me/zinch/commands/ExecuteScript.java
Normal file
58
src/main/java/me/zinch/commands/ExecuteScript.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/main/java/me/zinch/commands/UnknownCommand.java
Normal file
14
src/main/java/me/zinch/commands/UnknownCommand.java
Normal 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("Команда не распознана.");
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
29
src/main/java/me/zinch/files/ScriptLoader.java
Normal file
29
src/main/java/me/zinch/files/ScriptLoader.java
Normal 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("Произошла неожиданная ошибка во время работы с файлом!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user