Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

how to search recipe based on important ingredients ? java android

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

}

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...