Skip to content

Commit

Permalink
Updated IKOrganizationScraper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
roaraf committed Feb 26, 2026
1 parent 07d0180 commit 9121be5
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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[]{
Expand All @@ -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<Organization> getData() {
return Collections.unmodifiableList(this.organizationData);
}
Expand Down

0 comments on commit 9121be5

Please sign in to comment.