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

Create best-fit-recipes picker in RecipeRegister

parent 94c03500
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
......@@ -19,6 +19,5 @@ public class RecipeTileController {
this.recipe = recipe;
nameTag.setText(recipe.getName());
missingTag.setText(Integer.toString(recipe.getMissingIngredients()));
}
}
......@@ -11,10 +11,6 @@ import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.FileHandling;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
import no.ntnu.idatt1002.demo.data.recipes.*;
import java.io.IOException;
......@@ -86,7 +82,7 @@ public class SuggestRecipesController implements Initializable {
fridgeList.setItems(fridge);
}
public void addToFridge(String ingredient) {
/* public void addToFridge(String ingredient) {
System.out.println("Add to fridge!");
String toRemove = fridgeList.getSelectionModel().getSelectedItem();
ingredientsAtHand.addIngredient(FoodItem.valueOf(toRemove.toUpperCase()));
......@@ -95,14 +91,12 @@ public class SuggestRecipesController implements Initializable {
fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList());
fridgeList.setItems(fridge);
}
}*/
@FXML
private void goBack(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
System.out.printf("Back button pressed");
loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml"));
Parent root = loader.load();
......@@ -110,13 +104,19 @@ public class SuggestRecipesController implements Initializable {
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
private void setRecipeTiles(int number) {
// Ingredeints at hand and recipesRegister
Recipe[] recipes = recipeRegister.pickBestFits(4, ingredientsAtHand);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// If no ingredients at hand file exsists, add one and let it be empty. //TODO
ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge");
/*fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand());*/
fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList());
......@@ -127,5 +127,8 @@ public class SuggestRecipesController implements Initializable {
recipeRegister = FileHandler.readRecipeRegister("Recipes");
recipes = FXCollections.observableArrayList(recipeRegister.getRecipes());
// Get the number from FX-grid available?
setRecipeTiles(4);
}
}
......@@ -5,6 +5,7 @@ import java.util.List;
public class IngredientsAtHand {
//TODO: go through JavaDoc after refactoring.
//TODO: Make into a set
private final ArrayList<FoodItem> ingredientsAtHand = new ArrayList<>();
......
package no.ntnu.idatt1002.demo.data.recipes;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class RecipeRegister {
......@@ -29,4 +31,17 @@ public class RecipeRegister {
.findFirst().orElse(null);
}
public Recipe[] pickBestFits(int number, IngredientsAtHand atHand) {
Recipe[] recipes;
this.recipes.forEach(r -> r.updateIngredientStatus(atHand));
recipes = (Recipe[]) this.recipes.stream()
.sorted(Comparator.comparingInt(Recipe::getMissingIngredients))
.limit(number)
.toArray();
return recipes;
}
}
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