Skip to content

Commit

Permalink
Merge pull request #4 from TyrusLye/branch-Level-9
Browse files Browse the repository at this point in the history
A-Storage
  • Loading branch information
TyrusLye committed Oct 23, 2023
2 parents e73f390 + 4d45b43 commit 3c69f79
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 89 deletions.
93 changes: 4 additions & 89 deletions src/main/java/URBOI_PACKIN/ResponseController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package URBOI_PACKIN;

import URBOI_PACKIN.Storage.Storage;
import URBOI_PACKIN.TaskTypes.Deadline;
import URBOI_PACKIN.TaskTypes.Event;
import URBOI_PACKIN.TaskTypes.Todo;
Expand All @@ -10,6 +11,9 @@
import java.util.ArrayList;
import java.io.*;

import static URBOI_PACKIN.Storage.Storage.loadTasksFromFile;
import static URBOI_PACKIN.Storage.Storage.saveTasksToFile;

public class ResponseController {
//private static final String FILE_PATH = "src/main/java/tasks.txt";
private static final String FILE_PATH = "tasks.txt/";
Expand Down Expand Up @@ -97,77 +101,6 @@ public String getResponse(String command) {
return response.toString();
}

/**
* Loads tasks from a file into the task list.
*
* @param tasks The ArrayList to store the loaded tasks.
*/
private static void loadTasksFromFile(ArrayList<Task> tasks) {
try {
File file = new File(FILE_PATH);
if (file.exists()) {
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
Task task = createTaskFromLine(line);
if (task != null) {
tasks.add(task);
}
}
fileScanner.close();
}
} catch (FileNotFoundException e) {
// Handle file not found exception
System.out.println("File not found: " + FILE_PATH);
}
}

/**
* Creates a URBOI_PACKIN.Task object from a line of text in the specified format.
*
* @param line The line of text containing task details.
* @return A URBOI_PACKIN.Task object representing the task described in the line, or null if parsing fails.
*/
private static Task createTaskFromLine(String line) {
String[] parts = line.split(" \\| ");
if (parts.length < 3) {
return null;
}

String type = parts[0];
String status = parts[1];
String description = parts[2];

Task task = null;
switch (type) {
case "T":
task = new Todo(description);
break;
case "D":
if (parts.length >= 4) {
String by = parts[3];
task = new Deadline(description, LocalDateTime.parse(by, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
break;
case "E":
if (parts.length >= 5) {
String from = parts[3];
String to = parts[4];
task = new Event(description, from, to);
}
break;
}

if (task != null) {
if (status.equals("1")) {
task.markDone();
} else {
task.markNotDone();
}
}

return task;
}

private static void findTasks(ArrayList<Task> tasks, String keyword) {
System.out.println("Here are the matching tasks in your list:");
Expand All @@ -183,24 +116,6 @@ private static void findTasks(ArrayList<Task> tasks, String keyword) {
System.out.println("No matching tasks found.");
}
}

/**
* Save tasks to a file.
*
* @param tasks The ArrayList of tasks to save.
*/
private static void saveTasksToFile(ArrayList<Task> tasks) {
try {
FileWriter fileWriter = new FileWriter(FILE_PATH);
for (Task task : tasks) {
fileWriter.write(task.toFileString() + System.lineSeparator());
}
fileWriter.close();
} catch (IOException e) {
// Handle IO exception
System.out.println("Error saving tasks to file: " + e.getMessage());
}
}
}


Expand Down
93 changes: 93 additions & 0 deletions src/main/java/URBOI_PACKIN/Storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package URBOI_PACKIN.Storage;

import URBOI_PACKIN.Task;
import URBOI_PACKIN.TaskTypes.Deadline;
import URBOI_PACKIN.TaskTypes.Event;
import URBOI_PACKIN.TaskTypes.Todo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class Storage {
private static final String FILE_PATH = "tasks.txt";

public static void loadTasksFromFile(ArrayList<Task> tasks) {
try {
File file = new File(FILE_PATH);
if (file.exists()) {
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
Task task = createTaskFromLine(line);
if (task != null) {
tasks.add(task);
}
}
fileScanner.close();
}
} catch (FileNotFoundException e) {
// Handle file not found exception
System.out.println("File not found: " + FILE_PATH);
}
}

public static void saveTasksToFile(ArrayList<Task> tasks) {
try {
FileWriter fileWriter = new FileWriter(FILE_PATH);
for (Task task : tasks) {
fileWriter.write(task.toFileString() + System.lineSeparator());
}
fileWriter.close();
} catch (IOException e) {
// Handle IO exception
System.out.println("Error saving tasks to file: " + e.getMessage());
}
}

private static Task createTaskFromLine(String line) {
String[] parts = line.split(" \\| ");
if (parts.length < 3) {
return null;
}

String type = parts[0];
String status = parts[1];
String description = parts[2];

Task task = null;
switch (type) {
case "T":
task = new Todo(description);
break;
case "D":
if (parts.length >= 4) {
String by = parts[3];
task = new Deadline(description, LocalDateTime.parse(by, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
break;
case "E":
if (parts.length >= 5) {
String from = parts[3];
String to = parts[4];
task = new Event(description, from, to);
}
break;
}

if (task != null) {
if (status.equals("1")) {
task.markDone();
} else {
task.markNotDone();
}
}

return task;
}
}

0 comments on commit 3c69f79

Please sign in to comment.