From 83f0c53afa533671ba6d7c9cd7151ab9a5340871 Mon Sep 17 00:00:00 2001 From: howardwhw2 Date: Thu, 10 Feb 2022 15:48:14 +0800 Subject: [PATCH] Add tag extension There is a lack of any features to categorize tasks in the list to make it easier to see what the task is about Adding of tag a tag feature would help to solve this problem To do so adding the function to be able to tag tasks with additional details would make it easier to organize and categorize tasks. --- src/main/java/van/Deadline.java | 6 ++- src/main/java/van/Event.java | 6 ++- src/main/java/van/Parser.java | 70 +++++++++++++++++++++++++++---- src/main/java/van/Tag.java | 36 ++++++++++++++++ src/main/java/van/TagCommand.java | 30 +++++++++++++ src/main/java/van/Task.java | 10 +++++ src/main/java/van/TaskList.java | 8 ++++ src/main/java/van/ToDo.java | 5 ++- src/main/java/van/Ui.java | 12 +++++- 9 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 src/main/java/van/Tag.java create mode 100644 src/main/java/van/TagCommand.java diff --git a/src/main/java/van/Deadline.java b/src/main/java/van/Deadline.java index 1b044221e4..0e42f67c6a 100644 --- a/src/main/java/van/Deadline.java +++ b/src/main/java/van/Deadline.java @@ -30,7 +30,8 @@ public Deadline(String description, LocalDateTime date) { @Override public String saveStatus() { return "D|" + super.getCompletion() + "|" + description + "|" + - date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")); + date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")) + + "| " + tag.toString(); } /** @@ -42,6 +43,7 @@ public String saveStatus() { @Override public String getStatus() { return "[D]" + super.getStatus() + " (by:" + - date.format(DateTimeFormatter.ofPattern("MMM d yyyy HHmm")) + ")"; + date.format(DateTimeFormatter.ofPattern("MMM d yyyy HHmm")) + ") " + + tag.toString(); } } diff --git a/src/main/java/van/Event.java b/src/main/java/van/Event.java index df61c4de27..ac2c4dd0be 100644 --- a/src/main/java/van/Event.java +++ b/src/main/java/van/Event.java @@ -31,7 +31,8 @@ public Event(String description, LocalDateTime date) { @Override public String saveStatus() { return "E|" + super.getCompletion() + "|" + description + "|" + - date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")); + date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")) + + "| " + tag.toString(); } /** @@ -43,6 +44,7 @@ public String saveStatus() { @Override public String getStatus() { return "[E]" + super.getStatus() + " (at:" + - date.format(DateTimeFormatter.ofPattern("MMM d yyyy HHmm")) + ")"; + date.format(DateTimeFormatter.ofPattern("MMM d yyyy HHmm")) + ") " + + tag.toString(); } } diff --git a/src/main/java/van/Parser.java b/src/main/java/van/Parser.java index dd0ca6664a..886475e16e 100644 --- a/src/main/java/van/Parser.java +++ b/src/main/java/van/Parser.java @@ -35,6 +35,12 @@ public static Command parseCommand(String parameter) { case "find": output = caseFind(parameters); break; + case "tag": + output = caseTag(parameters); + break; + case "untag": + output = caseUnTag(parameters); + break; case "bye": output = new ExitCommand(); break; @@ -52,21 +58,24 @@ public static ArrayList parseArray(ArrayList taskList) { if (taskList != null) { for (int i = 0; i < taskList.size(); i++) { String fileLine = taskList.get(i); - String[] taskDetail = fileLine.split("\\|"); - switch (taskDetail[0]) { + String[] taskDetails = fileLine.split("\\|"); + switch (taskDetails[0]) { case "E": - date = LocalDateTime.parse(taskDetail[3], dateFormat); - tasks.add(new Event(taskDetail[2], date)); + date = LocalDateTime.parse(taskDetails[3], dateFormat); + tasks.add(new Event(taskDetails[2], date)); + addTags(tasks.get(i), taskDetails[4]); break; case "D": - date = LocalDateTime.parse(taskDetail[3], dateFormat); - tasks.add(new Deadline(taskDetail[2], date)); + date = LocalDateTime.parse(taskDetails[3], dateFormat); + tasks.add(new Deadline(taskDetails[2], date)); + addTags(tasks.get(i), taskDetails[4]); break; case "T": - tasks.add(new ToDo(taskDetail[2])); + tasks.add(new ToDo(taskDetails[2])); + addTags(tasks.get(i), taskDetails[3]); break; } - if (taskDetail[1].equals("1")) { + if (taskDetails[1].equals("1")) { tasks.get(i).setDone(); } } @@ -74,6 +83,13 @@ public static ArrayList parseArray(ArrayList taskList) { return tasks; } + public static void addTags(Task task, String tag) { + String[] tags = tag.split(" #"); + for (int i = 1; i < tags.length; i++) { + task.addTag(tags[i]); + } + } + public static String[] parseDescription(Task task) { String[] keywords = task.getDescription().split(" "); return keywords; @@ -189,4 +205,42 @@ private static Command caseFind(String[] parameters) { return new InvalidCommand(ex.getError()); } } + + private static Command caseTag(String[] parameters) { + try { + if (parameters.length != 2) { + throw new VanException("Invalid format. Please use tag #"); + } + String[] taskArguments = parameters[1].split(" #"); + if (taskArguments.length != 2) { + throw new VanException("Invalid format. Please use tag #"); + } + try { + return new TagCommand(true, Integer.parseInt(taskArguments[0]), taskArguments[1]); + } catch (NumberFormatException ex) { + return new InvalidCommand("Please use integers e.g. 1, 2"); + } + } catch(VanException ex) { + return new InvalidCommand(ex.getError()); + } + } + + private static Command caseUnTag(String[] parameters) { + try { + if (parameters.length != 2) { + throw new VanException("Invalid format. Please use untag #"); + } + String[] taskArguments = parameters[1].split(" #"); + if (taskArguments.length != 2) { + throw new VanException("Invalid format. Please use untag #"); + } + try { + return new TagCommand(false, Integer.parseInt(taskArguments[0]), taskArguments[1]); + } catch (NumberFormatException ex) { + return new InvalidCommand("Please use integers e.g. 1, 2"); + } + } catch(VanException ex) { + return new InvalidCommand(ex.getError()); + } + } } diff --git a/src/main/java/van/Tag.java b/src/main/java/van/Tag.java new file mode 100644 index 0000000000..12a6500b41 --- /dev/null +++ b/src/main/java/van/Tag.java @@ -0,0 +1,36 @@ +package van; + +import java.util.ArrayList; + +public class Tag { + private ArrayList tags; + + public Tag() { + tags = new ArrayList<>(); + } + + public void add(String newTag) { + if (!(tags.contains(newTag))) { + tags.add(newTag); + } + } + + public void delete(String newTag) { + int index = tags.indexOf(newTag); + if (index != -1) { + tags.remove(index); + } + } + + public ArrayList getTags() { + return tags; + } + + public String toString() { + String output = ""; + for (int i = 0; i < tags.size(); i++) { + output = output + "#" + tags.get(i) + " "; + } + return output; + } +} diff --git a/src/main/java/van/TagCommand.java b/src/main/java/van/TagCommand.java new file mode 100644 index 0000000000..b891b603ba --- /dev/null +++ b/src/main/java/van/TagCommand.java @@ -0,0 +1,30 @@ +package van; + +public class TagCommand implements Command { + private boolean isAddTag; + private String tag; + private int index; + + public TagCommand(boolean isAddTag,int index, String tag) { + this.isAddTag = isAddTag; + this.index = index; + this.tag = tag; + } + + public boolean executeCommand(Ui ui, TaskList taskList, Storage storage) { + try { + if (index > taskList.getTaskCount()) { + throw new VanException("Task number out of range"); + } + if (isAddTag) { + taskList.addTag(index, tag); + } else { + taskList.deleteTag(index, tag); + } + ui.tagMessage(isAddTag); + } catch (VanException ex) { + System.out.println(ex); + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/van/Task.java b/src/main/java/van/Task.java index 3fc679a5c6..cf154dbd6f 100644 --- a/src/main/java/van/Task.java +++ b/src/main/java/van/Task.java @@ -6,6 +6,7 @@ public class Task { protected String description; protected boolean done; + protected Tag tag; /** * Creates a Task object that encapsulates the description of the @@ -16,6 +17,7 @@ public class Task { public Task(String description) { this.description = description; this.done = false; + this.tag = new Tag(); } /** @@ -68,4 +70,12 @@ public void setUnDone() { public String getDescription() { return description; } + + public void addTag(String newTag) { + tag.add(newTag); + } + + public void deleteTag(String newTag) { + tag.delete(newTag); + } } diff --git a/src/main/java/van/TaskList.java b/src/main/java/van/TaskList.java index bebef53f35..aba0b4d39c 100644 --- a/src/main/java/van/TaskList.java +++ b/src/main/java/van/TaskList.java @@ -35,4 +35,12 @@ public int getTaskCount() { public ArrayList getList() { return tasks; } + + public void addTag(int taskNumber, String newTag) { + this.tasks.get(taskNumber - 1).addTag(newTag); + } + + public void deleteTag(int taskNumber, String newTag) { + this.tasks.get(taskNumber - 1).deleteTag(newTag); + } } diff --git a/src/main/java/van/ToDo.java b/src/main/java/van/ToDo.java index a10a2e71e2..06224bf660 100644 --- a/src/main/java/van/ToDo.java +++ b/src/main/java/van/ToDo.java @@ -22,7 +22,7 @@ public ToDo(String description) { */ @Override public String getStatus() { - return "[T]" + super.getStatus(); + return "[T]" + super.getStatus() + " " + tag.toString(); } /** @@ -33,6 +33,7 @@ public String getStatus() { */ @Override public String saveStatus() { - return "T|" + super.getCompletion() + "|" + description; + return "T|" + super.getCompletion() + "|" + description + + "| " + tag.toString(); } } diff --git a/src/main/java/van/Ui.java b/src/main/java/van/Ui.java index fbd3e1332c..306f1562f1 100644 --- a/src/main/java/van/Ui.java +++ b/src/main/java/van/Ui.java @@ -15,9 +15,19 @@ public void exitMessage() { } public void invalidMessage(String message) { + builder.setLength(0); builder.append(message); } + public void tagMessage(boolean isAddTag) { + builder.setLength(0); + if (isAddTag) { + builder.append("tag added\n"); + } else { + builder.append("tag deleted\n"); + } + } + public void taskMessage(Task task, int count) { builder.setLength(0); builder.append("Task added\n"); @@ -27,7 +37,7 @@ public void taskMessage(Task task, int count) { public void markMessage() { builder.setLength(0); - builder.append("Changed task status"); + builder.append("Changed task status\n"); } public void printList(ArrayList taskList) {