Skip to content

Commit 7279d68

Browse files
author
chloesyy
committed
Merge branch 'branch-Level-7'
# Conflicts: # src/main/java/duke/Duke.java Keep deleteTask method and tasks as ArrayList from Level-6 branch. Keep file saving methods from Level-7 branch.
2 parents 7036d71 + 50cf1ea commit 7279d68

6 files changed

Lines changed: 184 additions & 11 deletions

File tree

duke.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[T] | 0 | nothing
2+
[D] | 0 | nothing | today
3+
[E] | 1 | nothing | today
4+
[T] | 1 | here
5+
6+
7+

src/main/java/duke/Duke.java

Lines changed: 138 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import duke.task.Todo;
77

88
import java.util.ArrayList;
9+
import java.io.File;
10+
import java.io.FileWriter;
11+
import java.io.IOException;
912
import java.util.Scanner;
1013

1114
public class Duke {
@@ -18,6 +21,12 @@ public static void main(String[] args) {
1821
String line;
1922
Scanner in = new Scanner(System.in);
2023

24+
try {
25+
loadFile();
26+
} catch (IOException e) {
27+
System.out.println("Something went wrong: " + e.getMessage());
28+
}
29+
2130
printWelcomeMessage();
2231
line = in.nextLine();
2332

@@ -29,6 +38,52 @@ public static void main(String[] args) {
2938
printExitMessage();
3039
}
3140

41+
42+
/*
43+
Loads the file and creates new file if file does not exist
44+
*/
45+
public static void loadFile() throws IOException {
46+
File file = new File("duke.txt");
47+
if (!file.exists()) {
48+
file.createNewFile();
49+
}
50+
51+
Scanner read = new Scanner(file);
52+
while (read.hasNext()) {
53+
loadFileTask(read.nextLine());
54+
}
55+
read.close();
56+
}
57+
58+
/*
59+
Adds individual tasks into the array
60+
*/
61+
public static void loadFileTask(String line) {
62+
String[] taskDetails = line.split(" \\| ");
63+
64+
switch (taskDetails[0]) {
65+
case "[T]":
66+
addTodo(taskDetails[2], true);
67+
break;
68+
case "[D]":
69+
addDeadline(taskDetails[2] + " /by " + taskDetails[3], true);
70+
break;
71+
case "[E]":
72+
addEvent(taskDetails[2] + " /at " + taskDetails[3], true);
73+
break;
74+
default:
75+
// Happens if there is nothing in the list (or any garbage text)
76+
return;
77+
}
78+
79+
// Mark as done if the task is already done
80+
if (taskDetails[1].equals("1")) {
81+
tasks.get(tasks.size()-1).isDone = true;
82+
}
83+
}
84+
85+
86+
3287
public static void printWelcomeMessage() {
3388
System.out.println("Welcome! :D This is TaskThomas.");
3489
System.out.println("What are you doing today?");
@@ -50,6 +105,8 @@ public static void handleLine(String line) {
50105
}
51106
}
52107

108+
109+
53110
/*
54111
Prints the list of tasks and indicates whether they are done.
55112
*/
@@ -83,12 +140,20 @@ public static void markAsDone(String line) {
83140

84141
try {
85142
Task task = tasks.get(taskNum-1);
143+
String oldTaskString = task.toString();
144+
145+
// Update task arraylist and the file
86146
task.isDone = true;
147+
updateFile(oldTaskString, task.toString());
148+
149+
// Prints output message
87150
System.out.println("Good job! You've completed:");
88151
task.printTask();
89152
} catch (IndexOutOfBoundsException e) {
90153
System.out.println("Ohno! This is an invalid task number :(");
91154
return;
155+
} catch (IOException e) {
156+
System.out.println("Something went wrong: " + e.getMessage());
92157
}
93158
}
94159

@@ -109,13 +174,22 @@ public static void deleteTask(String line) {
109174

110175
try {
111176
Task task = tasks.get(taskNum - 1);
177+
String oldTaskString = task.toString();
178+
179+
// Updates task arraylist and file
112180
tasks.remove(task);
181+
updateFile(oldTaskString, "");
182+
183+
// Print output message
113184
System.out.println("Alright lazy bum... I'll delete this:");
114185
task.printTask();
115186
System.out.println("Now you have " + tasks.size() + " tasks in the list!");
116187
} catch (IndexOutOfBoundsException e) {
117188
System.out.println("Ohno! This is an invalid task number :(");
118189
return;
190+
} catch (IOException e) {
191+
System.out.println("Something went wrong: " + e.getMessage());
192+
return;
119193
}
120194
}
121195

@@ -125,14 +199,14 @@ public static void deleteTask(String line) {
125199
public static void addTask(String line) {
126200
if (line.startsWith("todo")) {
127201
try {
128-
addTodo(line.substring(5));
202+
addTodo(line.substring(5), false);
129203
} catch (StringIndexOutOfBoundsException e) {
130204
System.out.println("Ohno! The todo description cannot be empty :(");
131205
return;
132206
}
133207
} else if (line.startsWith("deadline")) {
134208
try {
135-
addDeadline(line.substring(9));
209+
addDeadline(line.substring(9), false);
136210
} catch (StringIndexOutOfBoundsException e) {
137211
System.out.println("Ohno! The deadline needs a description and a /by :(");
138212
return;
@@ -142,7 +216,7 @@ public static void addTask(String line) {
142216
}
143217
} else if (line.startsWith("event")) {
144218
try {
145-
addEvent(line.substring(6));
219+
addEvent(line.substring(6), false);
146220
} catch (StringIndexOutOfBoundsException e) {
147221
System.out.println("Ohno! The event needs a description and an /at :(");
148222
return;
@@ -161,20 +235,78 @@ public static void addTask(String line) {
161235
System.out.println("Now you have " + tasks.size() + " tasks in the list!");
162236
}
163237

238+
239+
164240
/*
165241
Adds either a to-do task, deadline task or event task to the list.
166242
*/
167-
public static void addTodo(String line) {
243+
public static void addTodo(String line, Boolean fromFile) {
168244
tasks.add(new Todo(line));
245+
246+
if (!fromFile) {
247+
writeToFile("duke.txt", tasks.get(tasks.size()-1).toString());
248+
}
169249
}
170250

171-
public static void addDeadline(String line) {
251+
public static void addDeadline(String line, Boolean fromFile) {
172252
String[] descriptionAndBy = line.split(" /by ");
173253
tasks.add(new Deadline(descriptionAndBy[0], descriptionAndBy[1]));
254+
255+
if (!fromFile) {
256+
writeToFile("duke.txt", tasks.get(tasks.size()-1).toString());
257+
}
174258
}
175259

176-
public static void addEvent(String line) {
260+
public static void addEvent(String line, Boolean fromFile) {
177261
String[] descriptionAndAt = line.split(" /at ");
178262
tasks.add(new Event(descriptionAndAt[1], descriptionAndAt[1]));
263+
264+
if (!fromFile) {
265+
writeToFile("duke.txt", tasks.get(tasks.size()-1).toString());
266+
}
267+
}
268+
269+
270+
/*
271+
272+
FILE UPDATE AND EDIT METHODS
273+
274+
*/
275+
/*
276+
Writes data to file
277+
*/
278+
public static void writeToFile(String filePath, String line) {
279+
try {
280+
FileWriter writer = new FileWriter(filePath, true);
281+
writer.write(System.lineSeparator() + line);
282+
writer.close();
283+
} catch (IOException e) {
284+
System.out.println("Something went wrong: " + e.getMessage());
285+
}
286+
}
287+
288+
/*
289+
Updates the file
290+
*/
291+
public static void updateFile(String oldTaskString, String newTaskString) throws IOException {
292+
File file = new File("duke.txt");
293+
Scanner read = new Scanner(file);
294+
StringBuffer buffer = new StringBuffer();
295+
296+
// Puts everything from file into buffer
297+
while (read.hasNext()) {
298+
buffer.append(read.nextLine() + System.lineSeparator());
299+
}
300+
301+
read.close();
302+
303+
// Puts everything from buffer into String
304+
String fileContents = buffer.toString();
305+
306+
// Update the file
307+
fileContents = fileContents.replace(oldTaskString, newTaskString);
308+
FileWriter writer = new FileWriter("duke.txt");
309+
writer.append(fileContents);
310+
writer.flush();
179311
}
180312
}

src/main/java/duke/task/Deadline.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package duke.task;
22

33
public class Deadline extends Task {
4-
private static final String DESCRIPTOR = "[D]";
54
private String by;
65

76
public Deadline(String description, String by) {
87
super(description);
98
this.by = by;
9+
this.DESCRIPTOR = "[D]";
10+
}
11+
12+
@Override
13+
public String getDescriptor() {
14+
return DESCRIPTOR;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return DESCRIPTOR + " | " + (this.isDone ? "1 | " : "0 | ") + this.description + " | " + this.by;
1020
}
1121

1222
@Override

src/main/java/duke/task/Event.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package duke.task;
22

33
public class Event extends Task {
4-
private static final String DESCRIPTOR = "[E]";
54
private String at;
65

76
public Event(String description, String at) {
87
super(description);
98
this.at = at;
9+
this.DESCRIPTOR = "[E]";
10+
}
11+
12+
@Override
13+
public String getDescriptor() {
14+
return DESCRIPTOR;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return DESCRIPTOR + " | " + (this.isDone ? "1 | " : "0 | ") + this.description + this.at;
1020
}
1121

1222
@Override

src/main/java/duke/task/Task.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package duke.task;
22

3-
public class Task {
3+
public abstract class Task {
44
protected String description;
55
public boolean isDone;
6+
protected String DESCRIPTOR;
67

78
public Task(String description) {
89
this.description = description;
@@ -14,6 +15,10 @@ public String getStatusIcon() {
1415
return (isDone ? "\u2713" : "\u2718");
1516
}
1617

18+
public abstract String getDescriptor();
19+
20+
public abstract String toString();
21+
1722
public void printTask() {
1823
System.out.println("[" + this.getStatusIcon() + "] " + this.description);
1924
}

src/main/java/duke/task/Todo.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package duke.task;
22

33
public class Todo extends Task {
4-
private static final String DESCRIPTOR = "[T]";
5-
64
public Todo(String description) {
75
super(description);
6+
this.DESCRIPTOR = "[T]";
7+
}
8+
9+
@Override
10+
public String getDescriptor() {
11+
return DESCRIPTOR;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return DESCRIPTOR + " | " + (this.isDone ? "1 | " : "0 | ") + this.description;
817
}
918

1019
@Override

0 commit comments

Comments
 (0)