Compare commits
No commits in common. "da878ec81c454c9291df6548711e109d6c96e4aa" and "f0b79f0db0c6e089f64748412da60e20c122c306" have entirely different histories.
da878ec81c
...
f0b79f0db0
@ -4,6 +4,7 @@ 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 {
|
||||
@ -27,7 +28,7 @@ public class ListCommand implements Command {
|
||||
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";
|
||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||
res.append("-".repeat(50) + "\n");
|
||||
@ -35,8 +36,8 @@ public class ListCommand implements Command {
|
||||
if (tasks.size() == 0)
|
||||
return res.append("\n" + msgIfEmpty).toString();
|
||||
|
||||
for (SimpleTask task : tasks) {
|
||||
SimpleTask.Data data = task.data();
|
||||
for (Task task : tasks) {
|
||||
SimpleTask.Data data = ((SimpleTask) task).data();
|
||||
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;
|
||||
|
||||
public abstract class Task implements Identifiable{
|
||||
public abstract class Task {
|
||||
private static int nextId = 1;
|
||||
|
||||
protected int id;
|
||||
@ -11,13 +11,8 @@ public abstract class Task implements Identifiable{
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return getId();
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -6,23 +6,23 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
import ru.kamask.pet.todo.model.Task;
|
||||
|
||||
public class InMemoryTaskRepository implements TaskRepository {
|
||||
private Map<Integer, SimpleTask> storage = new HashMap<>();
|
||||
private Map<Integer, Task> storage = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void save(SimpleTask task) {
|
||||
public void save(Task task) {
|
||||
storage.put(task.id(), task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<SimpleTask> findById(int id) {
|
||||
public Optional<Task> findById(int id) {
|
||||
return Optional.ofNullable(storage.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SimpleTask> findAll() {
|
||||
public List<Task> findAll() {
|
||||
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);
|
||||
}
|
||||
@ -1,5 +1,18 @@
|
||||
package ru.kamask.pet.todo.repo;
|
||||
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TaskRepository extends Repository<SimpleTask> {}
|
||||
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);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ 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;
|
||||
|
||||
@ -17,7 +18,7 @@ public class TaskService {
|
||||
repo.save(new SimpleTask(title));
|
||||
}
|
||||
|
||||
public Optional<SimpleTask> getById(int id) {
|
||||
public Optional<Task> getById(int id) {
|
||||
return repo.findById(id);
|
||||
}
|
||||
|
||||
@ -30,7 +31,7 @@ public class TaskService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<SimpleTask> list() {
|
||||
public List<Task> list() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@ -42,9 +43,9 @@ public class TaskService {
|
||||
return repo.has(id);
|
||||
}
|
||||
|
||||
public List<SimpleTask> search(String query) {
|
||||
public List<Task> search(String query) {
|
||||
return repo.findAll().stream()
|
||||
.filter(task -> task.data().title().contains(query))
|
||||
.filter(task -> ((SimpleTask) task).data().title().contains(query))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user