Skip to content

Commit

Permalink
Merge pull request #3 from yongmingyang/branch-Level-9
Browse files Browse the repository at this point in the history
Level-9
  • Loading branch information
yongmingyang committed Aug 25, 2020
2 parents be6713d + 02e75f9 commit 15a7277
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main.java;

import java.io.IOException;
import java.util.ArrayList;

/**
* The command class is used to handle and execute commands
* after input by user is processed by the parser.
*/

public class Command {
private String task;
private String action;
Expand Down Expand Up @@ -58,6 +57,23 @@ public void execute(TaskList taskList, Ui ui) throws DukeException {
} else if (action.equals("event")) {
taskList.addEvent(task);
ui.showAdded();
} else if (action.equals("find")) {
String[] split = task.split("find ");
String toFind = split[1];
ArrayList<Task> list = taskList.getList();
ArrayList<Task> filtered = new ArrayList<>();
if(list.size() == 0) {
System.out.println("You do not have any tasks yet");
} else {
System.out.println("Here are the tasks that matches '" + toFind + "'");
for (int i = 0; i < list.size(); i++) {
String task = list.get(i).toString();
if (task.contains(toFind)) {
filtered.add(list.get(i));
}
}
ui.showList(filtered);
}
} else {
ui.showInvalidCommand();
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public void run(){
Command nextCommand = parser.interpret(input);
nextCommand.execute(taskList, ui);
isEnd = nextCommand.isEnd();
} catch (IOException e) {
ui.showError(e);
} catch (DukeException e) {
ui.showError(e);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public Command interpret (String line) {
return new Command(line, "event");
} else if (line.indexOf("done") == 0 || line.equals("bye") || line.equals("list")) {
return new Command(line, line);
} else if (line.indexOf("find") == 0) {
return new Command(line, "find");
} else {
return new Command(line, null);
}
Expand Down

0 comments on commit 15a7277

Please sign in to comment.