66import duke .task .Todo ;
77
88import java .util .ArrayList ;
9+ import java .io .File ;
10+ import java .io .FileWriter ;
11+ import java .io .IOException ;
912import java .util .Scanner ;
1013
1114public 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}
0 commit comments