Compare commits
No commits in common. "fb5ede78cedb7597b0a99d4ac440f65357763ccb" and "f0b79f0db0c6e089f64748412da60e20c122c306" have entirely different histories.
fb5ede78ce
...
f0b79f0db0
@ -3,13 +3,12 @@ package ru.kamask.pet.todo;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.cli.CliEngine;
|
import ru.kamask.pet.todo.cli.CliEngine;
|
||||||
import ru.kamask.pet.todo.model.SimpleTask;
|
import ru.kamask.pet.todo.repo.InMemoryTaskRepository;
|
||||||
import ru.kamask.pet.todo.repo.InMemoryRepository;
|
|
||||||
import ru.kamask.pet.todo.service.TaskService;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
|
|
||||||
public class TodoApp {
|
public class TodoApp {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
var service = new TaskService(new InMemoryRepository<SimpleTask>());
|
var service = new TaskService(new InMemoryTaskRepository());
|
||||||
var cli = new CliEngine(service);
|
var cli = new CliEngine(service);
|
||||||
|
|
||||||
cli.start();
|
cli.start();
|
||||||
|
|||||||
@ -7,15 +7,14 @@ import java.util.Arrays;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
import ru.kamask.pet.todo.service.EntityService;
|
|
||||||
|
|
||||||
public class CliEngine {
|
public class CliEngine {
|
||||||
private HashMap<String, Command> registry = new HashMap<>();
|
private HashMap<String, Command> registry = new HashMap<>();
|
||||||
private EntityService<? extends Identifiable> service;
|
private TaskService service;
|
||||||
private BufferedReader reader;
|
private BufferedReader reader;
|
||||||
|
|
||||||
public CliEngine(EntityService<? extends Identifiable> service) {
|
public CliEngine(TaskService service) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
reader = new BufferedReader(new InputStreamReader(System.in));
|
reader = new BufferedReader(new InputStreamReader(System.in));
|
||||||
initializeCommands();
|
initializeCommands();
|
||||||
|
|||||||
@ -2,14 +2,13 @@ package ru.kamask.pet.todo.cli;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
import ru.kamask.pet.todo.service.EntityService;
|
|
||||||
|
|
||||||
public interface Command {
|
public interface Command {
|
||||||
String templateUsage = " %-30s // %s";
|
String templateUsage = " %-30s // %s";
|
||||||
String errorMessage = "Не корректно введена команда. Введите help для спарвки.";
|
String errorMessage = "Не корректно введена команда. Введите help для спарвки.";
|
||||||
|
|
||||||
Optional<String> handle(String[] args, EntityService<? extends Identifiable> service);
|
Optional<String> handle(String[] args, TaskService service);
|
||||||
|
|
||||||
String name();
|
String name();
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,6 @@ package ru.kamask.pet.todo.cli;
|
|||||||
|
|
||||||
import java.util.Optional;
|
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;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
|
|
||||||
public class CompleteCommand implements Command {
|
public class CompleteCommand implements Command {
|
||||||
@ -18,16 +16,14 @@ public class CompleteCommand implements Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
public Optional<String> handle(String[] args, TaskService service) {
|
||||||
if (args.length != 1)
|
if (args.length != 1)
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
|
|
||||||
TaskService taskService = (TaskService) service;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int id = Integer.parseInt(args[0]);
|
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)
|
||||||
: "Задача ID-%d не найдена.".formatted(id));
|
: "Задача ID-%d не найдена.".formatted(id));
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,6 @@ package ru.kamask.pet.todo.cli;
|
|||||||
|
|
||||||
import java.util.Optional;
|
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;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
|
|
||||||
public class CreateCommand implements Command {
|
public class CreateCommand implements Command {
|
||||||
@ -13,13 +11,12 @@ public class CreateCommand implements Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
public Optional<String> handle(String[] args, TaskService service) {
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
var title = String.join(" ", args);
|
var title = String.join(" ", args);
|
||||||
if (title.length() > 30)
|
if (title.length() > 30)
|
||||||
return Optional.of("Ошибка: максимальная длинна названия задачи 30 символов.");
|
return Optional.of("Ошибка: максимальная длинна названия задачи 30 символов.");
|
||||||
TaskService taskService = (TaskService) service;
|
service.create(title);
|
||||||
taskService.create(title);
|
|
||||||
return Optional.of(String.format("Задача \"%s\" успешно добавлена!", title));
|
return Optional.of(String.format("Задача \"%s\" успешно добавлена!", title));
|
||||||
}
|
}
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
|
|||||||
@ -2,8 +2,7 @@ package ru.kamask.pet.todo.cli;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
import ru.kamask.pet.todo.service.EntityService;
|
|
||||||
|
|
||||||
public class DeleteCommand implements Command {
|
public class DeleteCommand implements Command {
|
||||||
@Override
|
@Override
|
||||||
@ -17,7 +16,7 @@ public class DeleteCommand implements Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
public Optional<String> handle(String[] args, TaskService service) {
|
||||||
if (args.length != 1)
|
if (args.length != 1)
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
package ru.kamask.pet.todo.cli;
|
package ru.kamask.pet.todo.cli;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
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.model.Task;
|
||||||
import ru.kamask.pet.todo.service.TaskService;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
import ru.kamask.pet.todo.util.Formatter;
|
|
||||||
|
|
||||||
public class ListCommand implements Command {
|
public class ListCommand implements Command {
|
||||||
@Override
|
@Override
|
||||||
@ -14,17 +14,33 @@ public class ListCommand implements Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
public Optional<String> handle(String[] args, TaskService service) {
|
||||||
if (args.length > 0)
|
if (args.length > 0)
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
|
|
||||||
TaskService taskService = (TaskService) service;
|
var res = formatWithTable(service.list(), "Список задач пуст.");
|
||||||
|
|
||||||
return Optional.of(Formatter.asTable(taskService.getAll()));
|
return Optional.of(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String usage() {
|
public String usage() {
|
||||||
return String.format(templateUsage, name(), "Список всех задач.");
|
return String.format(templateUsage, name(), "Список всех задач.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String formatWithTable(List<Task> tasks, String msgIfEmpty) {
|
||||||
|
String template = "%-2s | %-30s | %s\n";
|
||||||
|
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||||
|
res.append("-".repeat(50) + "\n");
|
||||||
|
|
||||||
|
if (tasks.size() == 0)
|
||||||
|
return res.append("\n" + msgIfEmpty).toString();
|
||||||
|
|
||||||
|
for (Task task : tasks) {
|
||||||
|
SimpleTask.Data data = ((SimpleTask) task).data();
|
||||||
|
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,7 @@ package ru.kamask.pet.todo.cli;
|
|||||||
|
|
||||||
import java.util.Optional;
|
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;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
import ru.kamask.pet.todo.util.Formatter;
|
|
||||||
|
|
||||||
public class SearchCommand implements Command {
|
public class SearchCommand implements Command {
|
||||||
@Override
|
@Override
|
||||||
@ -19,16 +16,15 @@ public class SearchCommand implements Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
public Optional<String> handle(String[] args, TaskService service) {
|
||||||
if (args.length != 1)
|
if (args.length != 1)
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
if (args[0].length() < 3)
|
if (args[0].length() < 3)
|
||||||
return Optional.of("Длина запроса должна быть не менее 3 символов.");
|
return Optional.of("Длина запроса должна быть не менее 3 символов.");
|
||||||
|
|
||||||
TaskService taskService = (TaskService) service;
|
var matchTask = service.search(args[0]);
|
||||||
|
var res = new ListCommand().formatWithTable(matchTask, "Не найдено задач, соответствующих запросу.");
|
||||||
|
|
||||||
var matchTask = taskService.search(args[0]);
|
return Optional.of(res);
|
||||||
|
|
||||||
return Optional.of(Formatter.asTable(matchTask, "Не найдено задач, соответствующих запросу."));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.model;
|
|
||||||
|
|
||||||
public interface Identifiable {
|
|
||||||
int getId();
|
|
||||||
}
|
|
||||||
@ -1,6 +1,6 @@
|
|||||||
package ru.kamask.pet.todo.model;
|
package ru.kamask.pet.todo.model;
|
||||||
|
|
||||||
public abstract class Task implements Identifiable{
|
public abstract class Task {
|
||||||
private static int nextId = 1;
|
private static int nextId = 1;
|
||||||
|
|
||||||
protected int id;
|
protected int id;
|
||||||
@ -11,13 +11,8 @@ public abstract class Task implements Identifiable{
|
|||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int id() {
|
public int id() {
|
||||||
return getId();
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -6,23 +6,23 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
import ru.kamask.pet.todo.model.Task;
|
||||||
|
|
||||||
public class InMemoryRepository<T extends Identifiable> implements Repository<T> {
|
public class InMemoryTaskRepository implements TaskRepository {
|
||||||
private Map<Integer, T> storage = new HashMap<>();
|
private Map<Integer, Task> storage = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(T obj) {
|
public void save(Task task) {
|
||||||
storage.put(obj.getId(), obj);
|
storage.put(task.id(), task);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<T> findById(int id) {
|
public Optional<Task> findById(int id) {
|
||||||
return Optional.ofNullable(storage.get(id));
|
return Optional.ofNullable(storage.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> findAll() {
|
public List<Task> findAll() {
|
||||||
return new ArrayList<>(storage.values());
|
return new ArrayList<>(storage.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.repo;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
|
||||||
|
|
||||||
public interface Repository<T extends Identifiable> {
|
|
||||||
void save(T obj);
|
|
||||||
Optional<T> findById(int id);
|
|
||||||
List<T> findAll();
|
|
||||||
void delete(int id);
|
|
||||||
boolean has(int id);
|
|
||||||
}
|
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package ru.kamask.pet.todo.repo;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import ru.kamask.pet.todo.model.Task;
|
||||||
|
|
||||||
|
public interface TaskRepository {
|
||||||
|
void save(Task task);
|
||||||
|
|
||||||
|
Optional<Task> findById(int id);
|
||||||
|
|
||||||
|
List<Task> findAll();
|
||||||
|
|
||||||
|
void delete(int id);
|
||||||
|
|
||||||
|
boolean has(int id);
|
||||||
|
}
|
||||||
@ -1,35 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
|
||||||
import ru.kamask.pet.todo.repo.Repository;
|
|
||||||
|
|
||||||
public class EntityService<T extends Identifiable> {
|
|
||||||
private final Repository<T> repo;
|
|
||||||
|
|
||||||
protected EntityService(Repository<T> repo) {
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save(T obj) {
|
|
||||||
repo.save(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<T> getById(int id) {
|
|
||||||
return repo.findById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<T> getAll() {
|
|
||||||
return repo.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(int id) {
|
|
||||||
repo.delete(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean has(int id) {
|
|
||||||
return repo.has(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +1,29 @@
|
|||||||
package ru.kamask.pet.todo.service;
|
package ru.kamask.pet.todo.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import ru.kamask.pet.todo.model.Task;
|
||||||
import ru.kamask.pet.todo.model.SimpleTask;
|
import ru.kamask.pet.todo.model.SimpleTask;
|
||||||
import ru.kamask.pet.todo.repo.Repository;
|
import ru.kamask.pet.todo.repo.TaskRepository;
|
||||||
|
|
||||||
public class TaskService extends EntityService<SimpleTask> {
|
public class TaskService {
|
||||||
|
private final TaskRepository repo;
|
||||||
|
|
||||||
public TaskService(Repository<SimpleTask> repo) {
|
public TaskService(TaskRepository repo) {
|
||||||
super(repo);
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create(String title) {
|
public void create(String title) {
|
||||||
super.save(new SimpleTask(title));
|
repo.save(new SimpleTask(title));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<Task> getById(int id) {
|
||||||
|
return repo.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean complete(int id) {
|
public boolean complete(int id) {
|
||||||
var taskOpt = super.getById(id);
|
var taskOpt = repo.findById(id);
|
||||||
if (taskOpt.isPresent()) {
|
if (taskOpt.isPresent()) {
|
||||||
taskOpt.get().markAsCompleted();
|
taskOpt.get().markAsCompleted();
|
||||||
return true;
|
return true;
|
||||||
@ -25,10 +31,21 @@ public class TaskService extends EntityService<SimpleTask> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Task> list() {
|
||||||
|
return repo.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
public List<SimpleTask> search(String query) {
|
public void remove(int id) {
|
||||||
return super.getAll().stream()
|
repo.delete(id);
|
||||||
.filter(task -> task.data().title().contains(query))
|
}
|
||||||
|
|
||||||
|
public boolean has(int id) {
|
||||||
|
return repo.has(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Task> search(String query) {
|
||||||
|
return repo.findAll().stream()
|
||||||
|
.filter(task -> ((SimpleTask) task).data().title().contains(query))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.util;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.SimpleTask;
|
|
||||||
|
|
||||||
public class Formatter {
|
|
||||||
public static String asTable(List<SimpleTask> tasks){
|
|
||||||
return asTable(tasks, "Список задач пуст.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String asTable(List<SimpleTask> tasks, String msgIfEmpty){
|
|
||||||
String template = "%-2s | %-30s | %s\n";
|
|
||||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
|
||||||
res.append("-".repeat(50) + "\n");
|
|
||||||
|
|
||||||
if (tasks.size() == 0)
|
|
||||||
return res.append("\n" + msgIfEmpty).toString();
|
|
||||||
|
|
||||||
for (SimpleTask task : tasks) {
|
|
||||||
SimpleTask.Data data = task.data();
|
|
||||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user