diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java index 5784f342ca586d2df1a05f827872d064d03b45d5..108f13437ba6d6a412f927335b558b88cae4c6cd 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java @@ -70,7 +70,6 @@ public class AddIngredientController implements Initializable { listView.getItems().clear(); listView.getItems().addAll(searchList(searchBar.getText(), Arrays.stream(FoodItem.values()).toList().stream().map(value -> value.label).toArray(String[]::new))); // String[] - } @@ -89,7 +88,4 @@ public class AddIngredientController implements Initializable { Platform.runLater(() -> searchBar.requestFocus()); status.setWrapText(true); } - -//TODO: Add label with status message that fades in 5 sec. - } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java index cc6df94ee7805888d50601ccdb8661e2e152948e..9605cd70be7a57f0b6b4a7e830d958babb9416b8 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FileHandler.java @@ -4,21 +4,39 @@ import java.io.*; import java.util.List; import java.util.Scanner; -//TODO: Refactor to make use of generics, which requires -// refactoring of more classes and write mor eunit tests and JavaDoc. +/** + * 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 + */ public class FileHandler { private static final String fileType = ".register"; - private static final String registerName = "RecipeRegister"; private static final String filePath = "src/main/resources/recipes/"; + /** + * 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. + * + * @param recipeRegister A recipe register object that is to be written to file. + * @param title The title by which to name the .register-file. + */ public static void writeRegister(RecipeRegister recipeRegister, String title) { if (recipeRegister == null) { throw new IllegalArgumentException("Only a valid register object can be written to file."); } - try (FileWriter fileWriter = new FileWriter(filePath + title + fileType);) { + try (FileWriter fileWriter = new FileWriter(filePath + title + fileType)) { recipeRegister.getRecipes().forEach((recipe) -> { try { @@ -34,6 +52,13 @@ public class FileHandler { } } + /** + * 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. + * + * @param recipe A recipe object to format into a String. + * @return A String representation of the recipe at the specified format for file storage. + */ public static StringBuilder formatRecipe(Recipe recipe) { StringBuilder sb = new StringBuilder(); sb.append("# ") @@ -46,33 +71,35 @@ public class FileHandler { return sb; } + /** + * 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. + * + * @param ingredientList A list of ingredients to be formatted into a String. + * @return A String of the ingredients at the correct format for writing to file. + */ public static StringBuilder formatIngredientList(List<RecipeIngredient> ingredientList) { StringBuilder sb = new StringBuilder(); - ingredientList.forEach((ingredient) -> { - sb.append("- ") - .append(ingredient.getFoodType()) - .append(" | ") - .append(ingredient.getAmount()) - .append(" | ") - .append(ingredient.getUnit()) - .append("\n"); - }); - + ingredientList.forEach((ingredient) -> sb.append("- ") + .append(ingredient.getFoodType()) + .append(" | ") + .append(ingredient.getAmount()) + .append(" | ") + .append(ingredient.getUnit()) + .append("\n")); return sb; } - - //TODO: Midlertidig for testing. - public static void readToTerminal(String title) throws FileNotFoundException { - File file = new File(filePath + title + fileType); - Scanner sc = new Scanner(file); - while (sc.hasNext()) { - System.out.println(sc.nextLine()); - } - } - - + /** + * 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. + * + * @param title Title of the .register file at which the recipe register is saved. + * @return A recipe register object read from file. + */ public static RecipeRegister readRecipeRegister(String title) { File file = new File(filePath + title + fileType); @@ -92,12 +119,19 @@ public class FileHandler { System.out.println("The file was not found."); return null; } - return register; } + /** + * 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. + * + * @param readRecipe A String representation of one recipe as read from file. + * @return A recipe object based on the provided string representation. + */ public static Recipe readRecipe(String readRecipe) { - Scanner sc = new Scanner(readRecipe); Recipe recipe; @@ -105,7 +139,6 @@ public class FileHandler { String recipeName = sc.nextLine().strip(); StringBuilder sb = new StringBuilder(); - String line; recipe = new Recipe(recipeName, instructions); @@ -120,22 +153,26 @@ public class FileHandler { MeasuringUnit ingredientUnit = MeasuringUnit.valueOf(ingredientParts[2].strip()); recipe.addIngredient(ingredientType, ingredientAmount, ingredientUnit); - } else { sb.append(line).append("\n"); - if (!line.isBlank()) { - // sb.append(line).append("\n"); - } } } recipe.setInstructions(String.valueOf(sb).strip()); - return recipe; } - // ================ Ingredients at hand =============================== - + /** + * 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. + * + * @param ingredientsAtHand An IngredientsAtHand object that holds a collection of constants of the + * FoodItem enum class. + * @param title The title by which to name the file that the ingredients at hand are written to. + */ public static void writeIngredientsAtHand(IngredientsAtHand ingredientsAtHand, String title) throws IOException { if (ingredientsAtHand == null) { throw new IllegalArgumentException("Only a valid register object can be written to file."); @@ -143,11 +180,8 @@ public class FileHandler { StringBuilder sb = new StringBuilder(); - try (FileWriter fileWriter = new FileWriter(filePath + title + fileType);) { - ingredientsAtHand.getIngredientsAtHand().forEach((ingredient) -> { - sb.append(ingredient).append("\n"); - - }); + try (FileWriter fileWriter = new FileWriter(filePath + title + fileType)) { + ingredientsAtHand.getIngredientsAtHand().forEach((ingredient) -> sb.append(ingredient).append("\n")); try { fileWriter.write(String.valueOf(sb)); } catch (IOException e) { @@ -156,7 +190,13 @@ public class FileHandler { } } - + /** + * 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. + * @param title Title of the file to read the IngredientsAtHand object from. + * @return An IngredientsAtHand object based on the provided .register file. + */ public static IngredientsAtHand readIngredientsAtHand(String title) { File file = new File(filePath + title + fileType); @@ -175,7 +215,6 @@ public class FileHandler { System.out.println("The file was not found."); return null; } - return ingredientsAtHand; } } \ No newline at end of file diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java index 40f1718f138384a4ba2421ba196c473422fda0ea..36f392aca07e6fcb4131c50e6d8c1fdd3ecd0a8c 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/FileHandlerTest.java @@ -3,10 +3,7 @@ package no.ntnu.idatt1002.demo.data.recipes; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; - import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; import static org.junit.jupiter.api.Assertions.*; @@ -42,7 +39,6 @@ class FileHandlerTest { ingredientsAtHand.addIngredient(FoodItem.LEMON); ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT); - } @@ -50,7 +46,6 @@ class FileHandlerTest { @DisplayName("Write recipe register correctly to file as text.") void writeRecipeRegisterToFile() throws FileNotFoundException { assertAll(() -> FileHandler.writeRegister(recipeRegister, "RecipeRegister")); - //FileHandler.readToTerminal("RecipeRegister"); } @Test @@ -66,7 +61,6 @@ class FileHandlerTest { // Write the register object to file and read to terminal: FileHandler.writeRegister(registerReadFromTestFile, "ReadAndWrittenRegister"); - //FileHandler.readToTerminal("ReadAndWrittenRegister"); // Another iteration to spot accumulating spaces or new lines. RecipeRegister secondRegister = FileHandler.readRecipeRegister("ReadAndWrittenRegister"); @@ -85,5 +79,12 @@ class FileHandlerTest { //FileHandler.readToTerminal("AtHandRegister"); } + @Test + @DisplayName("Read ingredients at hand to file.") + void readIngredientsAtHandToFile() throws FileNotFoundException { + //assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, "AtHandRegister")); + //FileHandler.readToTerminal("AtHandRegister"); + } + } \ No newline at end of file