Skip to content

Commit

Permalink
Merge branch-Level-9 with master
Browse files Browse the repository at this point in the history
  • Loading branch information
Sampy147 committed Sep 12, 2022
2 parents ab7eb48 + 52505dc commit ecbc9fd
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 3 deletions.
Empty file removed duke.txt
Empty file.
1 change: 1 addition & 0 deletions src/main/java/duke/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Message {
public static final String FILE_NOT_FOUND = "The memory file cannot be found.";
public static final String FILE_READ_ERROR = "There is an error when reading the memory file.";
public static final String FILE_CREATE_ERROR = "There is an error when creating the memory file";
public static final String INVALID_FIND_TASK_FORMAT = "To find a task, please input this format: find {Keyword}";

/**
* Returns a String that describes that the task does not exist within the specified tasklist
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public boolean isExit() {
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
ui.showBye();
}

}
1 change: 1 addition & 0 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public abstract class Command {
* @throws DukeException if an exception is thrown when the command is executed
*/
public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException;

}
27 changes: 27 additions & 0 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package duke.command;

import duke.DukeException;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;

public class FindCommand extends Command {

private String keyword;

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

@Override
public boolean isExit() {
return false;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
String foundTasks = tasks.findTasks(this.keyword);
ui.showFound(foundTasks);
}

}
18 changes: 16 additions & 2 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public static Command parse(String fullCommand) throws DukeException {
return addEvent(fullCommand);
} else if (command.equals("todo")) {
return addToDo(fullCommand);
} else if (command.equals("delete")){
} else if (command.equals("delete")) {
return deleteTask(fullCommand);
} else if (command.equals("find")) {
return findTask(fullCommand);
}
throw new DukeException(Message.INVALID_USER_INPUT);
}
Expand All @@ -51,9 +53,12 @@ private static AddCommand addDeadline(String input) throws DukeException {
String description = stringArray[0].strip();
String by = stringArray[1].strip();
LocalDate deadlineDate = LocalDate.parse(by);
if (deadlineDate.isBefore(LocalDate.now()) || description.equals("")) {
if (deadlineDate.isBefore(LocalDate.now())) {
throw new DukeException(Message.INVALID_DATE_INPUT);
}
if (description.equals("")) {
throw new DukeException(Message.INVALID_DEADLINE_INPUT);
}
Deadline newDeadline = new Deadline(description, deadlineDate);
return new AddCommand(newDeadline);
} catch (ArrayIndexOutOfBoundsException e) {
Expand Down Expand Up @@ -108,4 +113,13 @@ private static DeleteCommand deleteTask(String command) throws DukeException {
}
}

private static FindCommand findTask(String command) throws DukeException {
String[] commandList = command.strip().split(" ");
try {
return new FindCommand(commandList[1]);
} catch (IndexOutOfBoundsException e) {
throw new DukeException(Message.INVALID_FIND_TASK_FORMAT);
}
}

}
9 changes: 9 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public void unmark() {
this.isDone = false;
}

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

/**
* Returns a string representation of this task
*
Expand Down
30 changes: 29 additions & 1 deletion src/main/java/duke/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void unmarkTaskAtPos(int position) throws IndexOutOfBoundsException{
*
* @param position the index of the task in the list of tasks
* @return the deleted task
* @throws IndexOutOfBoundsException the deleted task if given position > length of list of tasks or given position < 1
* @throws IndexOutOfBoundsException the deleted task if given position > length of list of tasks
* or given position < 1
*/
public Task deleteTaskAtPos(int position) throws IndexOutOfBoundsException {
Task deletedTask = getTask(position);
Expand All @@ -94,6 +95,33 @@ public Task deleteTaskAtPos(int position) throws IndexOutOfBoundsException {
return deletedTask;
}

/**
* Returns a string listing all the tasks found that include the keyword parameters given
* in their descriptions
*
* @param keyword the word used to find tasks that have this keyword in their descriptions
* @return a list of tasks containing the keyword
*/
public String findTasks(String keyword) {
String foundTasks = "";
int taskCount = 1;
for (Task task : this.taskArray) {
String description = task.getDescription();
String[] words = description.split(" ");
for (String word : words) {
if (word.equals(keyword)) {
if (taskCount == 1) {
foundTasks += taskCount + ". " + task;
} else {
foundTasks += "\n" + taskCount + ". " + task;
}
taskCount++;
}
}
}
return foundTasks;
}

/**
* Returns the simple string representation of the list of tasks
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ public void showDeleted(Task task) {
+ "\nNow you have " + this.tasks.getCount() + " tasks in the list.");
}

public void showFound(String foundTasks) {
showFullMessage("Here are the matching tasks in your list:\n" + foundTasks);
}

}

0 comments on commit ecbc9fd

Please sign in to comment.