本文介绍了如何根据重要成分搜索配方? Java Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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**

  • 香蕉
  • 牛奶
  • 鸡蛋
  • 米饭
    • banana
    • milk
    • egg
    • rice
    • 我们还可以制作不含糖和香草的香蕉奶昔,因此重要的成分将是牛奶和香蕉.

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

      和我的工作代码:

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

      推荐答案

      根据您的评论.您可以向您的成分类添加另一个变量.像这样:

      Based on your comments. You could add another variable to your Ingredient class. Like this:

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

      然后,您可以检查食谱中是否存在所有重要成分.

      Then you could check in the recipe if all the important ingredients are present or not.

      这篇关于如何根据重要成分搜索配方? Java Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:34