Skip to content

Commit

Permalink
Add Delete feature
Browse files Browse the repository at this point in the history
There is no option to delete a task from the list.

Add delete feature for users to remove a task.
  • Loading branch information
rgonslayer committed Aug 24, 2022
1 parent bca45e9 commit 1f2120d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 81 deletions.
81 changes: 0 additions & 81 deletions src/main/java/Duke.java

This file was deleted.

93 changes: 93 additions & 0 deletions src/main/java/Jarvis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Jarvis {
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);

String userInput;
String introduction = "Hello! I'm Jarvis \n"
+ "What can I do for you?";
String farewell = "Bye. Hope to see you again soon!";

List<Task> taskList = new ArrayList<>();

System.out.println(introduction);

while (true) {
//receive user input
userInput = inputScanner.nextLine();
//check if userinput is bye, break if true
if (userInput.equals("bye")) {
break;
}
//if userinput equals list, return task list
if (userInput.equals("list")) {
System.out.println("Here are the tasks in your list:\n");
for (int i = 0; i < taskList.size(); i ++) {
if (taskList.get(i) == null) {
break;
}
System.out.println((i + 1) + ". " + taskList.get(i).toString());
}
continue;
}
//if userinput equals mark, check which task and mark it
if (userInput.length() > 4 && userInput.substring(0, 4).equals("mark")) {
int toMark = Integer.parseInt(userInput.substring(5)) - 1;
taskList.get(toMark).mark();
String markResponse = "Nice! I've marked this task as done:\n ";
System.out.println(markResponse + taskList.get(toMark).toString());
continue;
}
//if userinput equals unmark, check which task and unmark
if (userInput.length() > 6 && userInput.substring(0, 6).equals("unmark")) {
int toMark = Integer.parseInt(userInput.substring(7)) - 1;
taskList.get(toMark).unmark();
String markResponse = "Ok, I've marked this task as not done yet:\n ";
System.out.println(markResponse + taskList.get(toMark).toString());
continue;
}
//if userinput equals delete, check which task and delete
if (userInput.length() > 6 && userInput.substring(0, 6).equals("delete")) {
int toDelete = Integer.parseInt(userInput.substring(7)) - 1;
Task deleteTask = taskList.get(toDelete);
taskList.remove(toDelete);
String deleteResponse = "Noted. I've removed this task:\n ";
System.out.println(deleteResponse + deleteTask.toString());
continue;
}

//if userinput equals to do add new to do task to list
if (userInput.length() > 4 && userInput.substring(0, 4).equals("todo")) {
taskList.add(new ToDo(userInput));
System.out.println("Got it. I've added this task:\n " + taskList.get(Task.count - 1)
+ "\nNow you have " + (Task.count) + " tasks in the list.");
}

//if userinput equals deadline add new deadline task to list
if (userInput.length() > 8 && userInput.substring(0, 8).equals("deadline")) {
int divisor = userInput.indexOf("/by");
String description = userInput.substring(9, divisor - 1);
String date = userInput.substring(divisor + 4);
taskList.add(new Deadline(description, date));
System.out.println("Got it. I've added this task:\n " + taskList.get(Task.count - 1)
+ "\nNow you have " + (Task.count) + " tasks in the list.");
}
//if userinput equals event add new event task to list
if (userInput.length() > 7 && userInput.substring(0,5).equals("event")) {
int divisor = userInput.indexOf("/at");
String description = userInput.substring(6, divisor - 1);
String date = userInput.substring(divisor + 4);
taskList.add(new Event(description, date));
System.out.println("Got it. I've added this task:\n " + taskList.get(Task.count - 1)
+ "\nNow you have " + (Task.count) + " tasks in the list.");
}
}

System.out.println(farewell);


}
}
2 changes: 2 additions & 0 deletions src/main/java/JarvisException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class JarvisException {
}

0 comments on commit 1f2120d

Please sign in to comment.