55import java .util .Scanner ;
66import java .util .ArrayList ;
77import java .util .List ;
8+ import java .io .IOException ;
9+ import java .io .FileReader ;
10+ import java .io .BufferedReader ;
11+ import java .io .FileWriter ;
12+ import java .io .BufferedWriter ;
813public class Huan {
914 private static List <Task > tasks = new ArrayList <>();
1015
16+ private static String taskFile = "tasklist.txt" ;
1117 public static Boolean isIndexValid (int index ) {
1218 return index >= 1 && index <= tasks .size ();
1319 }
@@ -41,7 +47,44 @@ public static void listTasks() {
4147 }
4248 }
4349 }
44- public static void readProcessCommand () {
50+
51+ public static void writeTasks () {
52+ try {
53+ BufferedWriter writer = new BufferedWriter (new FileWriter (taskFile ));
54+ for (Task task : tasks ) {
55+ writer .write (task .writeLine ());
56+ writer .newLine ();
57+ }
58+ writer .flush ();
59+ }catch (IOException e ) {
60+ e .printStackTrace ();
61+ }
62+ }
63+
64+ public static void processLine (String line ) throws Exception {
65+ String [] words = line .split (" " );
66+ String suffixWord = line .substring (words [0 ].length () + 1 );
67+
68+ if (words [0 ].length () != 2 || (words [0 ].charAt (1 ) != 'T' && words [0 ].charAt (1 ) != 'F' )) {
69+ throw new Exception ();
70+ }
71+
72+ switch (words [0 ].charAt (0 )) {
73+ case ('T' ):
74+ addTask (new TodoTask (suffixWord , words [0 ].charAt (1 ) == 'T' ));
75+ break ;
76+ case ('D' ):
77+ addTask (new DeadlineTask (suffixWord , words [0 ].charAt (1 ) == 'T' ));
78+ break ;
79+ case ('E' ):
80+ addTask (new EventTask (suffixWord , words [0 ].charAt (1 ) == 'T' ));
81+ break ;
82+ default :
83+ throw new Exception ();
84+ }
85+ }
86+
87+ public static void readProcessCommands () {
4588
4689 Scanner scanner = new Scanner (System .in );
4790 while (true ) {
@@ -79,6 +122,7 @@ public static void readProcessCommand() {
79122 } else {
80123 tasks .get (markIndex - 1 ).setIsDone (true );
81124 System .out .println ("Set task number " + markIndex + ": " + tasks .get (markIndex - 1 ).getName () + " as done." );
125+ writeTasks ();
82126 }
83127 } catch (Exception e ){
84128 System .out .println ("Incorrect format! Should be 'mark *n', where n is the index of the task you wish to mark as finished." );
@@ -92,6 +136,7 @@ public static void readProcessCommand() {
92136 } else {
93137 tasks .get (unmarkIndex - 1 ).setIsDone (false );
94138 System .out .println ("Set task number " + unmarkIndex + ": " + tasks .get (unmarkIndex - 1 ).getName () + " as not done." );
139+ writeTasks ();
95140 }
96141 } catch (Exception e ){
97142 System .out .println ("Incorrect format! Should be 'unmark *n', where n is the index of the task you wish to mark as unfinished." );
@@ -105,12 +150,14 @@ public static void readProcessCommand() {
105150 TodoTask todoTask = new TodoTask (suffixWord , false );
106151 addTask (todoTask );
107152 System .out .println ("Added todo type task with name: " + todoTask .getName ());
153+ writeTasks ();
108154 break ;
109155 case ("event" ):
110156 try {
111157 EventTask eventTask = new EventTask (suffixWord , false );
112158 addTask (eventTask );
113159 System .out .println ("Added event type task with name: " + eventTask .getName ());
160+ writeTasks ();
114161 } catch (Exception e ) {
115162 System .out .println ("Incorrect format! Should be 'event *event_name /from *start_time /to *end_time'." );
116163 }
@@ -120,6 +167,7 @@ public static void readProcessCommand() {
120167 DeadlineTask deadlineTask = new DeadlineTask (suffixWord , false );
121168 addTask (deadlineTask );
122169 System .out .println ("Added deadline type task with name: " + deadlineTask .getName ());
170+ writeTasks ();
123171 } catch (Exception e ) {
124172 System .out .println ("Incorrect format! Should be 'deadline *task_name /by *deadline_time'" );
125173 }
@@ -151,6 +199,20 @@ public static void main(String[] args) {
151199 String botName = "Huan" ;
152200 System .out .println ("Hello! I'm " + botName + ", a chat bot" );
153201
154- readProcessCommand ();
202+ try (BufferedReader reader = new BufferedReader (new FileReader (taskFile ))){
203+ String line ;
204+ try {
205+ while ((line = reader .readLine ()) != null ) {
206+ processLine (line );
207+ }
208+ } catch (Exception e ) {
209+ System .out .println ("Corrupt task save file" );
210+ tasks .clear ();
211+ }
212+
213+ } catch (IOException e ) {
214+ tasks .clear ();
215+ }
216+ readProcessCommands ();
155217 }
156218}
0 commit comments