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

Make Recipe controllers to provide test data and fill in grid with suggested recipes

parent 3e3112ec
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
package no.ntnu.idatt1002.demo.controller;
public class RecipeController {
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import no.ntnu.idatt1002.demo.data.recipes.*;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;
public class RecipeController implements Initializable {
@FXML
private GridPane recipeTiles;
private List<Recipe> recipes;
// For testing purposes at frontend:
private List<Recipe> getSuggestions() {
RecipeRegister recipeRegister = new RecipeRegister();
List<Recipe> suggestedRecipes = null;
IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 6, MeasuringUnit.PC));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.POTATO, 4, MeasuringUnit.PC));
ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 8, MeasuringUnit.DL));
Recipe recipe1 = new Recipe("Recipe no. 1", "Description");
recipe1.addIngredient(FoodItem.LEMON, 5, MeasuringUnit.PC);
recipe1.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC);
Recipe recipe2 = new Recipe("Recipe no. 2", "Description" );
recipe2.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC);
recipe2.addIngredient(FoodItem.TOMATO, 6, MeasuringUnit.PC);
recipe2.addIngredient(FoodItem.ORANGE, 1, MeasuringUnit.PC);
recipeRegister.addRecipe(recipe1);
recipeRegister.addRecipe(recipe2);
recipeRegister.getRecipes().forEach((recipe) -> recipe.updateIngredientStatus(ingredientsAtHand));
recipeRegister.getRecipes().sort(Comparator.comparing(Recipe::getMissingIngredients));
List<Recipe> sortedRecipes = recipeRegister.getRecipes();
for(int i =0; i<4; i++) {
if(sortedRecipes.get(i) != null) {
suggestedRecipes.add(sortedRecipes.get(i));
} else {
suggestedRecipes.add(null);
}
}
return suggestedRecipes;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
recipes.addAll(getSuggestions());
int column = 0;
int row = 0;
try {
for(int i = 0; i<4; i++) {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/src/main/resources/view/RecipeTile.fxml"));
AnchorPane anchorPane = fxmlLoader.load();
RecipeTileController recipeTileController = fxmlLoader.getController();
recipeTileController.setData(recipes.get(i));
if(column == 3){
column = 0;
row++;
}
recipeTiles.add(anchorPane, column++, row);
GridPane.setMargin(anchorPane, new Insets(10));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package no.ntnu.idatt1002.demo.controller;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import no.ntnu.idatt1002.demo.data.recipes.Recipe;
public class RecipeTileController {
@FXML
private Label nameTag;
@FXML
private Label missingTag;
private Recipe recipe;
public void setData(Recipe recipe) {
this.recipe = recipe;
nameTag.setText(recipe.getName());
missingTag.setText(Integer.toString(recipe.getMissingIngredients()));
}
}
......@@ -101,6 +101,7 @@ public class Recipe {
if(inRecipe.getFoodType() == atHand.getFoodType()) {
inRecipe.setAtHand(true);
} else {
inRecipe.setAtHand(false);
missingIngredients += 1;
}
});
......@@ -108,6 +109,9 @@ public class Recipe {
}
}
public int getMissingIngredients() {
return missingIngredients;
}
@Override
......
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