java.lang.Object
edu.ntnu.idatt2001.carljgu.filehandling.ArmyFileHandler

public class ArmyFileHandler extends Object
Handles writing, reading and importing armies to and from a file.
Version:
1.4 1.05.2022
Author:
Carl Gützkow
  • Field Details

    • readLinesSkipped

      private final List<String> readLinesSkipped
    • DELIMITER

      private final String DELIMITER
  • Constructor Details

    • ArmyFileHandler

      public ArmyFileHandler()
      Constructor which initiates the readLinesSkipped arraylist Sets the delimiter to a comma for comma seperated value.
  • Method Details

    • getReadLinesSkipped

      public List<String> getReadLinesSkipped()
      Gets the amount of units that were skipped when reading from file.
      Returns:
      readLinesSkipped - amount of lines skipped when reading army from file because of invalid unit.
    • writeToFile

      public void writeToFile(Army army, String filePath) throws NullPointerException, FileExtensionException, IOException
      Creates a file to write on or writes to an existing file. The client should handle any exception that is thrown with an error message the client creates.
      Parameters:
      army - Army - A collection of units that is written to a file
      filePath - String - name of the file and its full path
      Throws:
      NullPointerException - thrown if the army is a null object
      FileExtensionException - thrown if the file extension is not csv because of the file name
      IOException - thrown if an I/O error occurs
    • readArmyFromFile

      public Army readArmyFromFile(String filePath) throws IOException, FileNotFoundException, IllegalArgumentException
      Finds a specified csv file and converts it to an army if it can. If there is a line which has an unreadable unit it will continue on the next line The client should handle any exception that is thrown with an error message the client creates.
      Parameters:
      filePath - - String - name of the file path to read from
      Returns:
      army - Army - an army read from a csv file
      Throws:
      IOException - thrown if an I/O error occurs
      FileNotFoundException - thrown if the filepath was not found
      IllegalArgumentException - thrown if file extension is not csv or if army could not be created.
    • getFilePath

      public String getFilePath(String fileChooserType)
      Uses the system default file explorer to get a file path of the user's choice. If a user closes the file chooser, null is returned.
      Parameters:
      fileChooserType - String - OPEN or SAVE depending on whether the user wants to open a file or write to a file
      Returns:
      filePath - String - the file path of a csv file for an army