diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 00000000..b8735a8a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: python.Python + diff --git a/src/main/java/python/parser/Command.java b/src/main/java/python/parser/Command.java index 4a1aefc0..15d5042a 100644 --- a/src/main/java/python/parser/Command.java +++ b/src/main/java/python/parser/Command.java @@ -9,6 +9,7 @@ public class Command { public static final String COMMAND_TODO = "todo"; public static final String COMMAND_DEADLINE = "deadline"; public static final String COMMAND_EVENT = "event"; + public static final String COMMAND_FIND = "find"; public static boolean isCommandBye(String command) { return (command.equals(COMMAND_BYE)); diff --git a/src/main/java/python/parser/Parser.java b/src/main/java/python/parser/Parser.java index eaca6c57..1fa0a771 100644 --- a/src/main/java/python/parser/Parser.java +++ b/src/main/java/python/parser/Parser.java @@ -13,6 +13,10 @@ public static int extractTaskNoFromInputLine(String inputLine) { return Integer.parseInt(inputLine.split(" ")[1]); } + public static String extractKeywordFromInputLine(String inputLine) { + return inputLine.split(" ")[1]; + } + public static String extractTodoDescFromInputLine(String inputLine) { return inputLine.split(" ", 2)[1]; } diff --git a/src/main/java/python/task/TaskList.java b/src/main/java/python/task/TaskList.java index 17b1714f..cf400bcd 100644 --- a/src/main/java/python/task/TaskList.java +++ b/src/main/java/python/task/TaskList.java @@ -34,4 +34,13 @@ public static void addTask(Task task) { tasks.add(task); } + public static List findTask(String keyword) { + List matchedTasks = new ArrayList<>(); + for (int taskNo = 0; taskNo < getNumberOfTasks(); taskNo++) { + if (getTask(taskNo).getDescription().contains(keyword)) { + matchedTasks.add(getTask(taskNo)); + } + } + return matchedTasks; + } } \ No newline at end of file diff --git a/src/main/java/python/ui/Message.java b/src/main/java/python/ui/Message.java index 6c48bbe4..4fb48a0e 100644 --- a/src/main/java/python/ui/Message.java +++ b/src/main/java/python/ui/Message.java @@ -5,6 +5,7 @@ public class Message { final static String MESSAGE_ASK = "What can I do for you?"; final static String MESSAGE_BYE = "Bye. See you again when you run the program again!"; final static String MESSAGE_INT_AFTER_COMMAND = "Command must be followed by an integer (task id)!"; + final static String MESSAGE_KEYWORD_AFTER_COMMAND = "Command must be followed by a keyword!"; final static String MESSAGE_DESC_AFTER_COMMAND = "Command must be followed by a task description!!"; final static String MESSAGE_TIME_AFTER_FROM_CLAUSE = "Command must have /from clause followed by time it starts!"; @@ -25,4 +26,6 @@ public class Message { final static String MESSAGE_NEW_TODO = "New Todo! You have added this todo:"; final static String MESSAGE_NEW_DEADLINE = "New Deadline! You have added this deadline:"; final static String MESSAGE_NEW_EVENT = "New Event! You have added this event:"; + final static String MESSAGE_NO_MATCH = "No matching tasks found!"; + final static String MESSAGE_MATCHES_FOUND = "Here are the matching tasks:"; } diff --git a/src/main/java/python/ui/Ui.java b/src/main/java/python/ui/Ui.java index ff063504..5c2ffc61 100644 --- a/src/main/java/python/ui/Ui.java +++ b/src/main/java/python/ui/Ui.java @@ -7,7 +7,9 @@ import python.task.Event; import python.task.TaskList; import python.task.Todo; +import python.task.Task; +import java.util.List; import java.util.Scanner; public class Ui { @@ -51,9 +53,9 @@ public void displayTaskCount() { addEmojiAndPrint("You have " + TaskList.getNumberOfTasks() + " tasks!"); } - public void displayTasks() { - for (int taskNo = 0; taskNo < TaskList.getNumberOfTasks(); taskNo++) { - System.out.printf("\t\t\t%d. %s\n", taskNo + 1, TaskList.getTask(taskNo)); + public void displayTasks(List tasks) { + for (int taskNo = 0; taskNo < tasks.size(); taskNo++) { + System.out.printf("\t\t\t%d. %s\n", taskNo + 1, tasks.get(taskNo)); } } @@ -75,7 +77,7 @@ private void handleByeCommand() { private void handleListCommand() { displayTaskCount(); - displayTasks(); + displayTasks(TaskList.getTasks()); } private void handleMarkCommand() throws PythonException { @@ -224,6 +226,23 @@ private void handleEventCommand() throws PythonException { displayTaskCount(); } + private void handleFindCommand() throws PythonException { + String keyword; + try { + keyword = Parser.extractKeywordFromInputLine(inputLine); + } catch (ArrayIndexOutOfBoundsException e) { + throw new PythonException(Message.MESSAGE_KEYWORD_AFTER_COMMAND); + } + + List matchedTasks = TaskList.findTask(keyword); + + if (matchedTasks.isEmpty()) addEmojiAndPrint(Message.MESSAGE_NO_MATCH); + else { + addEmojiAndPrint(Message.MESSAGE_MATCHES_FOUND); + displayTasks(matchedTasks); + } + } + private void handleUnknownCommand() throws PythonException { if (inputLine.isEmpty()) { addEmojiAndPrint(Message.MESSAGE_EMPTY_COMMAND_JOKE); @@ -260,6 +279,9 @@ public void executeLine() throws PythonException { case Command.COMMAND_EVENT: handleEventCommand(); break; + case Command.COMMAND_FIND: + handleFindCommand(); + break; default: handleUnknownCommand(); break; diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 3631c6c3..7df3c0b5 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -101,6 +101,15 @@ 🐍: [E][ ] OOP Lecture (from: 12pm to: 2pm) 🐍: You have 8 tasks! ———————————————————————————————————————————————————————————————————————————————— + 🐍: Here are the matching tasks: + 1. [T][ ] borrow book + 2. [D][X] return book (by: Sunday) + 3. [D][ ] return book (by: 2 June) + ———————————————————————————————————————————————————————————————————————————————— + Error: Command must be followed by a keyword! + ———————————————————————————————————————————————————————————————————————————————— + 🐍: No matching tasks found! + ———————————————————————————————————————————————————————————————————————————————— 🐍: You have 8 tasks! 1. [T][ ] borrow book 2. [T][X] borrow cycle diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 5095ae96..bcbbb841 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -28,6 +28,9 @@ event OOP Lecture event OOP Lecture /from 12pm event OOP Lecture /from /to 2pm event OOP Lecture /from 12pm /to 2pm +find book +find +find exam list unmark 3 unmark 4