Skip to content

Commit

Permalink
edited save and load methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TriciaBK committed Oct 6, 2023
1 parent 5f21f79 commit 47f4996
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/main/java/Storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Storage {
* Method to save all list items to file recursively
*/
public static void saveListToFile() {
try (FileWriter fileWriter = new FileWriter("data/botbot.txt")){
try (FileWriter fileWriter = new FileWriter("/ip/data/botbot.txt")){
for (int i = 0; i<TaskList.size(); i++) {
fileWriter.write(list.get(i).toString() + "\n");
}
Expand Down Expand Up @@ -82,26 +82,40 @@ public static void extractEvent(String savedTask) throws DukeException {
* @throws DukeException when format of saved file is wrong and hence cannot be loaded
*/
public static void loadListFromFile() throws IOException, DukeException {
File file = new File("data/botbot.txt");
if (!file.exists()){
File directory = new File("/ip/data");
File file = new File("/ip/data/botbot.txt");

if (!directory.exists()) {
if (directory.mkdirs()) {
System.out.println("Directory created successfully.");
} else {
System.out.println("Failed to create directory.");
}
}

if (!file.exists()) {
try {
boolean createdDirectory = file.mkdirs();
boolean createdFile = file.createNewFile();
} catch (IOException e){
if (file.createNewFile()) {
System.out.println("File created successfully.");
} else {
System.out.println("Failed to create file.");
}
} catch (IOException e) {
System.out.println("Something went wrong: " + e.getMessage());
}
} else {
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNext()) {
String savedTask = fileScanner.nextLine();
if (savedTask.charAt(1) == ('T')) {
extractTodo(savedTask);
} else if (savedTask.charAt(1) == ('D')) {
extractDeadline(savedTask);
} else if (savedTask.charAt(1) == ('E')) {
extractEvent(savedTask);
}
}

Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNext()) {
String savedTask = fileScanner.nextLine();
if (savedTask.charAt(1) == ('T')) {
extractTodo(savedTask);
} else if (savedTask.charAt(1) == ('D')) {
extractDeadline(savedTask);
} else if (savedTask.charAt(1) == ('E')) {
extractEvent(savedTask);
}
}

}
}

0 comments on commit 47f4996

Please sign in to comment.