From 09cbf440adc1380eb8e4fb8cfdc39fcca1c5725f Mon Sep 17 00:00:00 2001 From: KamaSK Date: Wed, 4 Jun 2025 16:45:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20ListCommand?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -Обновление версии проекта до 2.2.0-SNAPSHOT -Рефакторинг класса ListCommand для объединения строк через класс StringBuilder -Повышение читаемости и производительности метода formatWithTable --- todo/pom.xml | 2 +- .../java/ru/kamask/pet/todo/cli/ListCommand.java | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/todo/pom.xml b/todo/pom.xml index 3058f16..d4451e7 100644 --- a/todo/pom.xml +++ b/todo/pom.xml @@ -6,7 +6,7 @@ ru.kamask.pet todo - 2.1 + 2.2.0-SNAPSHOT 24 diff --git a/todo/src/main/java/ru/kamask/pet/todo/cli/ListCommand.java b/todo/src/main/java/ru/kamask/pet/todo/cli/ListCommand.java index 80b2fc5..c7cb854 100644 --- a/todo/src/main/java/ru/kamask/pet/todo/cli/ListCommand.java +++ b/todo/src/main/java/ru/kamask/pet/todo/cli/ListCommand.java @@ -19,7 +19,7 @@ public class ListCommand implements Command { return Optional.of(Command.errorMessage); var res = formatWithTable(service.list(), "Список задач пуст."); - + return Optional.of(res); } @@ -28,20 +28,19 @@ public class ListCommand implements Command { return String.format(templateUsage, name(), "Список всех задач."); } - String formatWithTable(List tasks, String msgIfEmpty){ + String formatWithTable(List 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() ? "выполнено" : "не выполнено"); + res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено")); } - return res; + return res.toString(); } }