Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
idatt2106-v23-03
backend
Commits
3190b8b2
Commit
3190b8b2
authored
1 year ago
by
Stian Lyng
Browse files
Options
Downloads
Patches
Plain Diff
added random shuffle of zero match recipes
parent
734030fa
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java
+21
-5
21 additions, 5 deletions
...a/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java
with
21 additions
and
5 deletions
src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java
+
21
−
5
View file @
3190b8b2
...
...
@@ -6,7 +6,9 @@ import ntnu.idatt2016.v233.SmartMat.entity.Recipe;
import
ntnu.idatt2016.v233.SmartMat.entity.user.User
;
import
ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -165,14 +167,22 @@ public class RecipeService {
return
ResponseEntity
.
ok
(
"Recipe deleted from favorites"
);
}
}
/**
* Returns a list of RecipeWithMatchCount objects representing the weekly menu based on ingredients in a fridge.
* The list contains recipes with matched ingredients and additional recipes with no matching ingredients
* (if there are less than 5 recipes with matching ingredients). Recipes with 0 match count are shuffled before returning.
*
* @param fridgeId The ID of the fridge to get the weekly menu for
* @return A list of RecipeWithMatchCount objects representing the weekly menu
*/
public
List
<
RecipeWithMatchCount
>
getWeeklyMenu
(
Integer
fridgeId
)
{
// Get the results from both repository methods
List
<
Object
[]>
weeklyMenu
=
recipeRepository
.
findWeeklyMenu
(
fridgeId
);
List
<
Object
[]>
recipeProducts
=
recipeRepository
.
findRecipeProductsWithName
();
// Prepare a map to store RecipeDetails with their match count
Map
<
RecipeDetails
,
Integer
>
recipeMatchCountMap
=
new
HashMap
<>();
// Prepare a map to store RecipeDetails with their match count
Map
<
RecipeDetails
,
Integer
>
recipeMatchCountMap
=
new
HashMap
<>();
// Compare the item_name on both lists
for
(
Object
[]
menuRow
:
weeklyMenu
)
{
...
...
@@ -208,12 +218,18 @@ public class RecipeService {
.
collect
(
Collectors
.
toList
());
// Add additional recipes from uniqueRecipeDetails with a count of 0 if the list has less than 5 recipes
List
<
RecipeWithMatchCount
>
zeroMatchRecipes
=
new
ArrayList
<>();
for
(
RecipeDetails
recipeDetails
:
uniqueRecipeDetails
)
{
if
(
commonRecipes
.
size
()
<
5
&&
!
recipeMatchCountMap
.
containsKey
(
recipeDetails
))
{
common
Recipes
.
add
(
new
RecipeWithMatchCount
(
recipeDetails
,
0
));
zeroMatch
Recipes
.
add
(
new
RecipeWithMatchCount
(
recipeDetails
,
0
));
}
}
// Shuffle the zeroMatchRecipes list
Collections
.
shuffle
(
zeroMatchRecipes
);
// Combine the commonRecipes and zeroMatchRecipes lists
commonRecipes
.
addAll
(
zeroMatchRecipes
);
return
commonRecipes
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment