Compare commits
No commits in common. "fb5ede78cedb7597b0a99d4ac440f65357763ccb" and "8254f5292bc2a22d3cb35089aca6f8ea40c40855" have entirely different histories.
fb5ede78ce
...
8254f5292b
@ -1,11 +1,12 @@
|
||||
package ru.kamask.pet.todo.cli;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
import ru.kamask.pet.todo.util.Formatter;
|
||||
|
||||
public class ListCommand implements Command {
|
||||
@Override
|
||||
@ -17,14 +18,31 @@ public class ListCommand implements Command {
|
||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||
if (args.length > 0)
|
||||
return Optional.of(Command.errorMessage);
|
||||
|
||||
TaskService taskService = (TaskService) service;
|
||||
|
||||
return Optional.of(Formatter.asTable(taskService.getAll()));
|
||||
List<SimpleTask> allTasks = taskService.getAll();
|
||||
var res = formatWithTable(allTasks, "Список задач пуст.");
|
||||
|
||||
return Optional.of(res);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usage() {
|
||||
return String.format(templateUsage, name(), "Список всех задач.");
|
||||
}
|
||||
|
||||
String formatWithTable(List<SimpleTask> tasks, String msgIfEmpty) {
|
||||
String template = "%-2s | %-30s | %s\n";
|
||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||
res.append("-".repeat(50) + "\n");
|
||||
|
||||
if (tasks.size() == 0)
|
||||
return res.append("\n" + msgIfEmpty).toString();
|
||||
|
||||
for (SimpleTask task : tasks) {
|
||||
SimpleTask.Data data = task.data();
|
||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||
}
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import java.util.Optional;
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
import ru.kamask.pet.todo.util.Formatter;
|
||||
|
||||
public class SearchCommand implements Command {
|
||||
@Override
|
||||
@ -28,7 +27,8 @@ public class SearchCommand implements Command {
|
||||
TaskService taskService = (TaskService) service;
|
||||
|
||||
var matchTask = taskService.search(args[0]);
|
||||
var res = new ListCommand().formatWithTable(matchTask, "Не найдено задач, соответствующих запросу.");
|
||||
|
||||
return Optional.of(Formatter.asTable(matchTask, "Не найдено задач, соответствующих запросу."));
|
||||
return Optional.of(res);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
package ru.kamask.pet.todo.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
|
||||
public class Formatter {
|
||||
public static String asTable(List<SimpleTask> tasks){
|
||||
return asTable(tasks, "Список задач пуст.");
|
||||
}
|
||||
|
||||
public static String asTable(List<SimpleTask> tasks, String msgIfEmpty){
|
||||
String template = "%-2s | %-30s | %s\n";
|
||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||
res.append("-".repeat(50) + "\n");
|
||||
|
||||
if (tasks.size() == 0)
|
||||
return res.append("\n" + msgIfEmpty).toString();
|
||||
|
||||
for (SimpleTask task : tasks) {
|
||||
SimpleTask.Data data = task.data();
|
||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||
}
|
||||
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user