From 109de24693419328aa78f99e2e1ec84b25d1e858 Mon Sep 17 00:00:00 2001 From: Chang Ji-xuan Date: Wed, 30 Aug 2023 11:45:23 +0800 Subject: [PATCH] Level-6 added ability to delete tasks. --- src/main/java/Duke.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1bcab54bbc..0934aab06c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,3 @@ - import java.util.Scanner; import java.util.ArrayList; @@ -54,7 +53,7 @@ public static void main(String[] args) { System.out.println("Got it. I've added this task:"); System.out.println(" " + todo); - System.out.println("Now you have " + tasksList.size() + " tasks in the list"); + System.out.println("Now you have " + tasksList.size() + " tasks in the list."); } catch (EmptyTodoException e) { System.out.println(e.getMessage()); } @@ -82,7 +81,7 @@ public static void main(String[] args) { System.out.println("Got it. I've added this deadline:"); System.out.println(" " + deadlineTask); - System.out.println("Now you have " + tasksList.size() + " tasks in the list"); + System.out.println("Now you have " + tasksList.size() + " tasks in the list."); } else { System.out.println("Invalid input format for deadline command."); } @@ -114,11 +113,23 @@ public static void main(String[] args) { // Print confirmation message System.out.println("Got it. I've added this task:"); System.out.println(" " + eventTask); - System.out.println("Now you have " + tasksList.size() + " tasks in the list"); + System.out.println("Now you have " + tasksList.size() + " tasks in the list."); } else { System.out.println("Invalid input format for event command."); } } + } else if (command.startsWith("delete")) { + int taskNumber = Integer.parseInt(command.substring(7)) - 1; + if (taskNumber < tasksList.size()) { + Tasks task = tasksList.get(taskNumber); + tasksList.remove(taskNumber); + + System.out.println("Noted. I've removed this task: \n" + " " + task); + System.out.println("Now you have " + tasksList.size() + " tasks in the list."); + } + + + } else { try { throw new UnknownInputException(); @@ -126,7 +137,6 @@ public static void main(String[] args) { System.out.println(e.getMessage()); } } - } } }