Skip to content

Commit

Permalink
Add function to delete a task
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuLeYao committed Feb 7, 2023
1 parent b5e1e00 commit 2008de1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public void printDeadlineTask() {
"\n\t____________________________________________________________");
}

@Override
public void printDelete() {
int newTotalNumOfTasks = super.totalNumOfTasks - 1;
System.out.println("\t____________________________________________________________" +
"\n\t Noted. I've removed this task:" +
"\n\t " + this.taskType +
super.getTaskStatus() + " " + super.task + " (by: " + this.taskDeadline + ")" +
"\n\t Now you have " + newTotalNumOfTasks + " tasks in the list." +
"\n\t____________________________________________________________");
}

@Override
public String getTaskType() {
return this.taskType;
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public void chatDuke() {
taskName, eventStartTime, eventEndTime, allTasks.size() + 1);
allTasks.add(event);
event.printEventTask();
} else if (command.startsWith("delete")) {
missingIndexException(command);
invalidIndexException(command, allTasks.size());
String[] str = command.split(" ");
int taskIndex = Integer.parseInt(str[1]) - 1;
Task task = allTasks.get(taskIndex);
task.printDelete();
allTasks.remove(taskIndex);
} else if (command.equals("bye")){
saidBye = true;
this.printByeMessage();
Expand All @@ -121,6 +129,10 @@ public void chatDuke() {
}
} catch (DukeException d) {
System.out.println(d.getMessage());
} catch (NumberFormatException nfe) {
System.out.println("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to delete or un/mark a task cannot be a non-integer." +
"\n\t____________________________________________________________");
}
}
}
Expand Down Expand Up @@ -153,12 +165,6 @@ public void printCommandList(List<Task> allTasks) {
System.out.println("\t____________________________________________________________");
}

public void echoCommand(String command) {
System.out.println("\t____________________________________________________________" +
"\n\t" + " added: " + command +
"\n\t____________________________________________________________");
}

public void printByeMessage() {
System.out.println("\t____________________________________________________________" +
"\n\t Bye. Hope to see you again soon!" +
Expand Down Expand Up @@ -207,24 +213,29 @@ public void missingIndexException(String command) throws DukeException {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to unmark a task as not done cannot be empty." +
"\n\t____________________________________________________________");
} else if (command.equals("delete")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to delete a task as not done cannot be empty." +
"\n\t____________________________________________________________");
}
}

public void invalidIndexException(String command, int taskSize) throws DukeException {
if (command.startsWith("mark") || command.startsWith("unmark")) {
if (command.startsWith("mark") || command.startsWith("unmark")
||command.startsWith("delete")) {
String index = command.split(" ")[1];
int index1 = Integer.parseInt(index);
if (index1 <= 0) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be zero or less." +
"\n\t ☹ OOPS!!! The task index to delete or un/mark a task cannot be zero or less." +
"\n\t____________________________________________________________");
} else if (index.equals("")) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be empty." +
"\n\t ☹ OOPS!!! The task index to delete or un/mark a task cannot be empty." +
"\n\t____________________________________________________________");
} else if (index1 > taskSize) {
throw new DukeException("\t____________________________________________________________" +
"\n\t ☹ OOPS!!! The task index to mark a task as done cannot be more than" +
"\n\t ☹ OOPS!!! The task index to delete or un/mark a task cannot be more than" +
" number of tasks." +
"\n\t____________________________________________________________");
}
Expand All @@ -240,5 +251,4 @@ public void invalidCommandException(String command) throws DukeException {
"\n\t____________________________________________________________");
}
}

}
12 changes: 12 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public void printEventTask() {
"\n\t____________________________________________________________");
}

@Override
public void printDelete() {
int newTotalNumOfTasks = super.totalNumOfTasks - 1;
System.out.println("\t____________________________________________________________" +
"\n\t Noted. I've removed this task:" +
"\n\t " + this.taskType +
super.getTaskStatus() + " " + super.task + " (from: " + this.eventStartTime +
" to: " + this.eventEndTime + ")" + "\n\t Now you have " +
newTotalNumOfTasks + " tasks in the list." +
"\n\t____________________________________________________________");
}

@Override
public String getTaskType() {
return this.taskType;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.util.Objects;

public class Task {
protected final int taskNumber;
protected final boolean taskStatus;
Expand Down Expand Up @@ -27,6 +25,13 @@ public void unmarkAsUndone() {
"\n\t____________________________________________________________");
}

public void printDelete() {
System.out.println("\t____________________________________________________________" +
"\n\t Noted. I've removed this task:" + "\n\t " +
this.getTaskStatus() + " " + this.task +
"\n\t Now you have " + this.totalNumOfTasks + " tasks in the list.");
}

public int getTaskNumber() {
return this.taskNumber;
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ class Todo extends Task {
this.taskType = "[T]";
}

@Override
public void markAsDone() {
System.out.println("\t____________________________________________________________" +
"\n\t Nice! I've marked this task as done:\n" +
"\t " + this.taskType + "[X]" + " " + this.task +
"\n\t____________________________________________________________");
}

@Override
public void unmarkAsUndone() {
System.out.println("\t____________________________________________________________" +
"\n\t OK, I've marked this task as not done yet:\n" +
Expand All @@ -28,6 +30,16 @@ public void printToDoTask() {
"\n\t____________________________________________________________");
}

@Override
public void printDelete() {
int newTotalNumOfTasks = super.totalNumOfTasks - 1;
System.out.println("\t____________________________________________________________" +
"\n\t Noted. I've removed this task:" + "\n\t " + this.taskType +
super.getTaskStatus() + " " + super.task +
"\n\t Now you have " + newTotalNumOfTasks + " tasks in the list." +
"\n\t____________________________________________________________");
}

@Override
public String getTaskType() {
return this.taskType;
Expand Down

0 comments on commit 2008de1

Please sign in to comment.