I want to search recipe which match with shopping list items and it will suggest recipe to user based on most important ingredients like in Banana MilkShake {milk and banana} could make banana milkshake alone {sugar and vanilla} are optional,if I add items on shopping list like {banana,milk} it will search recipe on the basis of these ingredients rather {banana,milk,sugar and vanilla}
for example:
Banana Milk Shake Ingredients:
**Ingredient Name
1.banana (important)
2.milk (important)
3.vanilla (optional/not important)
4.sugar (optional/not important)
**Shopping List**
We could also make Banana Milkshake without sugar and vanilla so important ingredients will be milk and banana.
public class Ingredient implements Serializable{
private String name;
private String quantity;
public Ingredient(){
}
public Ingredient(String name,String quantity){
this.name=name;
this.quantity=quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}
and my working code:
public Set<Recipe> searchByIngredient(List<ShoppingList> shop, List<Recipe> list) {
int matchedItems = 0;
Set<Recipe> result = new HashSet<Recipe>();
for (Recipe rn : list) {
boolean recipeMatched = false;
for (ShoppingList r : shop) {
for (int i = 0; i < rn.getIngre().size(); i++) {
if (rn.getIng(i).contains(r.getName())) {
matchedItems++;
int temp = matchedItems;
if(temp >= 2){
result.add(rn);
recipeMatched = true;
}
}
if (recipeMatched)
break;
}
if (recipeMatched)
break;
}
matchedItems = 0;
}
return result;
}
public class Recipe implements Serializable{
private String instructions;
private String recName;
private String image;
private List<Ingredient> ing = new ArrayList<Ingredient>();
public Recipe(){
}
public Recipe(String item,String quan){
this.ing.add(new Ingredient(item, quan));
}
public List<Ingredient> getIngre(){
return ing;
}
public String getIng(int i){
return ing.get(i).toString();
}
public void setIngredients(List<Ingredient> item){
this.ing = item;
}
public String getRecName() {
return recName;
}
public void setRecName(String recName) {
this.recName = recName;
}
// @Override
public String toString() {
return recName;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…