Skip to content

Commit

Permalink
Add ability to save to .txt
Browse files Browse the repository at this point in the history
  • Loading branch information
jehousoh committed Aug 20, 2022
1 parent 94d1997 commit 2f6eae8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.[D][ ] Bye (by: Sunday)
19 changes: 19 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;
import java.io.IOException;

public class Duke {
/**
Expand All @@ -11,6 +15,19 @@ public static void main(String[] args) {
TaskList list = new TaskList();
Scanner sc = new Scanner(System.in);

try {
Files.createDirectories(Paths.get("./data"));
File file = new File("./data/duke.txt");

if (!file.exists()) {
boolean result = file.createNewFile();
}
}

catch (IOException e) {
e.printStackTrace();
}

while (true) {
in = sc.nextLine();

Expand Down Expand Up @@ -70,6 +87,8 @@ public static void main(String[] args) {
}
}
System.out.println("-------------------------------------------");

FileWriting.update("./data/duke.txt", list);
}
Messages.bye();
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/FileWriting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.io.IOException;
import java.io.FileWriter;

public class FileWriting {
/**
* Class to update .txt file
*/
public static void update(String filePath, TaskList list) {
try {
FileWriter fw = new FileWriter(filePath);
fw.write(list.getTasks());
fw.close();
}

catch (IOException e) {
e.printStackTrace();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public void printTasks() {
}
}

public String getTasks() {
int index = 0;
Task item;
String result = "";
while (index < taskList.size()) {
item = taskList.get(index);
result += ((index + 1) + "." + item.toString() + "\n");
index++;
}
return result;
}

public void mark(int index) {
taskList.get(index).markAsDone();
Messages.mark();
Expand Down
1 change: 1 addition & 0 deletions text-ui-test/data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.[D][ ] return book (by: Sunday)

0 comments on commit 2f6eae8

Please sign in to comment.