Skip to content

Commit

Permalink
Implement find function (level 9)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdiMangalam committed Oct 11, 2024
1 parent bbb61c9 commit 94d3b6e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/main/java/Flash.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ public static void main(String[] args) {
if (command.equalsIgnoreCase("bye")) {
UI.displayByeMessage();
break;
} else if (command.equalsIgnoreCase("list")) {
} else if (command.equals("list")) {
UI.displayTasks(taskList.tasks);
} else if (command.equalsIgnoreCase("mark")) {
} else if (command.equals("mark")) {
taskList.markTask(input);
storage.save(taskList.tasks);
} else if (command.equalsIgnoreCase("unmark")) {
} else if (command.equals("unmark")) {
taskList.unMarkTask(input);
storage.save(taskList.tasks);
} else if (command.equalsIgnoreCase("todo")) {
} else if (command.equals("todo")) {
taskList.addTodo(input);
storage.save(taskList.tasks);
} else if (command.equalsIgnoreCase("deadline")) {
} else if (command.equals("deadline")) {
taskList.addDeadline(input);
storage.save(taskList.tasks);
} else if (command.equalsIgnoreCase("event")) {
} else if (command.equals("event")) {
taskList.addEvent(input);
} else if (command.equalsIgnoreCase("delete")) {
} else if (command.equals("delete")) {
taskList.deleteTask(input);
storage.save(taskList.tasks);
} else if (command.equals("find")) {
taskList.listMatchedTasks(input);
} else {
throw new FlashException("Uh-oh! I don't know what that means.");
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public static String[] parseEvent(String input) {
String to = parts[2].trim();
return new String[]{description, from, to};
}

public static String parseKeyword(String input) {
String[] parts = input.split(" ", 2);
return parts[1];
}
}
22 changes: 20 additions & 2 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@
import java.util.List;
import java.util.ArrayList;

/**
* Handles loading and saving tasks to a file.
*/
public class Storage {
private String filePath;

/**
* Constructs a Storage object with the specified file path.
*
* @param filePath the path to the file where tasks are stored
*/
public Storage(String filePath) {
this.filePath = filePath;
}

// Method to load tasks from file
/**
* Loads tasks from the specified file.
*
* @return a list of tasks loaded from the file
* @throws FlashException if an error occurs during file operations or if the file format is corrupted
*/
public List<Task> load() throws FlashException {
List<Task> tasks = new ArrayList<>();
File file = new File(filePath);
Expand Down Expand Up @@ -69,7 +82,12 @@ public List<Task> load() throws FlashException {
return tasks;
}

// Method to save tasks to file
/**
* Saves the specified tasks to the file.
*
* @param tasks the list of tasks to save
* @throws FlashException if an error occurs during file operations
*/
public void save(List<Task> tasks) throws FlashException {
try (FileWriter fw = new FileWriter(filePath)) {
for (Task task : tasks) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,20 @@ public void markTask(String input) throws FlashException {
throw new FlashException("Invalid task number. Please enter a valid task number.");
}
}

public void listMatchedTasks(String input) throws FlashException {
try{
String keyword = Parser.parseKeyword(input);
int index = 1;
List<Task> matchedTasks = new ArrayList<>();
for (Task task : this.tasks) {
if(task.getDescription().contains(keyword)) {
matchedTasks.add(task);
}
}
UI.displayMatchedTasks(matchedTasks);
} catch (Exception e) {
throw new FlashException("Invalid Keyword");
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class UI {
public static void displayTasks(List<Task> tasks) {
System.out.println("____________________________________________________________");
System.out.println("Here are the tasks in you list:");
for(int i = 0; i < tasks.size(); i++) {
System.out.println(i+1 + "." + tasks.get(i));
}
Expand Down Expand Up @@ -62,4 +63,13 @@ static void displayByeMessage() {
System.out.println("Bye. Hope to see you again soon!");
System.out.println("____________________________________________________________");
}

public static void displayMatchedTasks(List<Task> matchedTasks) {
System.out.println("____________________________________________________________");
System.out.println("Here are the matching tasks in you list:");
for(int i = 0; i < matchedTasks.size(); i++) {
System.out.println(i+1 + "." + matchedTasks.get(i));
}
System.out.println("____________________________________________________________");
}
}
5 changes: 5 additions & 0 deletions src/main/java/exception/FlashException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package exception;

/**
* Signals an error specific to the Flash application.
*
* @param message should provide details about the error.
*/
public class FlashException extends Exception {
public FlashException(String message) {
super(message);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public String getIcon() {
}
}

public String getDescription() {
return this.description;
}

public void markDone() {
this.isDone = true;
}
Expand Down

0 comments on commit 94d3b6e

Please sign in to comment.