Skip to content

Commit

Permalink
Add read/write functionalities such that users can load their own tas…
Browse files Browse the repository at this point in the history
…k lists from their hard disks and save changes they made to their lists on their hard disks
  • Loading branch information
chihyingho authored and chihyingho committed Mar 2, 2022
1 parent 5ee1827 commit 8d655bc
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 92 deletions.
5 changes: 5 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
T|0|deadline
D|0|eat lunc|noon
D|0|lunc|tomorrow
T|0|lunch
T|0|lunch
5 changes: 5 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
}

@Override
public String getDetails() {
return "D|" + (isDone ? "1" : "0") + "|" + description + "|" + by;
}

}
291 changes: 201 additions & 90 deletions src/main/java/Duke.java

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public Event(String description, String at) {
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
}

@Override
public String getDetails() {
return "E|" + (isDone ? "1" : "0") + "|" + description + "|" + at;
}
}
38 changes: 38 additions & 0 deletions src/main/java/FileEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileEditor {
private static final String DIRECTORY = "data";
private static final String FILENAME = "duke.txt";
private static final String FILEPATH = DIRECTORY + File.separator + FILENAME;

public static void updateFile(ArrayList<Task> tasks) throws IOException {
FileWriter fw = new FileWriter(FILEPATH);
for (Task task : tasks) {
String taskToAppend = task.getDetails();
fw.write(taskToAppend + System.lineSeparator());
}
fw.close();
}

public ArrayList<String> readFileContents() throws IOException {
File directory = new File(DIRECTORY);
if(!directory.exists()) {
directory.mkdir();
}
File file = new File(DIRECTORY + File.separator + FILENAME);
if(!file.exists()) {
file.createNewFile();
}
Scanner s = new Scanner(file);
ArrayList<String> tasksFromFile = new ArrayList<>();
while (s.hasNextLine()) {
tasksFromFile.add(s.nextLine());
}
return tasksFromFile;
}
}
6 changes: 4 additions & 2 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class Task {
public abstract class Task {
protected String description;
protected boolean isDone;

Expand All @@ -8,7 +8,7 @@ public Task(String description) {
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
return (isDone ? "X" : " ");
}

public String getDescription() {
Expand Down Expand Up @@ -43,4 +43,6 @@ public String toString() {
return "[" + getStatusIcon() + "] " + description;
}

public abstract String getDetails();

}
5 changes: 5 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public Todo(String description) {
public String toString() {
return "[T]" + super.toString();
}

@Override
public String getDetails() {
return "T|" + (isDone ? "1" : "0") + "|" + description;
}
}

0 comments on commit 8d655bc

Please sign in to comment.