From 1cbdef71a0388209e67b9b70919b25fb66be7760 Mon Sep 17 00:00:00 2001 From: rebeccalaujx Date: Fri, 20 Aug 2021 02:58:42 +0800 Subject: [PATCH] Level-6 --- src/main/java/Duke.java | 13 +++++++++++++ src/main/java/TaskList.java | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index cf8ab9d205..16b2b8125a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -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", ""); diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 8f3acd594b..f88caee84d 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -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); }