Skip to content

Commit

Permalink
Level-9: Add find function
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaeliaaa committed Aug 25, 2020
1 parent 1f81aae commit fbecf99
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions data/listOfTasks.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
T | 1 | hey
E | 1 | hey | 2020-09-09
D | 0 | meeting agenda | 2020-08-26
E | 0 | meeting | 2020-08-27
5 changes: 5 additions & 0 deletions src/main/java/duke/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public static void handleCommands(String input, DukeCommandType commandType, Tas
case LIST:
tasks.getListOfTasks();
break;
case FIND:
String keyword = input.split(" ")[1];
System.out.println(keyword);
tasks.findTasks(keyword);
break;
case DONE:
try {
int index = Integer.parseInt(input.split(" ")[1]);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/DukeCommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum DukeCommandType {
DEADLINE,
EVENT,
LIST,
FIND,
DONE,
DELETE,
HELP,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public String toString(){
case UNKNOWN:
error += " I DON'T KNOW WHAT YOU MEAN :-(\n";
break;
case NO_MATCHING_FOUND:
error += " NO MATCHES FOUND :-(\n";
break;
case WRONG_FORMAT:
switch (commandType) {
case DEADLINE:
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/DukeExceptionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum DukeExceptionType {
MISSING_DESCRIPTION,
MISSING_TIMING,
INVALID_INDEX,
NO_MATCHING_FOUND,
WRONG_FORMAT,
UNKNOWN,
}
2 changes: 2 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static void parseInputs(TaskList tasks) throws DukeException {
CommandHandler.handleCommands(input, DukeCommandType.DELETE, tasks);
input = sc.nextLine();
continue;
} else if (input.startsWith("find")) {
CommandHandler.handleCommands(input, DukeCommandType.FIND, tasks);
} else {
CommandHandler.handleCommands(input, DukeCommandType.UNKNOWN, tasks);
input = sc.nextLine();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public void markAsDone() {
this.isDone = true;
}

/**
* Gets description of the task.
*
* @return
*/
public String getDescription() {
return this.description;
}

/**
* Returns string format of task.
*
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,30 @@ public static void delete(Integer index) {
+ "\n ____________________________________________________________\n";
System.out.println(str);
}

public static void findTasks(String keyword) {
ArrayList<Task> matchingTasks = new ArrayList<>();
for (Task task: tasks) {
if (task.getDescription().contains(keyword)) {
matchingTasks.add(task);
}
}
if (matchingTasks.isEmpty()) {
try {
throw new DukeException("", DukeExceptionType.NO_MATCHING_FOUND);
} catch (DukeException e) {
System.err.println(e);
}
} else {
int index = 1;
String str = " ____________________________________________________________"
+ "\n Here are the matching tasks in your list:";
for (Task task : matchingTasks) {
str += "\n " + index + ". " + task;
index++;
}
str += "\n ____________________________________________________________\n";
System.out.println(str);
}
}
}

0 comments on commit fbecf99

Please sign in to comment.