Compare commits
6
Commits
v2.1.0
...
da878ec81c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da878ec81c | ||
|
|
4cd38a9afe | ||
|
|
2c7deaa9ae | ||
|
|
8399ab0d58 | ||
|
|
f0b79f0db0 | ||
|
|
09cbf440ad |
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>ru.kamask.pet</groupId>
|
<groupId>ru.kamask.pet</groupId>
|
||||||
<artifactId>todo</artifactId>
|
<artifactId>todo</artifactId>
|
||||||
<version>2.1</version>
|
<version>2.2.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.release>24</maven.compiler.release>
|
<maven.compiler.release>24</maven.compiler.release>
|
||||||
|
|||||||
@@ -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,20 +27,19 @@ 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";
|
||||||
String res = "";
|
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||||
res += String.format(template, "ID", "Название задачи", "Статус");
|
res.append("-".repeat(50) + "\n");
|
||||||
res += "-".repeat(50) + "\n";
|
|
||||||
|
|
||||||
if (tasks.size() == 0)
|
if (tasks.size() == 0)
|
||||||
return res + "\n" + msgIfEmpty;
|
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 += String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено");
|
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;
|
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.SimpleTask;
|
||||||
|
|
||||||
public class InMemoryTaskRepository implements TaskRepository {
|
public class InMemoryTaskRepository implements TaskRepository {
|
||||||
private Map<Integer, Task> storage = new HashMap<>();
|
private Map<Integer, SimpleTask> storage = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Task task) {
|
public void save(SimpleTask task) {
|
||||||
storage.put(task.id(), task);
|
storage.put(task.id(), task);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Task> findById(int id) {
|
public Optional<SimpleTask> findById(int id) {
|
||||||
return Optional.ofNullable(storage.get(id));
|
return Optional.ofNullable(storage.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Task> findAll() {
|
public List<SimpleTask> findAll() {
|
||||||
return new ArrayList<>(storage.values());
|
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 +1,5 @@
|
|||||||
package ru.kamask.pet.todo.repo;
|
package ru.kamask.pet.todo.repo;
|
||||||
|
|
||||||
import java.util.List;
|
import ru.kamask.pet.todo.model.SimpleTask;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Task;
|
public interface TaskRepository extends Repository<SimpleTask> {}
|
||||||
|
|
||||||
public interface TaskRepository {
|
|
||||||
void save(Task task);
|
|
||||||
|
|
||||||
Optional<Task> findById(int id);
|
|
||||||
|
|
||||||
List<Task> findAll();
|
|
||||||
|
|
||||||
void delete(int id);
|
|
||||||
|
|
||||||
boolean has(int id);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ 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.TaskRepository;
|
||||||
|
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user