Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help printing an array list string, i need to keep it a string for my assingment here is my code, how can i

I need help printing an array list string, i need to keep it a string for my assingment here is my code, how can i get it to print out he ingredientAmount since it is a float and it wont print out as a string?

RECIPE.java

package recipe_class;

import java.util.Scanner;

import java.util.ArrayList;

/**

*

* @author gregory.shutter@snhu.edu

*/

public class Recipe {

private String recipeName;

private int servings; //Number of servings

private ArrayList recipeIngredients = new ArrayList(); //an array list for all ingredients in the recipe

private double totalRecipeCalories = 0.0; //this is the total calories of the recipe

public Recipe(String recipeName1, int serving) {

this.recipeName = "";

this.servings = 0;

this.recipeIngredients = new ArrayList<>(); //Array list starts empty

this.totalRecipeCalories = 0;

}

public Recipe(String recipeName, int servings,

ArrayList recipeIngredients, double totalRecipeCalories)

{

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = totalRecipeCalories;

}

public String getRecipeName() {

return recipeName;

}

public void setRecipeName(String recipeName) {

this.recipeName = recipeName;

}

public double getTotalRecipeCalories() {

return totalRecipeCalories;

}

public void setTotalRecipeCalories(double totalRecipeCalories) {

this.totalRecipeCalories = totalRecipeCalories;

}

public ArrayList getRecipeIngredients() {

return recipeIngredients;

}

public void setRecipeIngredients(ArrayList recipeIngredients) {

this.recipeIngredients = recipeIngredients;

}

public int getServings() {

return servings;

}

public void setServings(int servings) {

this.servings = servings;

}

public void printRecipe() {

int singleServingCalories = (int) (totalRecipeCalories / servings);

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

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

System.out.println("Ingredients:");

recipeIngredients.forEach((ingredient) -> {

System.out.println(ingredient);

});

System.out.println(recipeName + " has " + servings + " Servings and each serving has " + singleServingCalories + " Calories.");

System.out.println(recipeName + " has a total of " + totalRecipeCalories + " calories.");

} // I have altered the original print and added one for total calories for better explanation to user

public static Recipe createNewRecipe(String recipeName, int serving)

{

return new Recipe(recipeName, serving);

}

public boolean addIngredient(RecipeTest ingredient){

return this.recipeIngredients.add(ingredient);

}

}

RECIPETEST.java

package recipe_class;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.Iterator;

/**

*

* @author gregory.shutter@snhu.edu

*/

public class RecipeTest {

private String recipeName;

private int servings; //Number of servings

private ArrayList recipeIngredients = new ArrayList(); //an array list for all ingredients in the recipe

private double totalRecipeCalories = 0.0; //this is the total calories of the recipe

private Object scnr;

public RecipeTest() {

this.recipeName = "";

this.servings = 0;

this.recipeIngredients = new ArrayList<>(); //Array list starts empty

this.totalRecipeCalories = 0;

}

public RecipeTest(String recipeName, int servings,

ArrayList recipeIngredients, double totalRecipeCalories)

{

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = totalRecipeCalories;

}

RecipeTest(String recipeName, int serving) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

public String getRecipeName() {

return recipeName;

}

public void setRecipeName(String recipeName) {

this.recipeName = recipeName;

}

public double getTotalRecipeCalories() {

return totalRecipeCalories;

}

public void setTotalRecipeCalories(double totalRecipeCalories) {

this.totalRecipeCalories = totalRecipeCalories;

}

public ArrayList getRecipeIngredients() {

return recipeIngredients;

}

public void setRecipeIngredients(ArrayList recipeIngredients) {

this.recipeIngredients = recipeIngredients;

}

public int getServings() {

return servings;

}

public void setServings(int servings) {

this.servings = servings;

}

public void printRecipe() {

int singleServingCalories = (int) (totalRecipeCalories / servings);

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

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

System.out.println("Ingredients:");

recipeIngredients.forEach((ingredient) -> {

System.out.println(ingredient);

});

{

System.out.println(recipeName + " has " + servings + " Servings and each serving has " + singleServingCalories + " Calories.");

System.out.println(recipeName + " has a total of " + totalRecipeCalories + " calories.");

} // I have altered the original print and added one for total calories for better explanation to user

}

public static void main(String[] args) {

double totalRecipeCalories = 0;

ArrayList recipeIngredients = new ArrayList();

boolean addMoreIngredients = true;

Scanner scnr = new Scanner(System.in);

System.out.println("Please enter the recipe name: ");

String recipeName = "Pizza";

System.out.println("Please enter the number of servings: ");

int servings = 8;

do { //this loop starts the ingredient array list and finishes when users types 'end'

System.out.println("Please enter the ingredient name" +

" or type end if you are finished entering ingredients: ");

String ingredientName = "Cheese";

if (ingredientName.toLowerCase().equals("end")) {

addMoreIngredients = false;

} else {

recipeIngredients.add(ingredientName);

System.out.println("Please enter the ingredient amount: ");

float ingredientAmount = 3;

System.out.println("Please enter the unit of measurement: ");

String unitMeasurement = "Cups";

System.out.println("Please enter the ingredient Calories: ");

int ingredientCalories = 47;

recipeIngredients.add(unitMeasurement);

}

break;

} while (addMoreIngredients);

Recipe recipe1 = new Recipe(recipeName,

servings, recipeIngredients, totalRecipeCalories);

recipe1.printRecipe();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago