Skip to content

Commit

Permalink
Merge branch 'branch-Level-9'
Browse files Browse the repository at this point in the history
  • Loading branch information
gachia committed Sep 1, 2019
2 parents 8f4ddda + aad6c5d commit 8ba9c55
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Represents the Command for searching Tasks with a specified keyword.
* A sub-class of Command.
*/
public class FindCommand extends Command {

/**
* Overridden execute method from Command to search for Task objects whose
* description matches the search term. The method will output all Tasks that
* contain the search term in the description. It will throw an exception if
* the search term is empty.
* @param storage Storage object for saving purposes
* @param tasks Contains the list of tasks
* @param ui Holds Ui printing method and user input field
* @throws DukeException If searchTerm is empty
*/
@Override
public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException {
String searchTerm = ui.readDesc().trim();
if(searchTerm.isEmpty()){
throw new DukeException("Search term cannot be blank.");
}
tasks.searchTaskList(searchTerm);
}
}
2 changes: 2 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static Command parse(String userCmd) throws DukeException {
return new DoneCommand();
case "delete":
return new DeleteCommand();
case "find":
return new FindCommand();
case "exit":
return new ExitCommand();
case "todo":
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public boolean getIsDone() {
return isDone;
}

/**
* Returns the Task's description
* @return description of Task
*/
public String getDescription() {
return description;
}

/**
* Returns the Task object in a String format for saving into a text file.
* @return description and status of Task for saving
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ public void showTaskList() {
}
}

/**
* Prints out the Tasks whose description matches the search term
* @param searchTerm Keyword by user for search
*/
public void searchTaskList(String searchTerm) {
System.out.println("Here are the matching tasks in your list:");
int noOfSearches = 1;
for(int i = 0; i < list.size(); i++){
if(list.get(i).getDescription().contains(searchTerm)){
System.out.println((noOfSearches) + "." + list.get(i));
noOfSearches += 1;
}
}
}

/**
* Sets boolean variable isDone of selected Task object to true.
* @param doneIndex Index of selected Task object, 1-based index
Expand Down

0 comments on commit 8ba9c55

Please sign in to comment.