From bc60c4beab6b36ac219a995960ac55f4b02cb251 Mon Sep 17 00:00:00 2001 From: carljgu <carljgu@stud.ntnu.no> Date: Sat, 26 Mar 2022 15:25:44 +0100 Subject: [PATCH] fix: file not found exception --- .gitignore | 1 + README.md | 11 +++++++++++ .../java/edu/ntnu/idatt2001/carljgu/FileHandler.java | 12 ++++++------ .../edu/ntnu/idatt2001/carljgu/FileHandlerTest.java | 2 ++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 82dd17c..768a5c9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /dataSources.local.xml /target/ /.idea/ +wargames.iml \ No newline at end of file diff --git a/README.md b/README.md index 5ad6f01..1e9631e 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,14 @@ Added classes - Commander: Extends Cavalry, but a little stronger attack and armor. - Army: Contains several units. - Battle: Simulates a battle between two armies. One unit from one army attacks an opponent. + +## Release 2.0 + +Added classes +- FileHandler: A class to read and write an army to a file +- FileExtensionException: An exception thrown if an extension is wrong +- FileHandlerTest: Tests for testing file + +Added methods +- get*Units: Methods using streams to get Infantry, +Ranged, Cavalry and Commander units. diff --git a/src/main/java/edu/ntnu/idatt2001/carljgu/FileHandler.java b/src/main/java/edu/ntnu/idatt2001/carljgu/FileHandler.java index 02eb310..de8b8a9 100644 --- a/src/main/java/edu/ntnu/idatt2001/carljgu/FileHandler.java +++ b/src/main/java/edu/ntnu/idatt2001/carljgu/FileHandler.java @@ -7,10 +7,7 @@ import edu.ntnu.idatt2001.carljgu.units.specialized.CommanderUnit; import edu.ntnu.idatt2001.carljgu.units.specialized.InfantryUnit; import edu.ntnu.idatt2001.carljgu.units.specialized.RangedUnit; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.nio.file.FileSystems; import java.nio.file.InvalidPathException; @@ -55,9 +52,12 @@ public class FileHandler { * @return army - Army - an army read from a csv file * @throws FileExtensionException thrown if file extension is not csv * @throws IOException thrown if an I/O error occurs + * @throws FileNotFoundException thrown if the filepath was not found */ - public static Army readArmyFromFile(String filePath) throws FileExtensionException, IOException { - if (!filePath.startsWith(".csv", filePath.length() - 4)) throw new FileExtensionException("File extension has to be csv"); + public static Army readArmyFromFile(String filePath) + throws FileExtensionException, IOException, FileNotFoundException { + if (!filePath.startsWith(".csv", filePath.length() - 4)) + throw new FileExtensionException("File extension has to be csv"); try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { String line; diff --git a/src/test/java/edu/ntnu/idatt2001/carljgu/FileHandlerTest.java b/src/test/java/edu/ntnu/idatt2001/carljgu/FileHandlerTest.java index 9aeef7d..f30f5b6 100644 --- a/src/test/java/edu/ntnu/idatt2001/carljgu/FileHandlerTest.java +++ b/src/test/java/edu/ntnu/idatt2001/carljgu/FileHandlerTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.*; +import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; -- GitLab