@@ -9,7 +9,7 @@ public class Storage {
99 Parser parser ;
1010 Path storageFilePath ;
1111
12- public Storage (TaskList taskList , Parser parser ) throws IOException {
12+ public Storage (TaskList taskList , Parser parser ) throws DukeException {
1313 this .taskList = taskList ;
1414 this .parser = parser ;
1515 storageFilePath = Paths .get ("." , "data" , "test.txt" );
@@ -28,61 +28,63 @@ public Storage(TaskList taskList, Parser parser) throws IOException {
2828 LoadFile ();
2929 }
3030
31- public void LoadFile () throws IOException {
32-
33-
34- BufferedReader bf = new BufferedReader ( new FileReader ( storageFilePath . toString ()) );
35- String task = bf . readLine () ;
36- String [] inputs ;
37- while ( task != null ){
38- inputs = task . split ( " \\ | " , 4 ) ;
39- String taskType = inputs [ 0 ] ;
40- Task newTask ;
41- // for(String s : inputs) {
42- // System.out.print(s);
43- // }
44- // System.out.println(inputs.length) ;
45- try {
46- switch ( taskType ) {
47- case "T " : {
48- newTask = new Todo (inputs [2 ]);
49- break ;
50- }
51-
52- case "D " : {
53- newTask = Deadline .create (inputs [2 ], inputs [3 ]);
54- break ;
55- }
56-
57- case "E" : {
58- newTask = Event . create ( inputs [ 2 ], inputs [ 3 ] );
59- break ;
31+ public void LoadFile () throws DukeException {
32+ try {
33+ BufferedReader bf = new BufferedReader ( new FileReader ( storageFilePath . toString ()));
34+ String task = bf . readLine ( );
35+ String [] inputs ;
36+ while ( task != null ) {
37+ inputs = task . split ( " \\ | " , 4 );
38+ String taskType = inputs [ 0 ] ;
39+ Task newTask ;
40+ try {
41+ switch ( taskType ) {
42+ case "T" : {
43+ newTask = new Todo ( inputs [ 2 ]);
44+ break ;
45+ }
46+
47+ case "D " : {
48+ newTask = Deadline . create (inputs [2 ], inputs [ 3 ]);
49+ break ;
50+ }
51+
52+ case "E " : {
53+ newTask = Event .create (inputs [2 ], inputs [3 ]);
54+ break ;
55+ }
56+
57+ default : {
58+ throw new DukeException ( "smlj??????" );
59+ }
6060 }
6161
62- default : {
63- throw new DukeException ( "smlj??????" );
62+ if ( inputs [ 1 ]. equals ( "1" )) {
63+ newTask . complete ( );
6464 }
65+ taskList .AddTask (newTask , false );
66+ task = bf .readLine ();
67+ } catch (DukeException e ) {
68+ System .out .println (e .getMessage ());
6569 }
6670
67- if (inputs [1 ].equals ("1" )){
68- newTask .complete ();
69- }
70- taskList .AddTask (newTask ,false );
71- task = bf .readLine ();
72- }catch (DukeException e ){
73- System .out .println (e .getMessage ());
7471 }
75-
72+ }catch (IOException e ){
73+ throw new DukeException ("unable to load file" );
7674 }
7775
7876 }
7977
80- public void saveFile () throws IOException {
81- FileWriter fw = new FileWriter (storageFilePath .toString ());
78+ public void saveFile () throws DukeException {
79+ try {
80+ FileWriter fw = new FileWriter (storageFilePath .toString ());
8281 for (int i = 0 ; i < taskList .taskList .size (); i ++) {
8382 fw .write (taskList .taskList .get (i ).safeFileFormat ());
8483 }
85- fw .close ();
84+ fw .close ();
85+ }catch (IOException e ){
86+ throw new DukeException ("unable to save file" );
87+ }
8688 }
8789
8890}
0 commit comments