Compare commits
No commits in common. "18acbd3b327999cabc4aec0c3321d2f017198d7d" and "f0b79f0db0c6e089f64748412da60e20c122c306" have entirely different histories.
18acbd3b32
...
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();
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.SimpleTask;
|
import ru.kamask.pet.todo.model.SimpleTask;
|
||||||
|
import ru.kamask.pet.todo.model.Task;
|
||||||
import ru.kamask.pet.todo.service.TaskService;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
|
|
||||||
public class ListCommand implements Command {
|
public class ListCommand implements Command {
|
||||||
@ -17,7 +18,7 @@ public class ListCommand implements Command {
|
|||||||
if (args.length > 0)
|
if (args.length > 0)
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
|
|
||||||
var res = formatWithTable(service.getAll(), "Список задач пуст.");
|
var res = formatWithTable(service.list(), "Список задач пуст.");
|
||||||
|
|
||||||
return Optional.of(res);
|
return Optional.of(res);
|
||||||
}
|
}
|
||||||
@ -27,7 +28,7 @@ public class ListCommand implements Command {
|
|||||||
return String.format(templateUsage, name(), "Список всех задач.");
|
return String.format(templateUsage, name(), "Список всех задач.");
|
||||||
}
|
}
|
||||||
|
|
||||||
String formatWithTable(List<SimpleTask> tasks, String msgIfEmpty) {
|
String formatWithTable(List<Task> tasks, String msgIfEmpty) {
|
||||||
String template = "%-2s | %-30s | %s\n";
|
String template = "%-2s | %-30s | %s\n";
|
||||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||||
res.append("-".repeat(50) + "\n");
|
res.append("-".repeat(50) + "\n");
|
||||||
@ -35,8 +36,8 @@ public class ListCommand implements Command {
|
|||||||
if (tasks.size() == 0)
|
if (tasks.size() == 0)
|
||||||
return res.append("\n" + msgIfEmpty).toString();
|
return res.append("\n" + msgIfEmpty).toString();
|
||||||
|
|
||||||
for (SimpleTask task : tasks) {
|
for (Task task : tasks) {
|
||||||
SimpleTask.Data data = task.data();
|
SimpleTask.Data data = ((SimpleTask) task).data();
|
||||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user