From 9121be598097b1804afaecfe7b4f6a324adffc62 Mon Sep 17 00:00:00 2001 From: Roar Date: Thu, 26 Feb 2026 11:06:53 +0100 Subject: [PATCH] Updated IKOrganizationScraper Added method deleteCSV that deletes the csv if it exists. Added method to writeToCSV for cleaner generation of csv. Also added exceptions to handle missing data and failure to delete CSV file. --- .../team6/models/IKOrganizationScraper.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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); }