Compare commits

..

No commits in common. "8254f5292bc2a22d3cb35089aca6f8ea40c40855" and "18acbd3b327999cabc4aec0c3321d2f017198d7d" have entirely different histories.

7 changed files with 18 additions and 35 deletions

View File

@ -7,15 +7,14 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public class CliEngine {
private HashMap<String, Command> registry = new HashMap<>();
private EntityService<? extends Identifiable> service;
private TaskService service;
private BufferedReader reader;
public CliEngine(EntityService<? extends Identifiable> service) {
public CliEngine(TaskService service) {
this.service = service;
reader = new BufferedReader(new InputStreamReader(System.in));
initializeCommands();

View File

@ -2,14 +2,13 @@ package ru.kamask.pet.todo.cli;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public interface Command {
String templateUsage = " %-30s // %s";
String errorMessage = "Не корректно введена команда. Введите help для спарвки.";
Optional<String> handle(String[] args, EntityService<? extends Identifiable> service);
Optional<String> handle(String[] args, TaskService service);
String name();

View File

@ -2,8 +2,6 @@ package ru.kamask.pet.todo.cli;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public class CompleteCommand implements Command {
@ -18,16 +16,14 @@ public class CompleteCommand implements Command {
}
@Override
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
public Optional<String> handle(String[] args, TaskService service) {
if (args.length != 1)
return Optional.of(Command.errorMessage);
TaskService taskService = (TaskService) service;
try {
int id = Integer.parseInt(args[0]);
return Optional.of(taskService.complete(id)
return Optional.of(service.complete(id)
? "Задача ID-%d выполнена.".formatted(id)
: "Задача ID-%d не найдена.".formatted(id));

View File

@ -2,8 +2,6 @@ package ru.kamask.pet.todo.cli;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public class CreateCommand implements Command {
@ -13,13 +11,12 @@ public class CreateCommand implements Command{
}
@Override
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
public Optional<String> handle(String[] args, TaskService service) {
if (args.length > 0) {
var title = String.join(" ", args);
if (title.length() > 30)
return Optional.of("Ошибка: максимальная длинна названия задачи 30 символов.");
TaskService taskService = (TaskService) service;
taskService.create(title);
service.create(title);
return Optional.of(String.format("Задача \"%s\" успешно добавлена!", title));
}
return Optional.of(Command.errorMessage);

View File

@ -2,8 +2,7 @@ package ru.kamask.pet.todo.cli;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public class DeleteCommand implements Command {
@Override
@ -17,7 +16,7 @@ public class DeleteCommand implements Command{
}
@Override
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
public Optional<String> handle(String[] args, TaskService service) {
if (args.length != 1)
return Optional.of(Command.errorMessage);

View File

@ -3,9 +3,7 @@ package ru.kamask.pet.todo.cli;
import java.util.List;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.model.SimpleTask;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public class ListCommand implements Command {
@ -15,12 +13,11 @@ public class ListCommand implements Command {
}
@Override
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
public Optional<String> handle(String[] args, TaskService service) {
if (args.length > 0)
return Optional.of(Command.errorMessage);
TaskService taskService = (TaskService) service;
List<SimpleTask> allTasks = taskService.getAll();
var res = formatWithTable(allTasks, "Список задач пуст.");
var res = formatWithTable(service.getAll(), "Список задач пуст.");
return Optional.of(res);
}

View File

@ -2,8 +2,6 @@ package ru.kamask.pet.todo.cli;
import java.util.Optional;
import ru.kamask.pet.todo.model.Identifiable;
import ru.kamask.pet.todo.service.EntityService;
import ru.kamask.pet.todo.service.TaskService;
public class SearchCommand implements Command {
@ -18,15 +16,13 @@ public class SearchCommand implements Command {
}
@Override
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
public Optional<String> handle(String[] args, TaskService service) {
if (args.length != 1)
return Optional.of(Command.errorMessage);
if (args[0].length() < 3)
return Optional.of("Длина запроса должна быть не менее 3 символов.");
TaskService taskService = (TaskService) service;
var matchTask = taskService.search(args[0]);
var matchTask = service.search(args[0]);
var res = new ListCommand().formatWithTable(matchTask, "Не найдено задач, соответствующих запросу.");
return Optional.of(res);