Skip to content

Commit

Permalink
Save the tasks in the hard disk automatically whenever the task list …
Browse files Browse the repository at this point in the history
…changes. Load the data from the hard disk when the project starts up.
  • Loading branch information
dreammac3816547290 committed Aug 27, 2022
1 parent 09a9ccd commit 4418b59
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
Empty file added duke.txt
Empty file.
5 changes: 5 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public Deadline(String description, String deadline) {
public String toString() {
return super.toString() + "(by: " + deadline + ")";
}

@Override
public String data() {
return super.data() + ", " + deadline;
}
}
57 changes: 56 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;

public class Duke {
public static void main(String[] args) {
System.out.println("Hello! I'm Duke\nWhat can I do for you?");
Scanner sc = new Scanner(System.in);
ArrayList<Task> tasks = new ArrayList<>();
ArrayList<Task> tasks = initialize();
while (true) {
try {
String s = sc.next();
Expand Down Expand Up @@ -79,10 +83,61 @@ public static void main(String[] args) {
String rest = sc.nextLine();
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
modify(tasks);
} catch (DukeException e) {
System.out.println(e.getMessage());
}
}
sc.close();
}

private static ArrayList<Task> initialize() {
File file = new File("./duke.txt");
ArrayList<Task> tasks = new ArrayList<>();
Scanner sc;
try {
file.createNewFile();
sc = new Scanner(file);
} catch (IOException e) {
return tasks;
}
while (sc.hasNext()) {
String task = sc.nextLine();
String[] split = task.split(", ");
for (String element : split) {
System.out.println(element);
}
if (split[0].equals("T")) {
tasks.add(new Task(split[2]));
if (split[1].equals("1")) {
tasks.get(tasks.size() - 1).markAsDone();
}
} else if (split[0].equals("D")) {
tasks.add(new Deadline(split[2], split[3]));
if (split[1].equals("1")) {
tasks.get(tasks.size() - 1).markAsDone();
}
} else if (split[0].equals("E")) {
tasks.add(new Event(split[2], split[3]));
if (split[1].equals("1")) {
tasks.get(tasks.size() - 1).markAsDone();
}
}
}
return tasks;
}

private static void modify(ArrayList<Task> tasks) {
String data = "";
try {
FileWriter fw = new FileWriter("./duke.txt");
for (Task task : tasks) {
data += task.data() + "\n";
}
fw.write(data);
fw.close();
} catch (IOException e) {

}
}
}
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 time) {
public String toString() {
return super.toString() + "(at: " + time + ")";
}

@Override
public String data() {
return super.data() + ", " + time;
}
}
4 changes: 4 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public void markAsDone() {
public void markNotDone() {
isDone = false;
}

public String data() {
return type + ", " + (isDone ? 1 : 0) + ", " + description;
}
}

0 comments on commit 4418b59

Please sign in to comment.