Skip to content
Snippets Groups Projects
Commit 9b5cefb4 authored by HSoreide's avatar HSoreide
Browse files

FileHandler writes and reads ingredients at hand from file

parent eb177b1b
No related branches found
No related tags found
3 merge requests!42Hs frontend recipes,!41Hs frontend recipes,!36Hs rewrite file handling for recipes and ingredients
package no.ntnu.idatt1002.demo.data.recipes;
import no.ntnu.idatt1002.demo.data.Economics.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.nio.file.FileSystems;
import java.time.LocalDate;
//TODO: Refactor to make use of generics, which requires
// refactoring of more classes and write mor eunit tests and JavaDoc.
public class FileHandler {
// private static final String filePath = FileSystems.getDefault().getPath("src", "main", "resources", "recipes").toString();
private static final String fileType = ".register";
private static final String registerName = "RecipeRegister";
private static final String filePath = "src/main/resources/recipes/";
/* private static final String date = "date=";
private static final String description = "description=";
private static final String amount = "amount=";
private static final String isRecurring = "isRecurring=";
private static final String category = "category=";*/
public static void writeRegister(RecipeRegister recipeRegister, String title) {
if(recipeRegister == null) {
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 {
......@@ -42,7 +29,7 @@ public class FileHandler {
}
);
} catch ( IOException e ) {
} catch (IOException e) {
e.printStackTrace();
}
}
......@@ -52,17 +39,17 @@ public class FileHandler {
sb.append("# ")
.append(recipe.getName())
.append("\n")
.append(formatIngredientList(recipe))
.append(formatIngredientList(recipe.getIngredientList()))
.append("\n")
.append(recipe.getInstructions())
.append("\n\n");
return sb;
}
public static StringBuilder formatIngredientList(Recipe recipe) {
public static StringBuilder formatIngredientList(List<RecipeIngredient> ingredientList) {
StringBuilder sb = new StringBuilder();
recipe.getIngredientList().forEach((ingredient) -> {
ingredientList.forEach((ingredient) -> {
sb.append("- ")
.append(ingredient.getFoodType())
.append(" | ")
......@@ -80,14 +67,12 @@ public class FileHandler {
public static void readToTerminal(String title) throws FileNotFoundException {
File file = new File(filePath + title + fileType);
Scanner sc = new Scanner(file);
while(sc.hasNext()) {
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
}
public static RecipeRegister readRecipeRegister(String title) throws FileNotFoundException {
File file = new File(filePath + title + fileType);
......@@ -99,11 +84,11 @@ public class FileHandler {
while (sc.hasNext()) {
line = sc.next();
if(!line.isBlank()) {
if (!line.isBlank()) {
register.addRecipe(readRecipe(line));
}
}
}catch(FileNotFoundException e) {
} catch (FileNotFoundException e) {
System.out.println("The file was not found.");
return null;
}
......@@ -127,7 +112,7 @@ public class FileHandler {
while (sc.hasNextLine()) {
line = sc.nextLine();
if(line.startsWith("-")) {
if (line.startsWith("-")) {
String[] ingredientParts = line.split("\\|");
FoodItem ingredientType = FoodItem.valueOf(ingredientParts[0].replaceFirst("-", "").strip());
......@@ -137,7 +122,7 @@ public class FileHandler {
recipe.addIngredient(ingredientType, ingredientAmount, ingredientUnit);
} else {
if(!line.isBlank()) {
if (!line.isBlank()) {
sb.append(line).append("\n");
}
}
......@@ -146,4 +131,50 @@ public class FileHandler {
return recipe;
}
}
// ================ Ingredients at hand ===============================
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.");
}
StringBuilder sb = new StringBuilder();
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) {
throw new RuntimeException(e);
}
}
}
public static IngredientsAtHand readIngredientsAtHand(String title) {
File file = new File(filePath + title + fileType);
IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
try (Scanner sc = new Scanner(file)) {
String line;
while (sc.hasNext()) {
line = sc.next();
if (!line.isBlank()) {
ingredientsAtHand.addIngredient(FoodItem.valueOf(line));
}
}
} catch (FileNotFoundException e) {
System.out.println("The file was not found.");
return null;
}
return ingredientsAtHand;
}
}
\ No newline at end of file
......@@ -5,12 +5,15 @@ 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.*;
class FileHandlerTest {
RecipeRegister recipeRegister = new RecipeRegister();
IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
Recipe recipe1;
Recipe recipe2;
......@@ -32,6 +35,14 @@ class FileHandlerTest {
recipeRegister.addRecipe(recipe1);
recipeRegister.addRecipe(recipe2);
// For AtHand:
ingredientsAtHand.addIngredient(FoodItem.POTATO);
ingredientsAtHand.addIngredient(FoodItem.MILK);
ingredientsAtHand.addIngredient(FoodItem.LEMON);
ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT);
}
......@@ -64,5 +75,15 @@ class FileHandlerTest {
}
// Ingredients at hand nesting
@Test
@DisplayName("Write ingredients at hand to file.")
void writeIngredientsAtHandToFile() throws FileNotFoundException {
assertAll(() -> FileHandler.writeIngredientsAtHand(ingredientsAtHand, "AtHandRegister"));
FileHandler.readToTerminal("AtHandRegister");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment