Skip to content

Commit

Permalink
Merge branch 'branch-Level-7'
Browse files Browse the repository at this point in the history
* branch-Level-7:
  Create txt file
  Add Save & Load

# Conflicts:
#	src/main/java/AMY/AMY.java
  • Loading branch information
Brian030601 committed Oct 4, 2023
2 parents ccb1ed6 + d6c2854 commit d587100
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
3 changes: 3 additions & 0 deletions data/AMY.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T | 0 | hello
T | 0 | bye
T | 0 | close
61 changes: 60 additions & 1 deletion src/main/java/AMY/AMY.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package AMY;

import AMY.Exceptions.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

import AMY.Exceptions.EmptyDeadlineException;
import AMY.Exceptions.EmptyDeleteException;
import AMY.Exceptions.EmptyEventException;
import AMY.Exceptions.EmptyInput;
import AMY.Exceptions.EmptyMarkException;
import AMY.Exceptions.EmptyToDoException;
import AMY.Exceptions.EmptyUnmarkException;

import AMY.command.Deadline;
import AMY.command.Event;
import AMY.command.Task;
Expand All @@ -13,8 +25,10 @@ public class AMY {
public static final String BOT_NAME = "AMY";
public static final String LINE = "____________________________________________________________";
public static ArrayList<Task> taskList = new ArrayList<>();
public static int numberOfTasks;
public static Scanner scanner = new Scanner(System.in);


// Draws a line
public static void drawLine() {
System.out.println(LINE);
Expand Down Expand Up @@ -42,6 +56,49 @@ public static void byMessage() {
drawLine();
}

// File path for storing tasks
private static final String FILE_PATH = "./data/AMY.txt";

// Save tasks to a file
public static void saveTasksToFile() {
try {
FileWriter writer = new FileWriter(FILE_PATH);
for (Task task : taskList) {
writer.write(task.toFileString() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("☹ OOPS!!! Error saving tasks to the file.");
}
}

// Load tasks from a file
public static void loadTasksFromFile() {
try {
File file = new File(FILE_PATH);
if (!file.exists()) {
file.getParentFile().mkdirs(); // Create parent directories if they don't exist
file.createNewFile();
}
Scanner scanner = new Scanner(file);
int numberOfTasks = 0; // Initialize numberOfTasks
while (scanner.hasNextLine()) {
String taskData = scanner.nextLine();
Task task = Task.parseFromFileString(taskData);
if (task != null) {
AMY.addToList(task);
numberOfTasks++; // Increment numberOfTasks
}
}
scanner.close();
AMY.numberOfTasks = numberOfTasks; // Update numberOfTasks in AMY class
} catch (FileNotFoundException e) {
System.out.println("Data file not found. Creating a new file.");
} catch (IOException e) {
System.out.println("☹ OOPS!!! Error loading tasks from the file.");
}
}

// Add a task to the list
public static void addToList(Task task) {
//taskList[numberOfTasks] = task;
Expand Down Expand Up @@ -190,7 +247,9 @@ public static void manageInput() {
// Main method executes the Chat bot
public static void main(String[] args) {
welcomeMessage();
loadTasksFromFile();
manageInput();
saveTasksToFile();
byMessage();
}
}
52 changes: 52 additions & 0 deletions src/main/java/AMY/command/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,60 @@ public String getStatusIcon() {
return (isDone ? "X" : " ");
}

public String getDescription() {
return description;
}

// Convert the task to a file-readable string format
public String toFileString() {
String status = isDone ? "1" : "0";
return String.format("%s | %s | %s", getTaskType(), status, description);
}

// Parse a task from a file string
public static Task parseFromFileString(String fileString) {
String[] parts = fileString.split("\\|");
String taskType = parts[0].trim();
String status = parts[1].trim();
String description = parts[2].trim();

Task task;

if (taskType.equals("T")) {
task = new Todo(description);
} else if (taskType.equals("D")) {
// Extract the deadline information (e.g., "June 6th")
String by = parts[3].trim();
task = new Deadline(description, by);
} else if (taskType.equals("E")) {
// Extract the event information (e.g., "Aug 6th 2-4pm")
String fromTo = parts[3].trim();
String[] dateTimeParts = fromTo.split("\\|");
String from = dateTimeParts[0].trim();
String to = dateTimeParts[1].trim();
task = new Event(description, from, to);
} else {
// Handle unknown task types (optional)
task = null;
}

if (task != null) {
if (status.equals("1")) {
task.markAsDone();
}
}

return task;
}

protected String getTaskType() {
return "T";
}

@Override
public String toString() {
return "[" + this.getStatusIcon() + "] " + description;
}
}


0 comments on commit d587100

Please sign in to comment.