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 5f5ad545a128657db5d563776322db5af6759bc1..f5529fda9c96afebc89b1e5a8204a325b697559e 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java @@ -88,7 +88,7 @@ public class AddIngredientController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - ingredientsAtHand = FileHandler.readIngredientsAtHand("AtHandRegister"); + ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge"); /* ObservableList ingredients = FXCollections.observableArrayList(testIngredients);*/ /* ObservableList<FoodItem> ingredients = FXCollections.observableArrayList( diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java index 74b5ea8000e98b23d73b648aed83349bb1ca14f2..1248f75a8a08f376ef7672e40f58830c0dd076cc 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java @@ -1,10 +1,21 @@ package no.ntnu.idatt1002.demo.controller; +import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; +import javafx.scene.Node; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.Label; +import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.recipes.Recipe; -public class RecipeTileController { +import java.io.IOException; +import java.net.URL; +import java.util.ResourceBundle; + +public class RecipeTileController implements Initializable { @FXML private Label nameTag; @@ -12,12 +23,30 @@ public class RecipeTileController { @FXML private Label missingTag; - private Recipe recipe; + @FXML + private void tileClick(ActionEvent event) throws IOException { + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/view/Recipe.fxml")); - public void setData(Recipe recipe) { + // nameTag of action tile --> register get recipe(string name) --> Pass on to Controller of Recipe + + Parent root = loader.load(); + Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); + Scene scene = new Scene(root); + stage.setScene(scene); + stage.show(); + } - this.recipe = recipe; + + public void setData(Recipe recipe) { nameTag.setText(recipe.getName()); missingTag.setText(Integer.toString(recipe.getMissingIngredients())); + System.out.println(missingTag.getText()); + } + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + + nameTag.setWrapText(true); } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java index 9edb97ce6699ecc125d3631f88650ce24ea9f4f9..c56853cc65732e464fe43ff23c3feed8477e0450 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java @@ -10,11 +10,15 @@ import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.*; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.recipes.*; import java.io.IOException; import java.net.URL; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.ResourceBundle; @@ -37,10 +41,16 @@ public class SuggestRecipesController implements Initializable { @FXML private ListView<String> fridgeList; + @FXML + private GridPane recipeTiles; + private ObservableList<String> fridge; private ObservableList<Recipe> recipes; + private final int NUMBER_OF_TILES = 4; + + @FXML private void addIngredient(ActionEvent event) throws IOException { @@ -60,9 +70,11 @@ public class SuggestRecipesController implements Initializable { if (clickedButton.get() == ButtonType.APPLY) { // Refresh ingredientsAtHand. ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge"); - //TODO: Duplicate + //TODO: Duplicate code and assertion. + assert ingredientsAtHand != null; fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList()); fridgeList.setItems(fridge); + setRecipeTiles(); System.out.println("Clickecked OK!"); }else if(clickedButton.get() == ButtonType.CANCEL) { @@ -76,10 +88,12 @@ public class SuggestRecipesController implements Initializable { System.out.println("Remove the selected from fridge!"); String toRemove = fridgeList.getSelectionModel().getSelectedItem(); ingredientsAtHand.removeIngredient(FoodItem.valueOf(toRemove.replace(" ", "_").toUpperCase())); + FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge"); //TODO: Remove toUppercase solution above. //TODO: Consider factoring out to a update method. fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList()); fridgeList.setItems(fridge); + setRecipeTiles(); } /* public void addToFridge(String ingredient) { @@ -106,10 +120,37 @@ public class SuggestRecipesController implements Initializable { stage.show(); } - private void setRecipeTiles(int number) { + private void setRecipeTiles() { // Ingredeints at hand and recipesRegister - Recipe[] recipes = recipeRegister.pickBestFits(4, ingredientsAtHand); + ArrayList<Recipe> recipes = recipeRegister.pickBestFits(NUMBER_OF_TILES, ingredientsAtHand); + + + int i =0; + int j = 0; + for(Recipe r : recipes) { + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/view/RecipeTile.fxml")); + + + if(i > 1) { + j++; + i=0; + } + + try { + VBox vBox = loader.load(); + RecipeTileController recipeTileController = loader.getController(); + recipeTileController.setData(r); + + recipeTiles.add(vBox, j, i); + } catch (IOException e) { + throw new RuntimeException(e); + } + i++; + } + + // Include numbering? } @@ -128,7 +169,7 @@ public class SuggestRecipesController implements Initializable { recipes = FXCollections.observableArrayList(recipeRegister.getRecipes()); // Get the number from FX-grid available? - setRecipeTiles(4); + setRecipeTiles(); } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java index 94c956d7149385d2a83597e69b90c4849081cf11..338b03fd9facbf5638a9c12723c765e79c73decd 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java @@ -4,7 +4,7 @@ public enum FoodItem { ONION("onion"), MINCED_MEAT("minced meat"), - POTATO("potatoes"), + POTATO("potato"), YELLOW_CHEESE("yellow cheese"), WHEAT_FLOUR("wheat flour"), MILK("milk"), diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java index 39e84d8a1934d82e259762eba24639560f2d8078..287357a03ebda4665708ff9e2675896a5af61b6c 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java @@ -90,39 +90,20 @@ public class Recipe { */ public void updateIngredientStatus(IngredientsAtHand ingredientsAtHand) { // Will need a supporting class for converting between units to be accurate. - if(ingredientsAtHand == null) { - throw new NullPointerException("The ingredients at hand object must exist"); - } else if (ingredientsAtHand.getIngredientsAtHand().size() < 1) { - throw new IllegalArgumentException("The collection of ingredients at hand is empty."); - } else { - missingIngredients = 0; - int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count(); - ingredientList.forEach((inRecipe) -> { - inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType())); - }); - - }; - - /* ingredientList.forEach((inRecipe) -> { - ingredientsAtHand.getIngredientsAtHand().forEach((atHand) -> { - - System.out.println("----"); - System.out.println(inRecipe.getFoodType()); - System.out.println(atHand); - System.out.println("----"); - - if(inRecipe.getFoodType() == atHand) { - inRecipe.setAtHand(true); - - - } else { - inRecipe.setAtHand(false); - missingIngredients += 1; - } - }); - });*/ - } + if (ingredientsAtHand == null) { + throw new NullPointerException("The ingredients at hand object must exist"); + } else if (ingredientsAtHand.getIngredientsAtHand().size() < 1) { + throw new IllegalArgumentException("The collection of ingredients at hand is empty."); + } else { + missingIngredients = 0; + int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count(); + ingredientList.forEach((inRecipe) -> { + inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType())); + }); + missingIngredients = ingredientList.size()-notMissing; + } + } public int getMissingIngredients() { diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java index 8108bf77be11983bc7378e0f4650c32089ea0922..a83029e3d56becb9c0849fc32488633c8a00c6f8 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeRegister.java @@ -31,15 +31,15 @@ public class RecipeRegister { .findFirst().orElse(null); } - public Recipe[] pickBestFits(int number, IngredientsAtHand atHand) { - Recipe[] recipes; + + public ArrayList<Recipe> pickBestFits(int number, IngredientsAtHand atHand) { + ArrayList<Recipe> recipes; this.recipes.forEach(r -> r.updateIngredientStatus(atHand)); - recipes = (Recipe[]) this.recipes.stream() + recipes = this.recipes.stream() .sorted(Comparator.comparingInt(Recipe::getMissingIngredients)) - .limit(number) - .toArray(); + .limit(number).collect(Collectors.toCollection(ArrayList::new)); return recipes; } diff --git a/src/main/resources/recipes/Fridge.register b/src/main/resources/recipes/Fridge.register index fb1ef97c3dafb895dfa2bf4f030bcf5e2bdb2483..d5ce30e17a4aa5cc84cbff34cd61d329e1d0d890 100644 --- a/src/main/resources/recipes/Fridge.register +++ b/src/main/resources/recipes/Fridge.register @@ -1,8 +1,3 @@ -POTATO MILK LEMON -MINCED_MEAT -YELLOW_CHEESE -ORANGE -SALSA_SAUCE -ONION +POTATO diff --git a/src/main/resources/style.css b/src/main/resources/style.css index 013ec34c43a0110b13003521c72c1145e5aab704..1bf5da84a6a5b37b868ac02e41d7bf7ade8dc5f5 100644 --- a/src/main/resources/style.css +++ b/src/main/resources/style.css @@ -1,6 +1,6 @@ .recipe-tile { - -fx-background-color: rgb(215, 153, 27); + -fx-background-color: rgba(252, 203, 80, 0.8); -fx-background-radius: 25; } \ No newline at end of file diff --git a/src/main/resources/view/Recipe.fxml b/src/main/resources/view/Recipe.fxml index fde20f4c24c080a4e39f14e296bc4b2f7fae7659..d45c6e867306ba6e3596fa6c4ae3e12410876f94 100644 --- a/src/main/resources/view/Recipe.fxml +++ b/src/main/resources/view/Recipe.fxml @@ -1,14 +1,52 @@ <?xml version="1.0" encoding="UTF-8"?> -<?import java.lang.*?> -<?import java.util.*?> -<?import javafx.scene.*?> <?import javafx.scene.control.*?> +<?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> +<?import javafx.scene.text.*?> -<AnchorPane xmlns="http://javafx.com/javafx" - xmlns:fx="http://javafx.com/fxml" - fx:controller="view.Recipe" - prefHeight="400.0" prefWidth="600.0"> - +<AnchorPane prefHeight="695.0" prefWidth="1130.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.RecipeController"> + <children> + <ImageView fitHeight="695.0" fitWidth="1130.0" pickOnBounds="true" preserveRatio="true"> + <image> + <Image url="@../Images/backgroundMini.jpg" /> + </image></ImageView> + <BorderPane prefHeight="695.0" prefWidth="1130.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> + <top> + <HBox prefHeight="136.0" prefWidth="1130.0" BorderPane.alignment="CENTER"> + <children> + <Pane prefHeight="200.0" prefWidth="200.0"> + <children> + <Button layoutX="76.0" layoutY="26.0" mnemonicParsing="false" text="Button"> + <font> + <Font size="14.0" /> + </font></Button> + </children> + </Pane> + <Label fx:id="recipeName" text="RecipeName" textAlignment="CENTER"> + <font> + <Font size="36.0" /> + </font> + </Label> + </children> + </HBox> + </top> + <center> + <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER"> + <children> + <VBox prefHeight="457.0" prefWidth="265.0" /> + <TextFlow prefHeight="457.0" prefWidth="625.0" /> + </children> + </HBox> + </center> + <left> + <Pane prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" /> + </left> + <bottom> + <Pane prefHeight="102.0" prefWidth="1130.0" BorderPane.alignment="CENTER" /> + </bottom> + <right> + <Pane prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" /> + </right></BorderPane> + </children> </AnchorPane> diff --git a/src/main/resources/view/RecipeTile.fxml b/src/main/resources/view/RecipeTile.fxml index e48ac477f63b999c7408947ecb1fcedefe46bf3a..c31bad309b9a04c52b1cd1c244830dd8ee36aa1a 100644 --- a/src/main/resources/view/RecipeTile.fxml +++ b/src/main/resources/view/RecipeTile.fxml @@ -5,39 +5,39 @@ <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> -<AnchorPane id="recipe-tile" fx:id="recipeTile" prefHeight="300.0" prefWidth="400.0" styleClass="recipe-tile" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.RecipeTileController"> +<VBox fx:id="recipeTile" onMouseClicked="#tileClick" prefHeight="220.0" prefWidth="280.0" styleClass="recipe-tile" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.RecipeTileController"> <children> - <VBox prefHeight="350.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> + <Pane> <children> - <Pane prefHeight="262.0" prefWidth="500.0"> - <children> - <Label id="recipeTitle" fx:id="nameTag" alignment="CENTER" contentDisplay="CENTER" layoutY="3.0" prefHeight="237.0" prefWidth="400.0" text="Name of Recipe"> - <font> - <Font name="System Bold" size="36.0" /> - </font> - </Label> - </children></Pane> - <HBox prefHeight="88.0" prefWidth="500.0"> - <children> - <Label alignment="CENTER" prefHeight="63.0" prefWidth="292.0" text="Ingredients missing:"> - <font> - <Font size="24.0" /> - </font> - <opaqueInsets> - <Insets left="20.0" right="20.0" /> - </opaqueInsets> - </Label> - <Label id="noMissingIngredients" fx:id="missingTag" alignment="CENTER" contentDisplay="CENTER" prefHeight="69.0" prefWidth="127.0" text="# missing"> - <font> - <Font size="24.0" /> - </font> - </Label> - </children> + <Label id="recipeTitle" fx:id="nameTag" alignment="CENTER" contentDisplay="CENTER" layoutY="3.0" prefHeight="190.0" prefWidth="280.0" text="Name of Recipe" textAlignment="CENTER"> + <font> + <Font name="System Bold" size="24.0" /> + </font> + <padding> + <Insets left="10.0" /> + </padding> + </Label> + </children> + </Pane> + <HBox prefHeight="88.0" prefWidth="500.0"> + <children> + <Label alignment="CENTER" prefHeight="63.0" prefWidth="292.0" text="Ingredients missing:"> + <font> + <Font size="18.0" /> + </font> <opaqueInsets> <Insets left="20.0" right="20.0" /> </opaqueInsets> - </HBox> + </Label> + <Label id="noMissingIngredients" fx:id="missingTag" alignment="CENTER" contentDisplay="CENTER" prefHeight="69.0" prefWidth="127.0" text="# missing"> + <font> + <Font size="24.0" /> + </font> + </Label> </children> - </VBox> + <opaqueInsets> + <Insets left="20.0" right="20.0" /> + </opaqueInsets> + </HBox> </children> -</AnchorPane> +</VBox> diff --git a/src/main/resources/view/SuggestRecipes.fxml b/src/main/resources/view/SuggestRecipes.fxml index dbde1809a1a61c0db8597086fbd805773865a9c9..824ed52da531390a272024fa77a0be1812b8d7f5 100644 --- a/src/main/resources/view/SuggestRecipes.fxml +++ b/src/main/resources/view/SuggestRecipes.fxml @@ -78,7 +78,7 @@ </BorderPane> </top> <center> - <GridPane fx:id="recipeTiles" BorderPane.alignment="CENTER"> + <GridPane fx:id="recipeTiles" hgap="20.0" prefWidth="603.0" vgap="20.0" BorderPane.alignment="CENTER"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />