Skip to content

Commit

Permalink
latest
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil-2101 committed Mar 8, 2024
1 parent 087b64d commit 400d6a7
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/main/java/.idea/artifacts/main_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/main/java/.idea/artifacts/main_jar2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions src/main/java/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,13 @@ public FindCommand(String keyword) {
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
ArrayList<Task> match_input = tasks.find_input(keyword);
if (match_input.isEmpty()) {
ui.error("There are no tasks found that match the keyword: " + keyword);
} else {
} else if(keyword.isEmpty()){
throw new DukeException("Keyword is missing");
}else {
ui.line();
System.out.println("Here are the matching tasks in your list:");
for (int i = 0; i < match_input.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public void run() {
* @param args Command-line arguments.
*/
public static void main(String[] args) {
new Duke("out/tasks.txt").run();
new Duke("data/tasks.txt").run();
}
}
45 changes: 38 additions & 7 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,28 @@ public Storage(String filePath) {
}

/**
* Loads tasks from the file specified by the file path.
* Loads tasks from the specified file path. If the file or its parent directory does not exist,
* attempts to create them before reading tasks. Converts each line in the file to a Task object
* and adds it to the ArrayList of tasks.
*
* @return ArrayList of tasks loaded from the file.
* @throws DukeException If an error occurs during file reading or if the file is not found.
* @throws DukeException If an error occurs during file creation, access, reading, or if the file is not found.
*/
public ArrayList<Task> load() throws DukeException {
ArrayList<Task> tasks = new ArrayList<>();
File file = new File(filePath);

try {
if (!file.exists()) {
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
}
file.createNewFile();
}
} catch (IOException e) {
throw new DukeException("Error creating or accessing the file: " + e.getMessage());
}

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
Expand All @@ -36,27 +51,43 @@ public ArrayList<Task> load() throws DukeException {
}
}
} catch (FileNotFoundException e) {
throw new DukeException("File cannot be found.");
throw new DukeException("File not found: " + e.getMessage());
} catch (IOException e) {
throw new DukeException("Error loading tasks from file.");
throw new DukeException("Error loading tasks from file: " + e.getMessage());
}

return tasks;
}

/**
* Saves tasks to the file specified by the file path.
* Saves tasks to the specified file path. If the file or its parent directory does not exist,
* attempts to create them before saving the tasks.
*
* @param tasks ArrayList of tasks to be saved to the file.
* @throws DukeException If an error occurs during file writing.
* @throws DukeException If an error occurs during file creation, access, or writing.
*/
public void save(ArrayList<Task> tasks) throws DukeException {
File file = new File(filePath);

try {
if (!file.exists()) {
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
}
file.createNewFile();
}
} catch (IOException e) {
throw new DukeException("Error creating or accessing the file: " + e.getMessage());
}

try (FileWriter writer = new FileWriter(filePath)) {
for (Task task : tasks) {
writer.write(task.toFileString() + "\n");
}
} catch (IOException e) {
throw new DukeException("Error saving tasks to file.");
throw new DukeException("Error saving tasks to file: " + e.getMessage());
}
}

}
1 change: 1 addition & 0 deletions src/main/java/data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T|assignment|0
Binary file added src/main/java/out/artifacts/main_jar/main.jar
Binary file not shown.
Binary file added src/main/java/out/artifacts/main_jar2/main.jar
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions src/main/java/out/production/main/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified src/main/java/out/production/main/Duke.class
Binary file not shown.
Binary file modified src/main/java/out/production/main/FindCommand.class
Binary file not shown.
Binary file modified src/main/java/out/production/main/Storage.class
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/java/out/production/main/data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T|assignment|0

0 comments on commit 400d6a7

Please sign in to comment.