如何根据重要成分搜索食谱? java android

时间:2016-06-01 17:52:38

标签: java android arraylist

我想搜索与购物清单项目匹配的食谱,并根据最重要的成分建议用户食谱,例如Banana MilkShake {牛奶和香蕉}可以单独使用香蕉奶昔{糖和香草}是可选的,如果我在购物清单上添加物品,如{banana,milk},它将根据这些成分搜索食谱,而不是{香蕉,牛奶,糖和香草} 例如:

Banana Milk Shake Ingredients:

**Ingredient Name                
  1.banana                         (important)
  2.milk                           (important)
  3.vanilla                        (optional/not important)
  4.sugar                          (optional/not important) 

**Shopping List**
  • 香蕉

我们还可以制作没有糖和香草的香蕉奶昔,所以重要的成分将是牛奶和香蕉。

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;
    }

}

和我的工作代码:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的评论。您可以在Ingredient类中添加另一个变量。像这样:

public class Ingredient implements Serializable{

    private String name;
    private String quantity;
    private boolean isImportant;

    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;
    }

    public boolean getIsImportant() {
        return isImportant;
    }

    public void setIsImportant(boolean isImportant) {
        this.isImportant= isImportant;
    }

}

然后你可以检查配方中是否存在所有重要成分。