diff --git a/.gitignore b/.gitignore index 82dd17c03d109687cdf8be83f2532171bf25ff29..768a5c9bd617ff206142d4d933282abfb3f766f3 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 5ad6f010097db330214478fab20267c90275b447..1e9631eb953bdaf409a28fc18af30d08abd14cb3 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 02eb310035dc661f6ee40a3c2012e2f4bf7a9dc6..de8b8a999626c6c3fcdd49f466dab84526c88577 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 9aeef7d25cb1bc73bb6cadfc15ea571ccc5f54e1..f30f5b671c518169a12660990de32c8ef122371a 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;