Skip to content

Commit

Permalink
Final edits
Browse files Browse the repository at this point in the history
  • Loading branch information
pkpaing committed Feb 17, 2023
1 parent 9ed6fc0 commit 7b4bf29
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
3 changes: 2 additions & 1 deletion data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
T | 1 | go home
T | 1 | eat lunch
T | 0 | finish work
T | 0 | finish work
D | 0 | iP | 17-02-2023
93 changes: 57 additions & 36 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,47 @@ public static String handleCommand(String s, TaskList t) throws DukeException {
* Command to toggle whether a task is done or not
*/
public static String toggleMarked(String s, TaskList t, boolean mark) {
int taskNumber = Integer.parseInt(s.substring(s.length() - 1)) - 1;
if (mark && !t.get(taskNumber).getIsDone()) {
t.get(taskNumber).toggleMarked();
} else if (!mark && t.get(taskNumber).getIsDone()) {
t.get(taskNumber).toggleMarked();
} else {
return mark ? " This task is already marked" :
" This task is already unmarked";
}
try {
int taskNumber = Integer.parseInt(s.substring(s.length() - 1)) - 1;
if (mark && !t.get(taskNumber).getIsDone()) {
t.get(taskNumber).toggleMarked();
} else if (!mark && t.get(taskNumber).getIsDone()) {
t.get(taskNumber).toggleMarked();
} else {
return mark ? " This task is already marked" :
" This task is already unmarked";
}

String output = "";
if (s.contains("unmark")) {
output += " OK, I've marked this task as not done yet:";
} else {
output += " Nice! I've marked this task as done:";
String output = "";
if (s.contains("unmark")) {
output += " OK, I've marked this task as not done yet:";
} else {
output += " Nice! I've marked this task as done:";
}
return output + " " + t.get(taskNumber).toString();

} catch (StringIndexOutOfBoundsException e) {
return "Incorrect command format.\nUse: mark <index> or unmark <index>";
}
return output + " " + t.get(taskNumber).toString();

}

/**
* Command to add a To-Do to the TaskList
*/
public static String addTodo(String s, TaskList t) {
if (s.substring(4).isBlank()) {
return " OOPS!!! The description of a todo cannot be empty.";
} else {
Task newTask = new Todo(s.substring(5));
t.addTask(newTask);
return " added: " + newTask;
try {
if (s.substring(4).isBlank()) {
return " OOPS!!! The description of a todo cannot be empty.";
} else {
Task newTask = new Todo(s.substring(5));
t.addTask(newTask);
return " added: " + newTask;
}
} catch (StringIndexOutOfBoundsException e) {
return "Incorrect command format.\nUse: todo <description>";
}

}

/**
Expand Down Expand Up @@ -153,30 +164,40 @@ public static String addEvent(String s, TaskList t) {
* Command to delete a task from the TaskList
*/
public static String deleteTask(String s, TaskList t) {
if (s.substring(6).isBlank()) {
return " OOPS!!! You have not entered anything to delete.";
} else {
int taskNumber = Integer.parseInt(s.substring(s.length() - 1)) - 1;
Task deletedTask = t.get(taskNumber);
t.remove(taskNumber);
return " Noted. I've removed this task:\n " + deletedTask +
"\n Now you have " + t.size()+ " tasks in the list";
try {
if (s.substring(6).isBlank()) {
return " OOPS!!! You have not entered anything to delete.";
} else {
int taskNumber = Integer.parseInt(s.substring(s.length() - 1)) - 1;
Task deletedTask = t.get(taskNumber);
t.remove(taskNumber);
return " Noted. I've removed this task:\n " + deletedTask +
"\n Now you have " + t.size()+ " tasks in the list";

}
} catch (StringIndexOutOfBoundsException e) {
return "Incorrect command format.\nUse: delete <index>";
}

}

/**
* Command to find a Task in the TaskList
*/
public static String findTasks(String s, TaskList t) {
String findString = s.substring(5);
ArrayList<Task> foundTasks = new ArrayList<Task>();
for (Task task : t.getTasks()) {
if (task.toString().contains(findString)) {
foundTasks.add(task);
try {
String findString = s.substring(5);
ArrayList<Task> foundTasks = new ArrayList<Task>();
for (Task task : t.getTasks()) {
if (task.toString().contains(findString)) {
foundTasks.add(task);
}
}
TaskList searchResults = new TaskList(foundTasks);
return "Here are the tasks I found!\n" + searchResults.displayTasks();
} catch (StringIndexOutOfBoundsException e) {
return "Incorrect command format.\nUse: find <keyword>";
}
TaskList searchResults = new TaskList(foundTasks);
return "Here are the tasks I found!\n" + searchResults.displayTasks();

}
}

0 comments on commit 7b4bf29

Please sign in to comment.