Compare commits
10
Commits
v2.1.0
...
728cb4f84f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
728cb4f84f | ||
|
|
5140228f3e | ||
|
|
0174224089 | ||
|
|
8c0cc0aa06 | ||
|
|
da878ec81c | ||
|
|
4cd38a9afe | ||
|
|
2c7deaa9ae | ||
|
|
8399ab0d58 | ||
|
|
f0b79f0db0 | ||
|
|
09cbf440ad |
+1
-1
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>ru.kamask.pet</groupId>
|
||||
<artifactId>todo</artifactId>
|
||||
<version>2.1</version>
|
||||
<version>2.2.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>24</maven.compiler.release>
|
||||
|
||||
@@ -3,12 +3,13 @@ package ru.kamask.pet.todo;
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
|
||||
public class TodoApp {
|
||||
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);
|
||||
|
||||
cli.start();
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
import ru.kamask.pet.todo.model.Task;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
|
||||
public class ListCommand implements Command {
|
||||
@@ -28,20 +27,19 @@ public class ListCommand implements Command {
|
||||
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 res = "";
|
||||
res += String.format(template, "ID", "Название задачи", "Статус");
|
||||
res += "-".repeat(50) + "\n";
|
||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||
res.append("-".repeat(50) + "\n");
|
||||
|
||||
if (tasks.size() == 0)
|
||||
return res + "\n" + msgIfEmpty;
|
||||
return res.append("\n" + msgIfEmpty).toString();
|
||||
|
||||
for (Task task : tasks) {
|
||||
SimpleTask.Data data = ((SimpleTask) task).data();
|
||||
res += String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено");
|
||||
for (SimpleTask task : tasks) {
|
||||
SimpleTask.Data data = task.data();
|
||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||
}
|
||||
|
||||
return res;
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
public abstract class Task {
|
||||
public abstract class Task implements Identifiable{
|
||||
private static int nextId = 1;
|
||||
|
||||
protected int id;
|
||||
@@ -11,10 +11,15 @@ public abstract class Task {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public int id() {
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Задача: id - %d, title: \"%s\"", id, title);
|
||||
|
||||
+7
-7
@@ -6,23 +6,23 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Task;
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
|
||||
public class InMemoryTaskRepository implements TaskRepository {
|
||||
private Map<Integer, Task> storage = new HashMap<>();
|
||||
public class InMemoryRepository<T extends Identifiable> implements Repository<T> {
|
||||
private Map<Integer, T> storage = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void save(Task task) {
|
||||
storage.put(task.id(), task);
|
||||
public void save(T obj) {
|
||||
storage.put(obj.getId(), obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Task> findById(int id) {
|
||||
public Optional<T> findById(int id) {
|
||||
return Optional.ofNullable(storage.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Task> findAll() {
|
||||
public List<T> findAll() {
|
||||
return new ArrayList<>(storage.values());
|
||||
}
|
||||
|
||||
@@ -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.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Task;
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
import ru.kamask.pet.todo.repo.TaskRepository;
|
||||
import ru.kamask.pet.todo.repo.InMemoryRepository;
|
||||
|
||||
public class TaskService {
|
||||
private final TaskRepository repo;
|
||||
private final InMemoryRepository<SimpleTask> repo;
|
||||
|
||||
public TaskService(TaskRepository repo) {
|
||||
public TaskService(InMemoryRepository<SimpleTask> repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
@@ -18,7 +17,7 @@ public class TaskService {
|
||||
repo.save(new SimpleTask(title));
|
||||
}
|
||||
|
||||
public Optional<Task> getById(int id) {
|
||||
public Optional<SimpleTask> getById(int id) {
|
||||
return repo.findById(id);
|
||||
}
|
||||
|
||||
@@ -31,7 +30,7 @@ public class TaskService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Task> list() {
|
||||
public List<SimpleTask> list() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@@ -43,9 +42,9 @@ public class TaskService {
|
||||
return repo.has(id);
|
||||
}
|
||||
|
||||
public List<Task> search(String query) {
|
||||
public List<SimpleTask> search(String query) {
|
||||
return repo.findAll().stream()
|
||||
.filter(task -> ((SimpleTask) task).data().title().contains(query))
|
||||
.filter(task -> task.data().title().contains(query))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user