1+ import java .nio .file .Paths ;
2+ import java .util .Objects ;
13import java .util .Scanner ;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
6+ import java .io .File ;
7+ import java .io .FileWriter ;
8+ import java .io .IOException ;
9+ import java .nio .file .Path ;
210import HelperClass .Task ;
311
412public class Duke {
@@ -20,7 +28,7 @@ public static void Exit() {
2028
2129 }
2230
23- private static String getUserTaskName () {
31+ private static String GetUserTaskName () {
2432 Scanner getUserInput = new Scanner (System .in );
2533 String taskName = getUserInput .nextLine ();
2634 if (taskName .isEmpty ()) {
@@ -30,6 +38,144 @@ private static String getUserTaskName() {
3038 return taskName ;
3139 }
3240
41+ }
42+
43+ private static void BackgroundSetUp () {
44+ String directoryName = "data" ;
45+ String fileName = "list.txt" ;
46+
47+ File dir = new File (directoryName );
48+ if (!(dir .exists ())) {
49+ if (dir .mkdir ()) {
50+ System .out .println ("Directory '" + directoryName + "' created." );
51+ } else {
52+ System .err .println ("Failed to create directory '" + directoryName + "'." );
53+ return ;
54+ }
55+ }
56+
57+
58+ File file = new File (dir , fileName );
59+
60+ if (!(file .exists ())) {
61+ try {
62+ if (file .createNewFile ()) {
63+ System .out .println ("File '" + fileName + "' created in directory '" + directoryName + "'." );
64+ } else {
65+ System .err .println ("Failed to create file '" + fileName + "' in directory '" + directoryName + "'." );
66+ }
67+ } catch (IOException e ) {
68+ throw new RuntimeException (e );
69+ }
70+ }
71+ }
72+
73+
74+ private static List <String > ReadLine (String line ) {
75+ List <String > formattedLine = new ArrayList <>();
76+ Scanner lineScanner = new Scanner (line );
77+ while (lineScanner .hasNext ()) {
78+
79+ String token = lineScanner .next ();
80+ formattedLine .add (token );
81+
82+ }
83+ lineScanner .close ();
84+
85+ return formattedLine ;
86+ }
87+
88+
89+
90+ private static Task [] LoadList () {
91+ Task [] userList = new Task [100 ];
92+ int positionPointer = 0 ;
93+
94+ String fileName = "data/list.txt" ;
95+ Path path = Paths .get (fileName );
96+ try {
97+ Scanner fileScanner = new Scanner (path );
98+ while (fileScanner .hasNextLine ()){
99+
100+ // Record format: "Type | Status | Name | Time"
101+ // example: "D | 0 | return book | June 6th"
102+ // "0" for not done and "1" for done
103+
104+ String line = fileScanner .nextLine ();
105+
106+ List <String > formattedLine = ReadLine (line );
107+
108+
109+
110+ List <String > attributes = new ArrayList <>();
111+ String attributeName = "" ;
112+
113+ for (Object element : formattedLine ) {
114+ if (element .equals ("|" )) {
115+ attributes .add (attributeName );
116+ attributeName = "" ;
117+ } else {
118+ attributeName = attributeName + element + " " ;
119+
120+ }
121+ }
122+
123+ attributes .add (attributeName );
124+ boolean isDone = attributes .get (1 ).equals ("1" );
125+
126+ switch (attributes .get (0 )) {
127+ case "T" : {
128+ Task task = new Task (attributes .get (2 ), 1 , "Null" , isDone );
129+ userList [positionPointer ] = task ;
130+
131+ break ;
132+ }
133+ case "D" : {
134+ Task task = new Task (attributes .get (2 ), 2 , attributes .get (3 ), isDone );
135+ userList [positionPointer ] = task ;
136+
137+ break ;
138+ }
139+ case "E" : {
140+ Task task = new Task (attributes .get (2 ), 3 , attributes .get (3 ), isDone );
141+ userList [positionPointer ] = task ;
142+
143+ break ;
144+
145+
146+ }
147+ default :
148+ throw new IllegalStateException ("Unexpected value: " + attributes .get (0 ));
149+ }
150+
151+ positionPointer ++;
152+
153+
154+ }
155+ fileScanner .close ();
156+
157+ } catch (IOException e ) {
158+ throw new RuntimeException (e );
159+ }
160+
161+
162+ return userList ;
163+ }
164+
165+ private static void SaveList (Task [] userList , int numberOfElements ) {
166+ String fileName = "data/list.txt" ;
167+ try (FileWriter writer = new FileWriter (fileName )) {
168+ for (int i = 0 ; i < numberOfElements ; i ++) {
169+ writer .write (userList [i ].ForRecordingInTextFile ());
170+ writer .write ("\n " );
171+ }
172+
173+ } catch (IOException e ) {
174+ throw new RuntimeException (e );
175+ }
176+
177+
178+
33179 }
34180
35181
@@ -41,13 +187,15 @@ public static void main(String[] args) {
41187 + "|____/ \\ __,_|_|\\ _\\ ___|\n " ;
42188 System .out .println ("Hello from\n " + logo );
43189
190+ BackgroundSetUp ();
191+
44192
45193 Greet ();
46194
47195 boolean wantToExit = false ;
48196 Scanner getUserInput = new Scanner (System .in );
49197 Scanner getUserIndex = new Scanner (System .in );
50- Task [] userList = new Task [ 100 ] ;
198+ Task [] userList = LoadList () ;
51199
52200 int listPointer = 0 ;
53201
@@ -59,7 +207,10 @@ public static void main(String[] args) {
59207
60208 case "bye" :
61209 wantToExit = true ;
210+ getUserInput .close ();
211+ getUserIndex .close ();
62212 Exit ();
213+
63214 break ;
64215
65216 case "list" :
@@ -105,9 +256,9 @@ public static void main(String[] args) {
105256
106257 case "todo" :
107258 System .out .println ("Enter task name:" );
108- String taskName = getUserTaskName ();
259+ String taskName = GetUserTaskName ();
109260 if (!(taskName .isEmpty ())) {
110- userList [listPointer ] = new Task (taskName , 1 , "" );
261+ userList [listPointer ] = new Task (taskName , 1 , "Null" , false );
111262
112263 System .out .println ("Got it. I've added this task:" );
113264
@@ -122,11 +273,11 @@ public static void main(String[] args) {
122273
123274 case "deadline" :
124275 System .out .println ("Enter task name:" );
125- String taskN = getUserTaskName ();
276+ String taskN = GetUserTaskName ();
126277 if (!(taskN .isEmpty ())) {
127278 System .out .println ("Enter deadline:" );
128279 String timePeriod = getUserInput .nextLine ();
129- userList [listPointer ] = new Task (taskN , 2 , "by:" + timePeriod );
280+ userList [listPointer ] = new Task (taskN , 2 , "by:" + timePeriod , false );
130281
131282 System .out .println ("Got it. I've added this task:" );
132283
@@ -140,13 +291,14 @@ public static void main(String[] args) {
140291
141292 case "event" :
142293 System .out .println ("Enter task name:" );
143- String tN = getUserTaskName ();
294+ String tN = GetUserTaskName ();
144295 if (!(tN .isEmpty ())) {
145296 System .out .println ("Enter start time:" );
146297 String startTime = getUserInput .nextLine ();
147298 System .out .println ("Enter end time:" );
148299 String endTime = getUserInput .nextLine ();
149- userList [listPointer ] = new Task (tN , 3 , "from: " + startTime + " to: " + endTime );
300+ String timePeriod = "from: " + startTime + " to: " + endTime ;
301+ userList [listPointer ] = new Task (tN , 3 , timePeriod , false );
150302
151303 System .out .println ("Got it. I've added this task:" );
152304
@@ -202,6 +354,8 @@ public static void main(String[] args) {
202354 }
203355 printOneLine ();
204356
357+ SaveList (userList , listPointer );
358+
205359 }
206360
207361
0 commit comments