From aa0db38855844b4bc6d2bbc74803192a4cddc473 Mon Sep 17 00:00:00 2001 From: Paritosh-Pranjal Date: Fri, 8 Sep 2023 17:54:10 +0530 Subject: [PATCH] fix: update feature deadline --- .../training/tasks/AddTaskCommand.java | 18 ++++ .../training/tasks/CheckTaskCommand.java | 16 ++++ .../com/codurance/training/tasks/Command.java | 5 ++ .../com/codurance/training/tasks/Task.java | 32 +++---- .../codurance/training/tasks/TaskImpl.java | 46 ++++++++++ .../codurance/training/tasks/TaskList.java | 90 ++++++++++++++++--- .../training/tasks/UncheckTaskCommand.java | 16 ++++ 7 files changed, 189 insertions(+), 34 deletions(-) create mode 100644 java/src/main/java/com/codurance/training/tasks/AddTaskCommand.java create mode 100644 java/src/main/java/com/codurance/training/tasks/CheckTaskCommand.java create mode 100644 java/src/main/java/com/codurance/training/tasks/Command.java create mode 100644 java/src/main/java/com/codurance/training/tasks/TaskImpl.java create mode 100644 java/src/main/java/com/codurance/training/tasks/UncheckTaskCommand.java diff --git a/java/src/main/java/com/codurance/training/tasks/AddTaskCommand.java b/java/src/main/java/com/codurance/training/tasks/AddTaskCommand.java new file mode 100644 index 00000000..1c79da37 --- /dev/null +++ b/java/src/main/java/com/codurance/training/tasks/AddTaskCommand.java @@ -0,0 +1,18 @@ +package com.codurance.training.tasks; + +class AddTaskCommand implements Command { + private final TaskList taskList; + private final String project; + private final String description; + + public AddTaskCommand(TaskList taskList, String project, String description) { + this.taskList = taskList; + this.project = project; + this.description = description; + } + + @Override + public void execute() { + taskList.addTask(project, description); + } +} \ No newline at end of file diff --git a/java/src/main/java/com/codurance/training/tasks/CheckTaskCommand.java b/java/src/main/java/com/codurance/training/tasks/CheckTaskCommand.java new file mode 100644 index 00000000..1d1e918a --- /dev/null +++ b/java/src/main/java/com/codurance/training/tasks/CheckTaskCommand.java @@ -0,0 +1,16 @@ +package com.codurance.training.tasks; + +class CheckTaskCommand implements Command { + private final TaskList taskList; + private final String idString; + + public CheckTaskCommand(TaskList taskList, String idString) { + this.taskList = taskList; + this.idString = idString; + } + + @Override + public void execute() { + taskList.setDone(idString, true); + } +} diff --git a/java/src/main/java/com/codurance/training/tasks/Command.java b/java/src/main/java/com/codurance/training/tasks/Command.java new file mode 100644 index 00000000..43d8f8a1 --- /dev/null +++ b/java/src/main/java/com/codurance/training/tasks/Command.java @@ -0,0 +1,5 @@ +package com.codurance.training.tasks; + +interface Command { + void execute(); +} \ No newline at end of file diff --git a/java/src/main/java/com/codurance/training/tasks/Task.java b/java/src/main/java/com/codurance/training/tasks/Task.java index 31b39c98..7a294420 100644 --- a/java/src/main/java/com/codurance/training/tasks/Task.java +++ b/java/src/main/java/com/codurance/training/tasks/Task.java @@ -1,29 +1,17 @@ package com.codurance.training.tasks; -public final class Task { - private final long id; - private final String description; - private boolean done; +import java.util.Date; - public Task(long id, String description, boolean done) { - this.id = id; - this.description = description; - this.done = done; - } +interface Task { + long getId(); - public long getId() { - return id; - } + String getDescription(); - public String getDescription() { - return description; - } + boolean isDone(); - public boolean isDone() { - return done; - } + void setDone(boolean done); - public void setDone(boolean done) { - this.done = done; - } -} + Date getDeadline(); + + void setDeadline(Date deadline); +} \ No newline at end of file diff --git a/java/src/main/java/com/codurance/training/tasks/TaskImpl.java b/java/src/main/java/com/codurance/training/tasks/TaskImpl.java new file mode 100644 index 00000000..8f5b59d7 --- /dev/null +++ b/java/src/main/java/com/codurance/training/tasks/TaskImpl.java @@ -0,0 +1,46 @@ +package com.codurance.training.tasks; + +import java.util.Date; + +class TaskImpl implements Task { + private final long id; + private final String description; + private boolean done; + private Date deadline; + + public TaskImpl(long id, String description, boolean done) { + this.id = id; + this.description = description; + this.done = done; + } + + @Override + public long getId() { + return id; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public boolean isDone() { + return done; + } + + @Override + public void setDone(boolean done) { + this.done = done; + } + + @Override + public Date getDeadline() { + return deadline; + } + + @Override + public void setDeadline(Date deadline) { + this.deadline = deadline; + } +} diff --git a/java/src/main/java/com/codurance/training/tasks/TaskList.java b/java/src/main/java/com/codurance/training/tasks/TaskList.java index 9a4a8202..0ab47eeb 100644 --- a/java/src/main/java/com/codurance/training/tasks/TaskList.java +++ b/java/src/main/java/com/codurance/training/tasks/TaskList.java @@ -4,18 +4,17 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; -public final class TaskList implements Runnable { +public class TaskList { private static final String QUIT = "quit"; + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private final Map> tasks = new LinkedHashMap<>(); private final BufferedReader in; private final PrintWriter out; - private long lastId = 0; public static void main(String[] args) throws Exception { @@ -62,6 +61,12 @@ private void execute(String commandLine) { case "uncheck": uncheck(commandRest[1]); break; + case "today": + showTasksDueToday(); + break; + case "deadline": + setDeadline(commandRest[1]); + break; case "help": help(); break; @@ -71,16 +76,73 @@ private void execute(String commandLine) { } } + private void showTasksDueToday() { + Date today = new Date(); + for (Map.Entry> project : tasks.entrySet()) { + out.println(project.getKey()); + for (Task task : project.getValue()) { + if (task.getDeadline() != null && isSameDay(today, task.getDeadline())) { + out.printf(" [%c] %d: %s (Due: %s)%n", (task.isDone() ? 'x' : ' '), task.getId(), task.getDescription(), DATE_FORMAT.format(task.getDeadline())); + } + } + out.println(); + } + } + + private boolean isSameDay(Date date1, Date date2) { + Calendar cal1 = Calendar.getInstance(); + cal1.setTime(date1); + Calendar cal2 = Calendar.getInstance(); + cal2.setTime(date2); + return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && + cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && + cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH); + } + + private void setDeadline(String deadlineCommand) { + String[] parts = deadlineCommand.split(" ", 2); + String idString = parts[0]; + String date = parts[1]; + int id = Integer.parseInt(idString); + try { + Date deadline = DATE_FORMAT.parse(date); + setTaskDeadline(id, deadline); + } catch (ParseException e) { + out.println("Invalid date format. Use yyyy-MM-dd."); + } + } + + private void setTaskDeadline(int id, Date deadline) { + for (Map.Entry> project : tasks.entrySet()) { + for (Task task : project.getValue()) { + if (task.getId() == id) { + task.setDeadline(deadline); + return; + } + } + } + out.printf("Could not find a task with an ID of %d.", id); + out.println(); + } + private void show() { for (Map.Entry> project : tasks.entrySet()) { out.println(project.getKey()); for (Task task : project.getValue()) { - out.printf(" [%c] %d: %s%n", (task.isDone() ? 'x' : ' '), task.getId(), task.getDescription()); + printTask(task); } out.println(); } } + private void printTask(Task task) { + out.printf(" [%c] %d: %s", (task.isDone() ? 'x' : ' '), task.getId(), task.getDescription()); + if (task.getDeadline() != null) { + out.printf(" (Due: %s)", DATE_FORMAT.format(task.getDeadline())); + } + out.println(); + } + private void add(String commandLine) { String[] subcommandRest = commandLine.split(" ", 2); String subcommand = subcommandRest[0]; @@ -93,19 +155,20 @@ private void add(String commandLine) { } private void addProject(String name) { - tasks.put(name, new ArrayList()); + tasks.put(name, new ArrayList<>()); } - private void addTask(String project, String description) { + public void addTask(String project, String description) { List projectTasks = tasks.get(project); if (projectTasks == null) { out.printf("Could not find a project with the name \"%s\".", project); out.println(); return; } - projectTasks.add(new Task(nextId(), description, false)); + projectTasks.add(new TaskImpl(nextId(), description, false)); } + private void check(String idString) { setDone(idString, true); } @@ -114,7 +177,7 @@ private void uncheck(String idString) { setDone(idString, false); } - private void setDone(String idString, boolean done) { + public void setDone(String idString, boolean done) { int id = Integer.parseInt(idString); for (Map.Entry> project : tasks.entrySet()) { for (Task task : project.getValue()) { @@ -128,6 +191,7 @@ private void setDone(String idString, boolean done) { out.println(); } + private void help() { out.println("Commands:"); out.println(" show"); @@ -135,6 +199,8 @@ private void help() { out.println(" add task "); out.println(" check "); out.println(" uncheck "); + out.println(" today"); + out.println(" deadline "); out.println(); } @@ -146,4 +212,4 @@ private void error(String command) { private long nextId() { return ++lastId; } -} +} \ No newline at end of file diff --git a/java/src/main/java/com/codurance/training/tasks/UncheckTaskCommand.java b/java/src/main/java/com/codurance/training/tasks/UncheckTaskCommand.java new file mode 100644 index 00000000..24e03e36 --- /dev/null +++ b/java/src/main/java/com/codurance/training/tasks/UncheckTaskCommand.java @@ -0,0 +1,16 @@ +package com.codurance.training.tasks; + +class UncheckTaskCommand implements Command { + private final TaskList taskList; + private final String idString; + + public UncheckTaskCommand(TaskList taskList, String idString) { + this.taskList = taskList; + this.idString = idString; + } + + @Override + public void execute() { + taskList.setDone(idString, false); + } +}