-
Notifications
You must be signed in to change notification settings - Fork 1
Update search algo #46
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| package com.google.sps.data; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Meal { | ||
| private final Long id; | ||
|
|
@@ -50,4 +51,28 @@ public ArrayList<String> getIngredients() { | |
| public String getType() { | ||
| return this.type; | ||
| } | ||
|
|
||
| /** | ||
| * Counts the number of keywords that are matching in recipe. | ||
| * @param params list of strings with keywords, that describe search request. | ||
| * @return amount of occurrences of keywords from params in this meal. | ||
| */ | ||
| public int getFrequency(List<String> params) { | ||
| int frequency = 0; | ||
| for (String param : params) { | ||
| if (title.contains(param) || | ||
| description.contains(param) || | ||
| type.contains(param)) { | ||
| frequency++; | ||
| continue; | ||
| } | ||
| for (String ingredient : ingredients) { | ||
| if (ingredient.contains(param)) { | ||
| frequency++; | ||
| continue; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
then as long as the word "chocolate" is not in the title / description / type, returned frequency will be 3. However, if the title does include the word "chocolate", we'll return early with I believe you may have meant to break the outer loop instead. I think the simplest solution is to extract the second for loop to a method, e.g. |
||
| } | ||
| } | ||
| } | ||
| return frequency; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest adding a unit test for this method in a MealTest.java file.
A test that would check the exact number returned by this method for some instances.