Compare commits
8 Commits
f0b79f0db0
...
728cb4f84f
| Author | SHA1 | Date | |
|---|---|---|---|
| 728cb4f84f | |||
| 5140228f3e | |||
| 0174224089 | |||
| 8c0cc0aa06 | |||
| da878ec81c | |||
| 4cd38a9afe | |||
| 2c7deaa9ae | |||
| 8399ab0d58 |
@ -3,12 +3,13 @@ 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.repo.InMemoryTaskRepository;
|
import ru.kamask.pet.todo.model.SimpleTask;
|
||||||
|
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 InMemoryTaskRepository());
|
var service = new TaskService(new InMemoryRepository<SimpleTask>());
|
||||||
var cli = new CliEngine(service);
|
var cli = new CliEngine(service);
|
||||||
|
|
||||||
cli.start();
|
cli.start();
|
||||||
|
|||||||
@ -4,7 +4,6 @@ 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 {
|
||||||
@ -28,7 +27,7 @@ public class ListCommand implements Command {
|
|||||||
return String.format(templateUsage, name(), "Список всех задач.");
|
return String.format(templateUsage, name(), "Список всех задач.");
|
||||||
}
|
}
|
||||||
|
|
||||||
String formatWithTable(List<Task> tasks, String msgIfEmpty) {
|
String formatWithTable(List<SimpleTask> 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");
|
||||||
@ -36,8 +35,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 (Task task : tasks) {
|
for (SimpleTask task : tasks) {
|
||||||
SimpleTask.Data data = ((SimpleTask) task).data();
|
SimpleTask.Data data = task.data();
|
||||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,5 @@
|
|||||||
|
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 {
|
public abstract class Task implements Identifiable{
|
||||||
private static int nextId = 1;
|
private static int nextId = 1;
|
||||||
|
|
||||||
protected int id;
|
protected int id;
|
||||||
@ -11,10 +11,15 @@ public abstract class Task {
|
|||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int id() {
|
@Override
|
||||||
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int id() {
|
||||||
|
return getId();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Задача: id - %d, title: \"%s\"", id, title);
|
return String.format("Задача: id - %d, title: \"%s\"", id, title);
|
||||||
|
|||||||
@ -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.Task;
|
import ru.kamask.pet.todo.model.Identifiable;
|
||||||
|
|
||||||
public class InMemoryTaskRepository implements TaskRepository {
|
public class InMemoryRepository<T extends Identifiable> implements Repository<T> {
|
||||||
private Map<Integer, Task> storage = new HashMap<>();
|
private Map<Integer, T> storage = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Task task) {
|
public void save(T obj) {
|
||||||
storage.put(task.id(), task);
|
storage.put(obj.getId(), obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Task> findById(int id) {
|
public Optional<T> findById(int id) {
|
||||||
return Optional.ofNullable(storage.get(id));
|
return Optional.ofNullable(storage.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Task> findAll() {
|
public List<T> findAll() {
|
||||||
return new ArrayList<>(storage.values());
|
return new ArrayList<>(storage.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
14
todo/src/main/java/ru/kamask/pet/todo/repo/Repository.java
Normal file
14
todo/src/main/java/ru/kamask/pet/todo/repo/Repository.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
@ -1,18 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void save(T obj) {
|
||||||
|
repo.save(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Optional<T> getById(int id) {
|
||||||
|
return repo.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<T> getAll() {
|
||||||
|
return repo.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void remove(int id) {
|
||||||
|
repo.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean has(int id) {
|
||||||
|
return repo.has(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,14 +3,13 @@ package ru.kamask.pet.todo.service;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
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.TaskRepository;
|
import ru.kamask.pet.todo.repo.InMemoryRepository;
|
||||||
|
|
||||||
public class TaskService {
|
public class TaskService {
|
||||||
private final TaskRepository repo;
|
private final InMemoryRepository<SimpleTask> repo;
|
||||||
|
|
||||||
public TaskService(TaskRepository repo) {
|
public TaskService(InMemoryRepository<SimpleTask> repo) {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,7 +17,7 @@ public class TaskService {
|
|||||||
repo.save(new SimpleTask(title));
|
repo.save(new SimpleTask(title));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Task> getById(int id) {
|
public Optional<SimpleTask> getById(int id) {
|
||||||
return repo.findById(id);
|
return repo.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +30,7 @@ public class TaskService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Task> list() {
|
public List<SimpleTask> list() {
|
||||||
return repo.findAll();
|
return repo.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,9 +42,9 @@ public class TaskService {
|
|||||||
return repo.has(id);
|
return repo.has(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Task> search(String query) {
|
public List<SimpleTask> search(String query) {
|
||||||
return repo.findAll().stream()
|
return repo.findAll().stream()
|
||||||
.filter(task -> ((SimpleTask) task).data().title().contains(query))
|
.filter(task -> task.data().title().contains(query))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user