diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/IKOrganizationScraper.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/IKOrganizationScraper.java index d7e833d..e43eef0 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/IKOrganizationScraper.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/IKOrganizationScraper.java @@ -1,5 +1,6 @@ package ntnu.sytemutvikling.team6.models; +import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.time.Duration; @@ -83,6 +84,15 @@ public boolean updateData() { } public boolean writeToCSV() throws IOException { + if (this.organizationData.isEmpty()) { + throw new IllegalArgumentException("There is no data in the " + + "organizationData list. Run .updateData before proceeding."); + } + + if (!this.deleteCSV()) { + throw new IOException("Failed to delete the CSV file."); + } + try (CSVWriter writer = new CSVWriter(new FileWriter(filename))) { for (Organization o : this.organizationData) { writer.writeNext(new String[]{ @@ -96,6 +106,17 @@ public boolean writeToCSV() throws IOException { return true; } + public boolean deleteCSV() { + var file = new File(filename); + + // Returns true if file is deleted (or if file doesn't exist + // Returns false if file couldn't be deleted + if (file.exists()) { + return file.delete(); + } + return true; + } + public List getData() { return Collections.unmodifiableList(this.organizationData); }