Skip to content

Commit

Permalink
Add tag extension
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
howardwhw2 committed Feb 10, 2022
1 parent b72b77c commit 83f0c53
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/main/java/van/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}
}
6 changes: 4 additions & 2 deletions src/main/java/van/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}
}
70 changes: 62 additions & 8 deletions src/main/java/van/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,28 +58,38 @@ public static ArrayList<Task> parseArray(ArrayList<String> 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();
}
}
}
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;
Expand Down Expand Up @@ -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 <task number> #<tag>");
}
String[] taskArguments = parameters[1].split(" #");
if (taskArguments.length != 2) {
throw new VanException("Invalid format. Please use tag <task number> #<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 <task number> #<tag>");
}
String[] taskArguments = parameters[1].split(" #");
if (taskArguments.length != 2) {
throw new VanException("Invalid format. Please use untag <task number> #<tag>");
}
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());
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/van/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package van;

import java.util.ArrayList;

public class Tag {
private ArrayList<String> 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<String> getTags() {
return tags;
}

public String toString() {
String output = "";
for (int i = 0; i < tags.size(); i++) {
output = output + "#" + tags.get(i) + " ";
}
return output;
}
}
30 changes: 30 additions & 0 deletions src/main/java/van/TagCommand.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions src/main/java/van/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,7 @@ public class Task {
public Task(String description) {
this.description = description;
this.done = false;
this.tag = new Tag();
}

/**
Expand Down Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/van/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public int getTaskCount() {
public ArrayList<Task> 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);
}
}
5 changes: 3 additions & 2 deletions src/main/java/van/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ToDo(String description) {
*/
@Override
public String getStatus() {
return "[T]" + super.getStatus();
return "[T]" + super.getStatus() + " " + tag.toString();
}

/**
Expand All @@ -33,6 +33,7 @@ public String getStatus() {
*/
@Override
public String saveStatus() {
return "T|" + super.getCompletion() + "|" + description;
return "T|" + super.getCompletion() + "|" + description +
"| " + tag.toString();
}
}
12 changes: 11 additions & 1 deletion src/main/java/van/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<Task> taskList) {
Expand Down

0 comments on commit 83f0c53

Please sign in to comment.