99import java .util .Scanner ;
1010
1111import duke .exceptions .CorruptedLineException ;
12+ import duke .inputoutput .DukeIo ;
1213import duke .task .Task ;
1314
1415/**
1718public class Storage {
1819
1920 private static final String DEFAULT_SAVE_PATH = "data/SavedData.duke" ;
21+ private static final String PARSE_FAIL = "I was unable to parse line %s of the save file!" ;
2022 private File file ;
2123
2224 private Storage (File file ) {
@@ -31,12 +33,11 @@ private Storage(File file) {
3133 * @throws IOException Throws if pathing cannot exist.
3234 */
3335 public static Storage createStorage (String path ) throws IOException {
34- File newFile = new File (path );
35- File parentFolder = newFile .getParentFile ();
36- if (parentFolder != null ) {
37- parentFolder .mkdir ();
36+ if (path .trim ().isEmpty ()) {
37+ path = DEFAULT_SAVE_PATH ;
3838 }
39- newFile .createNewFile ();
39+ File newFile = new File (path );
40+ ensureExistance (newFile );
4041 return new Storage (newFile );
4142 }
4243
@@ -50,13 +51,24 @@ public static Storage createStorage() throws IOException {
5051 return createStorage (DEFAULT_SAVE_PATH );
5152 }
5253
54+ private static void ensureExistance (File file ) throws IOException {
55+ if (file .exists ()) {
56+ return ;
57+ }
58+ File parentFolder = file .getParentFile ();
59+ if (parentFolder != null ) {
60+ parentFolder .mkdirs ();
61+ }
62+ file .createNewFile ();
63+ }
64+
5365 /**
5466 * Read the save file and convert it to a list of Task.
5567 *
5668 * @return List of Tasks
5769 * @throws FileNotFoundException Throws when save file does not exist
5870 */
59- public List <Task > readFile () throws FileNotFoundException {
71+ public List <Task > readFile (DukeIo io ) throws FileNotFoundException {
6072 List <Task > ret = new ArrayList <>();
6173 List <Integer > corruptedLines = new ArrayList <>();
6274
@@ -75,6 +87,16 @@ public List<Task> readFile() throws FileNotFoundException {
7587 }
7688 }
7789 sc .close ();
90+ if (corruptedLines .size () == 0 ) {
91+ return ret ;
92+ }
93+ StringBuilder joinedList = corruptedLines .stream ().collect (StringBuilder ::new , (sb , num ) -> {
94+ sb .append (num );
95+ sb .append (", " );
96+ }, StringBuilder ::append );
97+ // removes the ", " of the last string
98+ joinedList .setLength (joinedList .length () - 2 );
99+ io .printError (String .format (PARSE_FAIL , joinedList .toString ()));
78100 return ret ;
79101 }
80102
@@ -85,9 +107,7 @@ public List<Task> readFile() throws FileNotFoundException {
85107 * @throws IOException Throws when save file doesn't exist
86108 */
87109 public void saveData (ParsedData [] dataList ) throws IOException {
88- if (!file .exists ()) {
89- file .createNewFile ();
90- }
110+ ensureExistance (file );
91111
92112 assert file .exists ();
93113 StringBuilder sb = new StringBuilder ();
@@ -117,9 +137,7 @@ public void saveTasks(TaskList tl) throws IOException {
117137 * @throws IOException Throws when save file is missing
118138 */
119139 public void saveTask (Task task ) throws IOException {
120- if (!file .exists ()) {
121- file .createNewFile ();
122- }
140+ ensureExistance (file );
123141 assert file .exists ();
124142
125143 FileWriter fw = new FileWriter (file , true );
0 commit comments