Class FileHandler

java.lang.Object
no.ntnu.idatt1002.demo.data.recipes.FileHandler

public class FileHandler extends Object
The FileHandler class is a static class that handles reading and writing to the .register files for storing dinner recipes and ingredients at hand. Files of this class are stored at src/main/resources/recipes.
Author:
hannesofie
  • Constructor Details

    • FileHandler

      public FileHandler()
  • Method Details

    • writeRegister

      public static void writeRegister(RecipeRegister recipeRegister, String fileDestination)
      The method takes a RecipeRegister object and a String as parameters. The recipe register is then written to a file named after the provided string, with the file-type ".register" in the /main/recourses/recipes folder. The file is written at the following format and is only sensitive to new-lines within the instructions-String: # Recipe name - Ingredient 1 | amount | unit - Ingredient 2 | amount | unit - ... Instructions An IllegalArgumentException is thrown if the recipe register is null. IOExceptions may occur upon writing to file, in which case the stacktrace is printed to terminal.
      Parameters:
      recipeRegister - A recipe register object that is to be written to file.
      fileDestination - The path and title by which to name the .register-file.
    • formatRecipe

      public static StringBuilder formatRecipe(Recipe recipe)
      The method supports the method for writing a recipe register to file by taking in a single recipe object and return it as a String at the correct format for storage.
      Parameters:
      recipe - A recipe object to format into a String.
      Returns:
      A String representation of the recipe at the specified format for file storage.
    • formatIngredientList

      public static StringBuilder formatIngredientList(List<RecipeIngredient> ingredientList)
      The method supports the 'formatRecipe' method by receiving a list of ingredient objects and returning a String at the correct format for the writing to file.
      Parameters:
      ingredientList - A list of ingredients to be formatted into a String.
      Returns:
      A String of the ingredients at the correct format for writing to file.
    • readRecipeRegister

      public static RecipeRegister readRecipeRegister(String fileDestination) throws IOException
      The method reads a recipe register from file and returns the recipe register object. The title of the file is provided as a String. If the file doesn't exist, a message is written to the terminal and null is returned instead of a recipe register. Each recipe is separated by a '#'-sign and passed on to the method 'readRecipe' that reads and returns each Recipe object for the register.
      Parameters:
      fileDestination - Title of the .register file at which the recipe register is saved.
      Returns:
      A recipe register object read from file.
      Throws:
      IOException
    • readRecipe

      public static Recipe readRecipe(String fileDestination)
      The method supports the readRecipeRegister method by receiving a string containing the information needed to create one specific recipe object. The method reads the needed information from this String and returns the Recipe object based on this. The beginning of each ingredient line is recognized by a hyphen ('-'), while the instructions are all lines, including internal new-lines, that do not start with a hyphen.
      Parameters:
      fileDestination - A String representation of the path and recipe to read from file.
      Returns:
      A recipe object based on the provided string representation.
    • writeIngredientsAtHand

      public static void writeIngredientsAtHand(IngredientsAtHand ingredientsAtHand, String fileDestination) throws IOException
      The method takes in an IngredientsAtHand object and writes it to a .register-file with the provided String as title. The file contains no other information than a simple list of FoodItem constants separated by new-line. The resulting file is stored at /main/recourses/recipes/. An IllegalArgumentException is thrown if the ingredients at hand object is null. IOExceptions may occur upon writing to file, in which case the stacktrace is printed to terminal.
      Parameters:
      ingredientsAtHand - An IngredientsAtHand object that holds a collection of constants of the FoodItem enum class.
      fileDestination - The path and title by which to name the file that the ingredients at hand are written to.
      Throws:
      IOException - if an input or output error occurs.
    • readIngredientsAtHand

      public static IngredientsAtHand readIngredientsAtHand(String fileDestination) throws IOException
      The method reads an IngredientsAtHand object from the .register file with the name provided as a parameter and stored at /main/recourses/recipes/. If the file is not found, a FileNotFoundException is thrown and null is returned instead of a IngredientsAtHand object.
      Parameters:
      fileDestination - Path and title of the file to read the IngredientsAtHand object from.
      Returns:
      An IngredientsAtHand object based on the provided .register file.
      Throws:
      IOException