Skip to content

Commit

Permalink
Merge branch 'branch-Level-9'
Browse files Browse the repository at this point in the history
  • Loading branch information
minzzelo committed Aug 22, 2020
2 parents f6f5e43 + 902e661 commit db6981d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
T | 0 | borrow book
D | 0 | return book | 06-06-2020 11:00
16 changes: 15 additions & 1 deletion src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Parser {
* @throws DukeInvalidDateTimeException If date and time input is invalid.
* @throws DukeInvalidCommandException If command is invalid.
*/
static Command parse(String fullCommand) throws DukeEmptyInputException, DukeInvalidDateTimeException, DukeInvalidCommandException {
static Command parse(String fullCommand) throws DukeEmptyInputException, DukeInvalidDateTimeException, DukeInvalidCommandException, DukeInvalidKeywordException {
String[] commandArr = fullCommand.trim().split(" ", 2);
switch(commandArr[0]) {
case "bye":
Expand All @@ -30,6 +30,11 @@ static Command parse(String fullCommand) throws DukeEmptyInputException, DukeInv
return parseDone(Integer.parseInt(commandArr[1]));
case "delete":
return parseDelete(Integer.parseInt(commandArr[1]));
case "find":
if(commandArr.length < 2) {
throw new DukeInvalidKeywordException();
}
return parseFind(commandArr[1]);
case "todo":
if(commandArr.length < 2) {
throw new DukeEmptyInputException("The description of a todo cannot be empty.");
Expand Down Expand Up @@ -84,6 +89,15 @@ static DeleteCommand parseDelete(int taskNo) {
return new DeleteCommand(taskNo);
}

/**
* Parses an FindCommand associated with a keyword.
* @param keyword Search keyword.
* @return Returns an FindCommand associated with the search keyword.
*/
static FindCommand parseFind(String keyword) {
return new FindCommand(keyword);
}

/**
* Parses an AddCommand associated with a Todo.
* @param description Description of the Todo.
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public Task deleteTask(int taskNo) {
return task;
}

/**
* Finds the tasks related to keyword.
* @param keyword Keyword used to find the related tasks.
* @return Returns a list of related tasks.
*/
public List<Task> findTasks(String keyword) {
List<Task> relatedTasks = new ArrayList<>();
for(Task task : this.tasks) {
if(task.description.contains(keyword)) {
relatedTasks.add(task);
}
}
return relatedTasks;
}

/**
* Retrieves a string describing the task size.
* @return Returns the string.
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke;

import java.util.List;
import java.util.Scanner;
import duke.task.*;

Expand Down Expand Up @@ -50,12 +51,30 @@ public void list(TaskList taskList) {
System.out.println("Here are the tasks in your list:");
for (int i = 1; i <= taskList.size(); i++) {
Task task = taskList.get(i - 1);
String message = String.valueOf(i) + ".";
String message = i + ".";
message += task;
System.out.println(message);
}
}

/**
* Parses the list of tasks and prints the search result.
* @param searchResult Search result for task list.
*/
public void printSearchResult(List<Task> searchResult) {
if(searchResult.size() < 1) {
System.out.println("There are no matching results!");
} else {
System.out.println("Here are the matching tasks in your list:");
for (int i = 1; i <= searchResult.size(); i++) {
Task task = searchResult.get(i - 1);
String message = i + ".";
message += task;
System.out.println(message);
}
}
}

/**
* Read the user input.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package duke.command;

import duke.*;
import duke.task.*;

import java.util.List;

public class FindCommand extends Command {
private String keyword;

public FindCommand(String keyword) {
this.keyword = keyword;
}

public void execute(TaskList taskList, Ui ui, Storage storage) {
List<Task> relatedTasks = taskList.findTasks(keyword);
ui.printSearchResult(relatedTasks);
}
}
7 changes: 7 additions & 0 deletions src/main/java/duke/exception/DukeInvalidKeywordException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package duke.exception;

public class DukeInvalidKeywordException extends DukeException{
public DukeInvalidKeywordException() {
super("Please enter a valid keyword!");
}
}

0 comments on commit db6981d

Please sign in to comment.