Skip to content

Commit 5bb12d4

Browse files
committed
Level 6. Delete
1 parent 6b91d86 commit 5bb12d4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/main/java/Duke.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,43 @@ public static void main(String[] args) {
158158
}
159159
break;
160160

161+
case "delete":
162+
System.out.println("Enter index:");
163+
int ind = getUserIndex.nextInt() - 1;
164+
if (ind < 0 || ind >= listPointer) {
165+
System.out.println("Invalid index.");
166+
} else {
167+
168+
System.out.println("Noted. I've removed this task:");
169+
170+
System.out.println(userList[listPointer].display());
171+
172+
Task[] newUserList = new Task[100];
173+
174+
for (int a = 0, k = 0; a < listPointer; a++) {
175+
176+
// if the index is
177+
// the removal element index
178+
if (a == ind) {
179+
continue;
180+
}
181+
182+
// if the index is not
183+
// the removal element index
184+
newUserList[k++] = userList[a];
185+
}
186+
187+
listPointer = listPointer - 1;
188+
189+
userList = newUserList;
190+
191+
System.out.println("Now you have " + listPointer + " tasks in the list.");
192+
193+
}
194+
195+
break;
196+
197+
161198
default:
162199
System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-(");
163200

0 commit comments

Comments
 (0)