Skip to content

Commit

Permalink
Merge pull request #3 from NGXZS/branch-Level-9
Browse files Browse the repository at this point in the history
Level 9: Add find function
  • Loading branch information
NGXZS authored Mar 7, 2024
2 parents 8347c38 + f0bc768 commit f7e7ef8
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 38 deletions.
23 changes: 12 additions & 11 deletions saveFile.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
1. [T][1] item2
1. [T][0] item2
2. [T][0] item5
3. [D][1] item6 (by: sfadsfs)
4. [T][0] item2
5. [T][1] item6
6. [D][0] item7 (by: efs)
7. [D][0] kjdfl (by: sdf)
8. [D][0] sdf(by: sdf sd /fds)
9. [D][0] something (by: by 2/12/2019)
10. [D][0] something (by: 2/12/2019)
11. [D][0] haha (by: 2/12/2019)
12. [D][0] halo (by: Dec 11 2019)
3. [T][0] item2
4. [T][1] item6
5. [D][0] item7 (by: efs)
6. [D][0] kjdfl (by: sdf)
7. [D][0] sdf(by: sdf sd /fds)
8. [D][0] something (by: by 2/12/2019)
9. [D][0] something (by: 2/12/2019)
10. [D][1] haha (by: 2/12/2019)
11. [D][1] halo (by: Dec 11 2019)
12. [T][0] testing
13. [D][0] someting (by: Aug 22 2014)
2 changes: 1 addition & 1 deletion src/main/java/RecrBad.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import helperFunctions.TaskList;
import helperFunctions.Ui;

public class RecrBad { //TODO more OOP
public class RecrBad {
private TaskList tasks;
public RecrBad(String FILE_PATH) {
tasks = new TaskList();
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/helperFunctions/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
import java.time.format.DateTimeFormatter;

public class Parser {
public static boolean processUserInput(TaskList tasks, String line, String FILE_PATH, boolean isReadMode) throws InvalidParamsException {
String[] req = line.split(" ");
public static boolean processUserInput(TaskList tasks, String userInput, String FILE_PATH, boolean isReadMode) throws InvalidParamsException {
String[] req = userInput.split(" ");

if (req[0].equalsIgnoreCase("BYE")) {
return false; // EXITS loop
}
if (req[0].equalsIgnoreCase("LIST")) {
System.out.print(tasks.displayList());
} else if (req[0].toUpperCase().contains("MARK")) { // both unmark & mark contains 'mark'
boolean isMark = !line.toUpperCase().contains("UNMARK");
boolean isMark = !userInput.toUpperCase().contains("UNMARK");
tasks.markOperation(req, isMark, FILE_PATH, isReadMode);
} else if (req[0].equalsIgnoreCase("DELETE")) {
tasks.deleteOperation(req, FILE_PATH);
} else if (req[0].equalsIgnoreCase("FIND")) {
tasks.findOperation(req, FILE_PATH);
} else if (req[0].equalsIgnoreCase("TODO")) {
tasks.addTodoTask(req, line, FILE_PATH, isReadMode); // change tasks to ArrayList<TAsk>, f void now
tasks.addTodoTask(req, userInput, FILE_PATH, isReadMode); // change tasks to ArrayList<TAsk>, f void now
} else if (req[0].equalsIgnoreCase("DEADLINE")) {
tasks.addDeadlineTask(req, line, FILE_PATH, isReadMode);
tasks.addDeadlineTask(req, userInput, FILE_PATH, isReadMode);
} else if (req[0].equalsIgnoreCase("EVENT")) {
tasks.addEventTask(req, line, FILE_PATH, isReadMode);
tasks.addEventTask(req, userInput, FILE_PATH, isReadMode);
} else {
throw new InvalidParamsException("No such command." + System.lineSeparator() + Ui.printCommandsList());
}
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/helperFunctions/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected String displayList() {
*
* @param index index of specific task
*/
private String displayListItem(int index) { // change from void to string
private String displayListItem(int index) {
return ((index + 1) + ". [" + tasks.get(index).getType() + "]["
+ tasks.get(index).getStatus() + "] "
+ tasks.get(index).getDescription()
Expand All @@ -60,7 +60,7 @@ protected void addEventTask(String[] req, String line, String FILE_PATH, boolean
String timeRange = " (from: " + line.substring(indexFrom + 1, indexTo) +
"to " + line.substring(indexTo + 1) + ")";
// add to tasks
Task newTask = new Event(description, timeRange);
Task newTask = new Event(description, timeRange, tasks.size() + 1);
tasks.add(newTask);
if (isReadMode) {
return; // no need execute code below (for writing only)
Expand Down Expand Up @@ -92,7 +92,7 @@ protected void addDeadlineTask(String[] req, String line, String FILE_PATH, bool
deadline += ")";
String description = line.substring(startIndexOfDescription, indexDeadline);
// add to tasks
Task newTask = new Deadline(description, deadline);
Task newTask = new Deadline(description, deadline, tasks.size() + 1);
tasks.add(newTask);

if (isReadMode) {
Expand All @@ -114,7 +114,7 @@ protected void addTodoTask(String[] req, String line, String FILE_PATH, boolean
// process input
String description = line.substring(startIndexOfDescription);
// add to tasks
Task newTask = new Todo(description);
Task newTask = new Todo(description, tasks.size() + 1);
tasks.add(newTask);
if (isReadMode) {
// no need execute code below (for writing only)
Expand Down Expand Up @@ -157,7 +157,7 @@ protected void deleteOperation(String[] req, String FILE_PATH) throws InvalidPar
* @param req String[] input from user
* @param isMark type of operation: mark or unmark
*/
protected void markOperation( String[] req, boolean isMark, String FILE_PATH, boolean isReadMode) throws InvalidParamsException {
protected void markOperation(String[] req, boolean isMark, String FILE_PATH, boolean isReadMode) throws InvalidParamsException {
// check for input validity
if (req.length < 2) {
throw new InvalidParamsException("invalid mark/ unmark operation");
Expand Down Expand Up @@ -188,4 +188,23 @@ protected void markOperation( String[] req, boolean isMark, String FILE_PATH, bo
System.out.println("Has " + mark + " task" + taskNum + ":");
System.out.print(displayListItem(taskNum - 1));
}

public void findOperation(String[] req, String filePath) throws InvalidParamsException {
// check for input validity
if (req.length < 2) {
throw new InvalidParamsException("invalid find operation");
}
String findKeyword = req[1];
String tasksToPrint = "";
for (Task task : tasks) {
if (task.getDescription().contains(findKeyword)) {
tasksToPrint += displayListItem(task.getTaskNum() - 1);
}
}
if (tasksToPrint.isEmpty()) {
System.out.println("No tasks found");
return;
}
System.out.print(tasksToPrint);
}
}
4 changes: 2 additions & 2 deletions src/main/java/helperFunctions/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public static void readInput(TaskList tasks, String FILE_PATH) {

while (isRun) {
printLine();
String line = in.nextLine(); // reads input
String userInput = in.nextLine(); // reads input
// process input
try {
boolean isReadMode = false;
isRun = Parser.processUserInput(tasks, line, FILE_PATH, isReadMode);
isRun = Parser.processUserInput(tasks, userInput, FILE_PATH, isReadMode);
} catch (InvalidParamsException e) {
showLoadingError(e.getMessage()); // prints out error message
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public class Deadline extends Task{
protected String by;

public Deadline(String description, String by){
super(description + by);
public Deadline(String description, String by, int taskNum){
super(description + by, taskNum);
this.type = 'D';
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public class Event extends Task{
protected String timeRange;

public Event(String description, String timeRange){
super(description + timeRange);
public Event(String description, String timeRange, int taskNum){
super(description + timeRange, taskNum);
this.type = 'E';
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ public class Task {
protected boolean isDone;
protected char type;

public Task(String description){
protected int taskNum;

public Task(String description, int taskNum){
this.description = description;
this.isDone = false;
this.type = ' ';
this.taskNum = taskNum;
}

public String getDescription(){
Expand All @@ -26,5 +29,7 @@ public void markAsNotDone(){
public char getType(){
return type;
}

public int getTaskNum() {
return taskNum;
}
}
12 changes: 5 additions & 7 deletions src/main/java/tasks/Todo.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package tasks;

public class Todo extends Task{
public class Todo extends Task {

public Todo(String description){
super(description);
public Todo(String description, int taskNum) {
super(description, taskNum);
this.type = 'T';
}

@Override
public char getType(){
return this.type;
return type;
}


}

0 comments on commit f7e7ef8

Please sign in to comment.