Question: Need help with java code solution I have a code im working on, I can get the desired reults ill post code and the results

Need help with java code solution

I have a code im working on, I can get the desired reults ill post code and the results i get:

Recipe.Java

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package recipecollection;

import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner;

/** * */

public class Recipe {

private String recipeName; private int serving; //Number of servings private ArrayList recipeIngredients; //an array list for all ingredients in the recipe private double totalRecipeCalories; //this is the total calories of the recipe public Recipe() { this.recipeName = ""; this.serving = 0; this.recipeIngredients = new ArrayList<>(); //Array list starts empty this.totalRecipeCalories = 0;

} public Recipe(String recipeName, int serving) { this.recipeName = recipeName; this.serving = serving; this.recipeIngredients = new ArrayList<>();

}

public Recipe(String recipeName, int servings,ArrayList recipeIngredients2, double totalRecipeCalories) {

this.recipeName = recipeName; this.serving = serving; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = totalRecipeCalories;

} public String getRecipeName() {

return recipeName;

}

public void setRecipeName(String recipeName){

this.recipeName = recipeName;

}

public int getServing() {

return serving;

}

public void setServing(int serving) {

this.serving = serving;

}

public ArrayList getRecipeIngredients() {

return recipeIngredients;

}

public void setRecipeIngredients(ArrayList recipeIngredients){

this.recipeIngredients = recipeIngredients;

}

public double getTotalRecipeCalories(){

return totalRecipeCalories;

}

public void setTotalRecipeCalories(double totalRecipeCalories){

this.totalRecipeCalories = totalRecipeCalories;

}

public void printRecipe() { int singleServingCalories = (int) (totalRecipeCalories / serving);

System.out.println("Recipe: " + recipeName);

System.out.println("Serves: " + serving);

System.out.println("Total Calories: " + totalRecipeCalories);

int i = 0;

System.out.println(" The ingredients of the Recipe: ");

for (Ingredient ing : recipeIngredients) { i++; ing.printItemDetails(); } }

public static Recipe createNewRecipe(String recipeName, int serving)

{ return new Recipe(recipeName, serving); }

public boolean addIngredient(Ingredient ingredient){ return this.recipeIngredients.add(ingredient);

}

}

Ingredient.Java

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package recipecollection;

import java.util.ArrayList;

public class Ingredient {

/** * */

String nameOfIngredient; //name of ingredient float ingredientAmount; // number of cups, tsp, tbsp, ounces String unitMeasurement; //The measurement unit int numberCaloriesPerMeasure; //calories per measurement unit double totalCalories; //total calories of ingredient private ArrayList ingredientList;

public Ingredient() {

}

public Ingredient(String nameOfIngredient, float ingredientAmount,int numberCaloriesPerMeasure, double totalCalories) {

this.nameOfIngredient = nameOfIngredient; this.ingredientAmount = ingredientAmount; this.numberCaloriesPerMeasure = numberCaloriesPerMeasure; this.totalCalories = totalCalories;

}

Ingredient(String ingredientName, float ingredientAmount, String unitMeasurement, int numberCaloriesPerMeasure, double totalRecipeCalories) { }

public String getnameOfIngredient(){ return nameOfIngredient;

}

public void setnameOfIngredient(String nameOfIngredient) { this.nameOfIngredient = nameOfIngredient;

}

public float getingredientAmount(){ return ingredientAmount;

}

public void setingredientAmount(float ingredientAmount){ this.ingredientAmount = ingredientAmount;

}

public String getunitMeasurement(){ return unitMeasurement;

}

public void setunitMeasurement(String unitMeasurement){ this.unitMeasurement = unitMeasurement;

} public int getnumberCaloriesPerMeasure() { return numberCaloriesPerMeasure;

}

public void setnumberCaloriesPerMeasure(int numberCaloriesPerMeasure) { this.numberCaloriesPerMeasure = numberCaloriesPerMeasure;

}

public double getTotalCalories() { return totalCalories;

}

public void setTotalCalories(double totalCalories) { this.totalCalories = totalCalories;

}

public Ingredient addIngredient(String ingredient) { Ingredient i = new Ingredient(); i.ingredientList.add(ingredient); return i; }

public void printItemDetails() {

System.out.println("Ingredient Name: " + this.nameOfIngredient); System.out.println("Number of Units: " + this.ingredientAmount); System.out.println("Measurement Type: " + this.unitMeasurement); System.out.println("Total Calories: " + this.totalCalories); System.out.println("Total Calories Per Cup: " + this.numberCaloriesPerMeasure); System.out.println();

}

}

Recipe_Test

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package recipecollection;

import java.util.ArrayList;

import java.util.Scanner;

public class Recipe_Test

{

public static void main(String[] args) { double totalRecipeCalories = 0; ArrayList recipeIngredients = new ArrayList(); boolean addMoreIngredients = true; String reply="";

Scanner scnr = new Scanner(System.in); double totalCal=0;

System.out.println("Please enter the recipe name: "); String recipeName = scnr.nextLine();

System.out.println("Please enter the number of servings: "); int servings = scnr.nextInt();

Recipe recipe1 = Recipe.createNewRecipe(recipeName, servings);

do { System.out.println("Please enter the ingredient name or type end if you are finished entering ingredients: "); String nameOfIngredient = scnr.next();

if (nameOfIngredient.toLowerCase().equals("end"))

{ addMoreIngredients = false; }

else { System.out.println("Please enter the ingredient amount: "); float ingredientAmount = scnr.nextFloat();

System.out.println("Please enter the unit of measurement: "); String unitMeasurement = scnr.next(); System.out.println("Please enter the number of calories per measurement: "); int numberCaloriesPerMeasure = scnr.nextInt();

totalRecipeCalories = numberCaloriesPerMeasure * ingredientAmount;

Ingredient ingredient1 = new Ingredient(nameOfIngredient,ingredientAmount,unitMeasurement,numberCaloriesPerMeasure,totalRecipeCalories); recipe1.addIngredient(ingredient1);

totalCal = recipe1.getTotalRecipeCalories()+totalRecipeCalories; recipe1.setTotalRecipeCalories(totalCal);

System.out.println("Do you want to continue. Y/N");

reply = scnr.next(); } } while (!reply.equals("n")) ; scnr.close(); recipe1.printRecipe();

}

}

these are the results I get... Why do I get null and 0's for the ingredients of the recipe if information is entered?

run: Please enter the recipe name: Pizza Please enter the number of servings: 8 Please enter the ingredient name or type end if you are finished entering ingredients: Cheese Please enter the ingredient amount: 3 Please enter the unit of measurement: Cups Please enter the number of calories per measurement: 123 Do you want to continue. Y/N n Recipe: Pizza Serves: 8 Total Calories: 369.0

The ingredients of the Recipe:

Ingredient Name: null

Number of Units: 0.0 Measurement Type: null Total Calories: 0.0 Total Calories Per Cup: 0

BUILD SUCCESSFUL (total time: 15 seconds)

Thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!