I am trying to calculate the cost of a "Recipe" object which contains a hashmap<Ingredient, Double>
I have created the Ingredient class which contains pricePerUnit as one of its values.
The Double is the amount of the ingredients that is needed for the recipe.
I want to calculate the cost of the recipe given that each ingredient instance has a costPerUnit and we know how many units we need for each ingredient.
costOfRecipe = ingredient.costPerUnit * amount (looping through all the 3 ingredients in the hashmap)
I create the recipe with a constructor that takes 3 ingredients (since I don't know how to make a constructor with a variable amount of Ingredients/Amounts - help on this would also be appreciated)
Code below for the ingredient and the recipe class and also the main class which works fine when I run the code. Sorry for the messy comments, I am stumbling as I go and I have accepted some "suggestions" from inteliJ like casting to Ingredient and some others.
Recipe Class
package com.mealplanner; import java.util.*; public class Recipe { String name = null; String category= null; double costOfRecipe; int numberOfIngredients = 0; ArrayList <Ingredient> ingredients = new ArrayList<Ingredient>(); ArrayList<Double> amounts = new ArrayList<Double>(); HashMap<Ingredient, Double> recipe = new HashMap(); public Recipe(){} public Recipe(String name, String category, Ingredient ing1Name, double ing1Amount, Ingredient ing2Name, double ing2Amount,Ingredient ing3Name, double ing3Amount ){ this.name = name; this.category = category; recipe.put(ing1Name, ing1Amount); recipe.put(ing2Name, ing2Amount); recipe.put(ing3Name, ing3Amount); numberOfIngredients = 3; costOfRecipe = calculateCostOfRecipe(recipe); } public String getName(){ return name; } public int getNumberOfIngredients() { return numberOfIngredients; } public HashMap<Ingredient, Double> getRecipe() { return recipe; } /* public static double calculateCostOfRecipe(ArrayList<Ingredient> ingredients, ArrayList<Double> localAmounts){ float costOfRecipe = 0; for (Ingredient i: ingredients) { //*************THIS IS WHERE I AM STUCK********************* int counter = 1; costOfRecipe += i.pricePerUnit* localAmounts.get(counter); counter ++; } return costOfRecipe; } */ public static double calculateCostOfRecipe(HashMap<Ingredient, Double> recipe){ // Need to make the loop through the hashmap and multiply the amount and unit price double recipeCost = 0; List<Ingredient> listKey; List<Double> listValue; for (Map.Entry<Ingredient, Double> entry: recipe.entrySet()) { listKey = (List<Ingredient>) entry.getKey(); listValue = Collections.singletonList(entry.getValue()); //Ingredient i = new Ingredient(); //i.(recipe.keySet()); //if recipe.containsKey(); //recipeCost = recipeCost; } int size = listKey.size(); for(int i =0: i<= size: ++i){ listKey(i) } return recipeCost; } /*void add1Ingredient(){ ingredients. }*/ }
Ingredient Class
package com.mealplanner; public class Ingredient extends Recipe{ String name = null; Boolean organic = false; String unitPurchased = null; String unitOfMeasure; float purchasePrice; float purchasedAmount; float pricePerUnit; public Ingredient() { } public Ingredient(String ingName, String ingUnitPurchased, boolean ingOrganic, float ingAmount, float ingPrice) { name = ingName; unitPurchased = ingUnitPurchased; unitOfMeasure = null; organic = ingOrganic; purchasedAmount = ingAmount; purchasePrice = ingPrice; pricePerUnit = calculatePricePerUnit(this); } public float calculatePricePerUnit(Ingredient ingredient) { String unit = ingredient.unitPurchased; float calculatedPrice = 0; switch (unit) { case "GRAMS" -> { calculatedPrice = (1000f / ingredient.purchasedAmount) * ingredient.purchasePrice; this.setUnitOfMeasure("KILOGRAM"); } case "KILOGRAMS" -> { calculatedPrice = (ingredient.purchasePrice / ingredient.purchasedAmount); this.setUnitOfMeasure("KILOGRAM"); } case "MILLILITERS" -> { calculatedPrice = (1000f / ingredient.purchasedAmount) * ingredient.purchasePrice; this.setUnitOfMeasure("LITER"); } case "LITERS" -> { calculatedPrice = (ingredient.purchasePrice / ingredient.purchasedAmount); this.setUnitOfMeasure("LITER"); } case "CUPS" -> { calculatedPrice = (1000f / (ingredient.purchasedAmount * 0.237f)) * ingredient.purchasePrice; this.setUnitOfMeasure("LITER"); } case "TABLESPOONS" -> { calculatedPrice = (1000f / (ingredient.purchasedAmount * 0.0147868f)) * ingredient.purchasePrice; this.setUnitOfMeasure("LITER"); } case "TEASPOONS" -> { calculatedPrice = (1000f / (ingredient.purchasedAmount * 0.00492892f)) * ingredient.purchasePrice; this.setUnitOfMeasure("LITER"); } } return calculatedPrice; } public String getUnitPurchased() { return unitPurchased; } public void setUnitPurchased(String unitPurchased) { this.unitPurchased = unitPurchased; } public Boolean getOrganic() { return organic; } public float getPricePerUnit() { return pricePerUnit; } public float getPurchasedAmount() { return purchasedAmount; } public float getPurchasePrice() { return purchasePrice; } public String getName() { return name; } public String getUnitOfMeasure() { return unitOfMeasure; } public void setName(String name) { this.name = name; } public void setOrganic(Boolean organic) { this.organic = organic; } public void setPricePerUnit(float pricePerUnit) { this.pricePerUnit = pricePerUnit; } public void setPurchasedAmount(float purchasedAmount) { this.purchasedAmount = purchasedAmount; } public void setPurchasePrice(float purchasePrice) { this.purchasePrice = purchasePrice; } public void setUnitOfMeasure(String unitOfMeasure) { this.unitOfMeasure = unitOfMeasure; } public enum UnitPurchased { GRAMS, KILOGRAMS, MILLILITERS, LITERS, CUPS, TABLESPOONS, TEASPOONS } public enum UnitOfMeasure { LITER, KILOGRAM } }
Main Class
package com.mealplanner; import java.util.Scanner; public class Main { public static void main(String[] args){ /*System.out.println("Welcome to the meal planer"); System.out.println("type a unit for your new ingredient"); Scanner scanner = new Scanner(System.in); String userInput = scanner.nextLine(); Ingredient apple = new Ingredient(); apple.setUnitPurchased(userInput); System.out.println(apple.getUnitPurchased());*/ Ingredient avocado = new Ingredient("avocado", "GRAMS", true,200,5 ); //System.out.println(avocado.name + ", " + avocado.pricePerUnit + ", " + avocado.unitOfMeasure); System.out.println(String.format("Your fruit is %s, which costs %f dollars per %s.", avocado.name, avocado.pricePerUnit, avocado.unitOfMeasure)); Ingredient rice = new Ingredient("rice", "KILOGRAMS", false, 3, 9); Ingredient pepper = new Ingredient("pepper", "GRAMS", true, 500, 5); Recipe stirFry = new Recipe("Stir Fry", "Dinner", avocado, 500, rice, 0.5, pepper, 1); System.out.println("You are about to cook " + stirFry.getName() + "!"); System.out.println("Your recipe has " + stirFry.getNumberOfIngredients() +" ingredients."); System.out.println("Ingredient list and amounts: "+ stirFry.getRecipe()); System.out.println(stirFry.costOfRecipe); System.out.println(stirFry.recipe.keySet()); } }