/** * Adds two Recipe objects together into a new Recipe object. * * <p>It merges ingredients, summing them if they are on both sides. It adds servings together * into the output. It also accumulates nutrition information for both Recipes. * * @param recipeLeft A recipe object. * @param recipeRight Another recipe object. * @return A new Recipe object with the fields summed together. */ public static Recipe addRecipes(Recipe recipeLeft, Recipe recipeRight) { // copy the left recipe to a new container object Recipe newRecipe = new Recipe(recipeLeft); // add servings from both newRecipe.servings += recipeRight.servings; // add nutrition info from both newRecipe.nutritionInformation.add(recipeRight.nutritionInformation); // assemble new ingredients newRecipe.ingredients = new ArrayList<RecipeIngredient>(); // step one: add known ingredients together for (RecipeIngredient ingredientLeft : recipeLeft.ingredients) // get the ingredient on the right that matches the one on the left for (RecipeIngredient ingredientRight : recipeRight.ingredients) // if the two ingredients have matching names if (ingredientLeft.getName().equals(ingredientRight.getName())) // then add them together on the newIngredients list newRecipe.ingredients.add(ingredientLeft.add(ingredientRight)); // step two: add unknown ingredients from right to left as is for (RecipeIngredient ingredientRight : recipeRight.ingredients) // if the ingredient is unknown if (!newRecipe.ingredients.contains(ingredientRight)) // then add it newRecipe.ingredients.add(ingredientRight); // return new recipe return newRecipe; }
/** * retreives information from the nutritionix api * * @param curRecipe */ public void getNutritionixIngredientInfo(Recipe curRecipe) { ArrayList<NutritionixInfo> nutritionixIngredientList = new ArrayList<NutritionixInfo>(); List<RecipeIngredient> ingredients = this.recipe.getIngredients(); try { // Use the Nutritionix API to lookup nutrition facts info for each one of our ingredients for (RecipeIngredient ingredient : ingredients) { String ingredientName = ingredient.getIngredient().getIngredientName(); nutritionixIngredientList.add(nutritionixBean.searchForIngredient(ingredientName, 0, 1)); } // Calculate and set the nutrition attributes for this class for (RecipeIngredient ingredient : ingredients) { int servings = Math.round(ingredient.getIngredientAmount()); for (NutritionixInfo ni : nutritionixIngredientList) { this.setCalories(servings * (this.getCalories() + ni.getNfCalories())); this.setCaloriesFromFat( servings * (this.getCaloriesFromFat() + ni.getNfCaloriesFromFat())); this.setCholesterol(servings * (this.getCholesterol() + ni.getNfCholesterol())); this.setDietaryFiber(servings * (this.getDietaryFiber() + ni.getNfDietaryFiber())); this.setProtein(servings * (this.getProtein() + ni.getNfProtein())); this.setSaturatedFat(servings * (this.getSaturatedFat() + ni.getNfSaturatedFat())); this.setSodium(servings * (this.getSodium() + ni.getNfSodium())); this.setSugars(servings * (this.getSugars() + ni.getNfSugars())); this.setTotalCarbohydrates( servings * (this.getTotalCarbohydrates() + ni.getNfTotalCarbohydrate())); this.setTotalFat(servings * (this.getTotalFat() + ni.getNfTotalFat())); this.setTransFat(servings * (this.getTransFat() + ni.getNfTransFat())); this.setCalciumPercentage(servings * (this.getCalciumPercentage() + ni.getNfCalciumDv())); this.setIronPercentage(servings * (this.getIronPercentage() + ni.getNfIronDv())); this.setVitaminAPercentage( servings * (this.getVitaminAPercentage() + ni.getNfVitaminADv())); this.setVitaminCPercentage( servings * (this.getVitaminCPercentage() + ni.getNfVitaminCDv())); } } } catch (MalformedURLException ex) { Logger.getLogger(ViewRecipeBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ViewRecipeBean.class.getName()).log(Level.SEVERE, null, ex); } catch (JSONException ex) { Logger.getLogger(ViewRecipeBean.class.getName()).log(Level.SEVERE, null, ex); } }
/** * Scales a Recipe's Ingredients, Serving Size, and Nutritional Information to match the scaled * factor. * * @param recipe Recipe to scale. * @param scaleFactor The factor to scale it by. * @return A new Recipe object with scaled fields. */ public static Recipe scaleRecipe(Recipe recipe, double scaleFactor) { // rebuild and scale ingredients ArrayList<RecipeIngredient> newIngredientsList = new ArrayList<RecipeIngredient>(); for (RecipeIngredient ingredient : recipe.getIngredients()) { if (ingredient.getAmount() == null) newIngredientsList.add(ingredient); else newIngredientsList.add(ingredient.scale(scaleFactor)); } // build the new Recipe and return return new Recipe( recipe.getName(), recipe.getInstructions(), recipe.getServingSize().scale(scaleFactor), recipe.getServings(), recipe.getRating(), newIngredientsList, recipe.getNutritionInformation().scale(scaleFactor)); }
public String getReadableIngredientsList() { if (recipe != null) { String ingredientsListString = ""; List<RecipeIngredient> ingredients = this.recipe.getIngredients(); int ingredientsCount = ingredients.size(); int i = 1; for (RecipeIngredient ingredient : ingredients) { if (i != ingredientsCount) { ingredientsListString += ingredient.getIngredient().getIngredientName() + ", "; } else { ingredientsListString += ingredient.getIngredient().getIngredientName(); } i++; } return ingredientsListString; } else { return ""; } }