Skip to content

Commit

Permalink
Give users a way to find a task by searching for a keyword. Also, cha…
Browse files Browse the repository at this point in the history
…nge command to be abstract.
  • Loading branch information
JTWang2000 committed Feb 26, 2020
1 parent 4116a14 commit 0749817
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 44 deletions.
24 changes: 0 additions & 24 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,10 @@ public void run() {
ui.showLine();
}
}
ui.showGoodByeMessage();
ui.showLine();
}

public static void main(String[] args) {
new Duke(FILE_PATH).run();
}


/*public static void loadTasks() {
File f = new File(FILE_PATH);
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
String oneTask;
oneTask = s.nextLine();
loadATask(oneTask);
}
} catch (FileNotFoundException e) {
System.out.println("\tCreating back up file in hard disk... ");
System.out.println();
try {
f.createNewFile();
} catch (IOException error) {
System.out.println("\tSomething went wrong: " + error.getMessage());
}
}
}*/
}

6 changes: 2 additions & 4 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

import java.io.IOException;

public class Command {
public abstract class Command {
public Command(){

}

public void execute (TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {

}
public abstract void execute (TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException;

public Boolean isExit() {
return false;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/duke/command/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package duke.command;

import duke.common.DukeException;
import duke.storage.Storage;
import duke.taskList.TaskList;
import duke.ui.Ui;

import java.io.IOException;

public class ExitCommand extends Command{
public ExitCommand() {

Expand All @@ -9,4 +16,8 @@ public ExitCommand() {
public Boolean isExit() {
return true;
}

public void execute (TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
ui.showGoodByeMessage();
}
}
29 changes: 29 additions & 0 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package duke.command;

import duke.common.DukeException;
import duke.storage.Storage;
import duke.taskList.TaskList;
import duke.taskList.task.Task;
import duke.ui.Ui;

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

public class FindCommand extends Command{
private static String findString;
private static ArrayList<Integer> findCount;

public FindCommand (String findString) {
this.findString = findString;
findCount = new ArrayList<>();
}

public void execute (TaskList tasks, Ui ui, Storage storage) throws IOException, DukeException {
for(int i = 0; i<tasks.size(); i++) {
if(tasks.getATask(i).getDescription().contains(findString)) {
findCount.add(i);
}
}
ui.showFindTask(tasks,findCount);
}
}
1 change: 1 addition & 0 deletions src/main/java/duke/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public final class Constants {
public static final String TODO = "todo";
public static final String DEADLINE = "deadline";
public static final String EVENT = "event";
public static final String FIND = "find";

public static final String LINE_BREAK = "\t____________________________________________________________";

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ public static Command parse(String fullCommand) throws DukeException{
}
int deleteCount = Integer.parseInt(responses[1]);
return new DeleteCommand(deleteCount);
case FIND:
if (responses.length < 2 ) {
throw new DukeException("\tPlease input the thing you want to find");
}
return new FindCommand(responses[1]);
case LIST:
return new ListCommand();

case BYE:
return new ExitCommand();
default:
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/duke/taskList/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public static Task deleteTask(int deleteCount) {
return tasks.remove(deleteCount);
}

public ArrayList<Task> getTaskList() {
return this.tasks;
}

public Task getATask(int taskCount) {
return this.tasks.get(taskCount);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import duke.taskList.TaskList;
import duke.taskList.task.Task;

import java.util.ArrayList;
import java.util.Scanner;

import static duke.common.Constants.LINE_BREAK;
Expand Down Expand Up @@ -77,6 +78,17 @@ public void showAddTaskMessage(TaskList tasks) {
System.out.println("\tNow you have " + tasks.size() + " tasks in the list");
}

public void showFindTask (TaskList tasks, ArrayList<Integer> findCount) {
if (findCount.size() > 0) {
System.out.println("\tHere are the matching tasks in your list:");
for (Integer i : findCount) {
System.out.println("\t" + (i + 1) + "." + tasks.getATask(i));
}
} else {
System.out.println("\t No matching tasks.");
}
}

public void showLoadingError() {
System.out.println("\tCreating back up file in the hard disk...");
}
Expand Down
33 changes: 28 additions & 5 deletions text-ui-test/ACTUAL.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Hello from
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[T][ ] eat
[T][ ] read book
Now you have 1 tasks in the list
____________________________________________________________
____________________________________________________________
Expand All @@ -44,9 +44,25 @@ Hello from
Now you have 4 tasks in the list
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[D][ ] book review (by: Friday)
Now you have 5 tasks in the list
____________________________________________________________
____________________________________________________________
The description of a event cannot be empty.
____________________________________________________________
____________________________________________________________
Here are the matching tasks in your list:
1.[T][ ] read book
5.[D][ ] book review (by: Friday)
____________________________________________________________
____________________________________________________________
Please input the thing you want to find
____________________________________________________________
____________________________________________________________
No matching tasks.
____________________________________________________________
____________________________________________________________
Nice! I've marked this task as done:
yes! sleep
____________________________________________________________
Expand All @@ -55,23 +71,30 @@ Hello from
yes! cs2113
____________________________________________________________
____________________________________________________________
There is no task 10. Please reconsider the index.
____________________________________________________________
____________________________________________________________
Here are the tasks in your list:
1.[T][ ] eat
1.[T][ ] read book
2.[T][/] sleep
3.[D][/] cs2113 (by: Tuesday)
4.[E][ ] project meeting (at: Mon 2-4pm)
5.[D][ ] book review (by: Friday)
____________________________________________________________
____________________________________________________________
Noted. I've removed this task:
[T][/] sleep
Now you have 3 tasks in the list.
Now you have 4 tasks in the list.
____________________________________________________________
____________________________________________________________
Please input a task number that you want to delete
____________________________________________________________
____________________________________________________________
Here are the tasks in your list:
1.[T][ ] eat
1.[T][ ] read book
2.[D][/] cs2113 (by: Tuesday)
3.[E][ ] project meeting (at: Mon 2-4pm)
____________________________________________________________
4.[D][ ] book review (by: Friday)
____________________________________________________________
____________________________________________________________
Bye. Hope to see you again soon!
Expand Down
33 changes: 28 additions & 5 deletions text-ui-test/EXPECTED.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Hello from
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[T][ ] eat
[T][ ] read book
Now you have 1 tasks in the list
____________________________________________________________
____________________________________________________________
Expand All @@ -44,9 +44,25 @@ Hello from
Now you have 4 tasks in the list
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[D][ ] book review (by: Friday)
Now you have 5 tasks in the list
____________________________________________________________
____________________________________________________________
The description of a event cannot be empty.
____________________________________________________________
____________________________________________________________
Here are the matching tasks in your list:
1.[T][ ] read book
5.[D][ ] book review (by: Friday)
____________________________________________________________
____________________________________________________________
Please input the thing you want to find
____________________________________________________________
____________________________________________________________
No matching tasks.
____________________________________________________________
____________________________________________________________
Nice! I've marked this task as done:
yes! sleep
____________________________________________________________
Expand All @@ -55,23 +71,30 @@ Hello from
yes! cs2113
____________________________________________________________
____________________________________________________________
There is no task 10. Please reconsider the index.
____________________________________________________________
____________________________________________________________
Here are the tasks in your list:
1.[T][ ] eat
1.[T][ ] read book
2.[T][/] sleep
3.[D][/] cs2113 (by: Tuesday)
4.[E][ ] project meeting (at: Mon 2-4pm)
5.[D][ ] book review (by: Friday)
____________________________________________________________
____________________________________________________________
Noted. I've removed this task:
[T][/] sleep
Now you have 3 tasks in the list.
Now you have 4 tasks in the list.
____________________________________________________________
____________________________________________________________
Please input a task number that you want to delete
____________________________________________________________
____________________________________________________________
Here are the tasks in your list:
1.[T][ ] eat
1.[T][ ] read book
2.[D][/] cs2113 (by: Tuesday)
3.[E][ ] project meeting (at: Mon 2-4pm)
____________________________________________________________
4.[D][ ] book review (by: Friday)
____________________________________________________________
____________________________________________________________
Bye. Hope to see you again soon!
Expand Down
3 changes: 2 additions & 1 deletion text-ui-test/data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
T | 0 | eat
T | 0 | read book
D | 1 | cs2113 | Tuesday
E | 0 | project meeting | Mon 2-4pm
D | 0 | book review | Friday
8 changes: 7 additions & 1 deletion text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
done
list
todo eat
todo read book
todo sleep
todo
heyhey
deadline cs2113 /by Tuesday
event project meeting /at Mon 2-4pm
deadline book review /by Friday
event
find book
find
find exam
done 2
done 3
done 10
list
delete 2
delete
list
bye

0 comments on commit 0749817

Please sign in to comment.