Skip to content

Commit

Permalink
Level-6
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccalaujx committed Aug 19, 2021
1 parent 310178c commit 1cbdef7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public static void main(String[] args) {
System.out.println(task.markedAsDoneToString());
}
}
} else if (input.startsWith("delete")) {
if (input.equals("delete") || input.equals("delete ")) {
throw new DukeException("An index must follow after the command word 'delete'.");
} else {
int arrIndex = Integer.valueOf(input.substring(7)) - 1;
if (arrIndex < 0 || arrIndex >= ls.getSize()) {
throw new DukeException("Item does not exist in the list.");
} else {
Task task = ls.getTask(arrIndex);
ls.removeTask(arrIndex);
System.out.println(ls.removeTaskToString(task));
}
}
} else {
if (input.startsWith("todo")) {
String taskDesc = input.replaceFirst("^todo", "");
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public String addTaskToString(Task task) {
+ "\nNow you have " + this.tasklist.size() + " tasks in the list.");
}

public void removeTask(int index) {
this.tasklist.remove(index);
}

public String removeTaskToString(Task task) {
return ("Noted. I've removed this task: \n" +
task.toString()
+ "\nNow you have " + this.tasklist.size() + " tasks in the list.");
}

public Task getTask(int index) {
return this.tasklist.get(index);
}
Expand Down

0 comments on commit 1cbdef7

Please sign in to comment.