Skip to content

Commit 4723747

Browse files
committed
Merge branch 'branch-Level-7'
* branch-Level-7: Add save function # Conflicts: # src/main/java/burger/Burger.java # src/main/java/burger/list/List.java # src/main/java/burger/task/Task.java
2 parents 1856003 + 0e943d8 commit 4723747

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

src/main/java/burger/Burger.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package burger;
22

3+
import java.io.IOException;
34
import java.util.Scanner;
45

56
import burger.list.List;
67

7-
public class
8-
{
8+
import static burger.BurgerFileClass.getSaveFile;
9+
import static burger.BurgerFileClass.setSaveFile;
10+
import static burger.BurgerFileClass.PATHNAME;
11+
12+
public class Burger {
913
static final String CHATBOT_NAME = "Burger";
1014
static final String HORIZONTAL_LINE = "---------------------------------";
1115

12-
public static void main(String[] args) {
16+
public static void main(String[] args) throws IOException {
1317
welcomeMessage();
14-
List myList = new List();
18+
List myList = getSaveFile();
1519
Scanner input = new Scanner(System.in);
1620
boolean isPolling = true;
1721
while (isPolling) {
@@ -32,7 +36,7 @@ public static void main(String[] args) {
3236
}
3337
}
3438
}
35-
goodbye();
39+
goodbye(myList);
3640
}
3741

3842
private static void welcomeMessage() {
@@ -55,7 +59,9 @@ private static void printUnknownInputError() {
5559
printLine();
5660
}
5761

58-
public static void goodbye() {
62+
public static void goodbye(List currList) throws IOException {
63+
System.out.print("Saving file");
64+
setSaveFile(PATHNAME, currList);
5965
printLine();
6066
System.out.println("Bye. Hope to see you again soon!");
6167
printLine();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package burger;
2+
3+
import burger.list.List;
4+
5+
import java.io.File;
6+
import java.io.FileWriter;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.util.Scanner;
11+
12+
public class BurgerFileClass {
13+
static final int TDEINDEX = 0;
14+
static final int MARKINDEX = 1;
15+
16+
static final int TASKINDEX = 2;
17+
static final String PATHNAME = java.nio.file.Paths.get("data","burger.txt")
18+
.normalize().toString();
19+
20+
public static void readFromFile(String filePath, List list) throws IOException {
21+
Files.createDirectories(Paths.get("data"));
22+
File f = new File(filePath); // create a File for the given file path
23+
Scanner s = new Scanner(f); // create a Scanner using the File as the source
24+
while (s.hasNext()) {
25+
String[] currLineArray = s.nextLine().split("\\|");
26+
char currTDE = currLineArray[TDEINDEX].charAt(0);
27+
char currMark = currLineArray[MARKINDEX].charAt(0);
28+
String currTask = currLineArray[TASKINDEX];
29+
list.addFromSaveFile(currTDE, currMark, currTask);
30+
list.totalTasks++;
31+
}
32+
}
33+
34+
public static void setSaveFile(String filePath, List newList) {
35+
try {
36+
FileWriter fw = new FileWriter(filePath);
37+
int i = 0;
38+
String textToWrite;
39+
while (i < newList.totalTasks) {
40+
textToWrite = newList.getTask(i).getTDE() + "|" + newList.getTask(i).getTick() + "|" + newList.getTask(i).getName();
41+
fw.write(textToWrite + System.lineSeparator());
42+
System.out.print(".");
43+
i++;
44+
}
45+
fw.close();
46+
System.out.println();
47+
System.out.println("File successfully saved!");
48+
} catch (IOException e) {
49+
System.out.println();
50+
System.out.println("OH NO! File not saved!");
51+
}
52+
53+
}
54+
55+
public static List getSaveFile() {
56+
List newList = new List();
57+
try {
58+
readFromFile(PATHNAME, newList);
59+
System.out.println("File is found!");
60+
System.out.println("Current List: ");
61+
newList.printTaskList();
62+
} catch (IOException e) {
63+
System.out.println("File is not found! But we will create one for you!");
64+
}
65+
return newList;
66+
}
67+
}

src/main/java/burger/list/List.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public boolean isValidCommand(String[] textArray) {
6666
}
6767
}
6868

69+
public void addFromSaveFile(char tde, char mark, String task) {
70+
Task currTask = new Task(task, tde);
71+
if (mark == 'X') {
72+
currTask.markDone();
73+
}
74+
taskList.add(currTask);
75+
}
76+
6977
private static int getIdx(String[] textArray) {
7078
StringBuilder idx = new StringBuilder();
7179
for (int i = 1; i < textArray.length; i++) {

src/main/java/burger/task/Task.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Task(String task, char tde) {
1717
*/
1818
public void printTask() {
1919
char tde = getTDE() ;
20-
String tick = getTick();
20+
char tick = getTick();
2121
String name = getName();
2222
System.out.print("[" + tde + "]");
2323
System.out.print("[" + tick + "] ");
@@ -27,8 +27,9 @@ public void printTask() {
2727
public String getName() {
2828
return this.name;
2929
}
30-
public String getTick() {
31-
return this.hasDone ? "X" : " ";
30+
31+
public char getTick() {
32+
return this.hasDone ? 'X' : ' ';
3233
}
3334
public char getTDE() {
3435
return this.tde;

0 commit comments

Comments
 (0)