Question
how to get this program to function. It is three separate program that need to function as one for my final project. New to Java
how to get this program to function. It is three separate program that need to function as one for my final project. New to Java so please give me simple instructions so I do not get even more confused. Thanks!
/* * 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 SteppingStones;
import java.util.ArrayList;
import java.util.Scanner;
/** * * @author larrysalaets_snhu */ public class FinalProject2_CollectionManager {
public class Recipe {
private String recipeName;
private int serving;
private ArrayList recipeIngredients;
private double totalRecipeCalories;
public Recipe() {
this.recipeName = "";
this.serving = 0; //<--- assignment value with appropriate data type
this.recipeIngredients = new ArrayList<>(); //<-- assignment value for empty ArrayList
this.totalRecipeCalories = 0;
}
public Recipe(String recipeName, int serving) {
this.recipeName = recipeName;
this.serving = serving;
this.recipeIngredients = new ArrayList<>();
}
//accessor that retrieves the recipe name
public String getRecipeName() {
return recipeName;
}
//mutator that sets the recipe name
public void setRecipeName(String recipeName){
this.recipeName = recipeName;
}
//accessor to retrieve the serving size
public int getServing() {
return serving;
}
//mutator to set the serving size
public void setServing(int serving) {
this.serving = serving;
}
// calls the Array that holds the recipe ingredients
public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
//sets the ingredients held by the array
public void setRecipeIngredients(ArrayList recipeIngredients){
this.recipeIngredients = recipeIngredients;
}
//accessor that calls the total recipe calories
public double getTotalRecipeCalories(){
return totalRecipeCalories;
}
//mutator that sets the total recipe calories
public void setTotalRecipeCalories(double totalRecipeCalories){
this.totalRecipeCalories = totalRecipeCalories;
}
//Recipe class
public Recipe(String recipeName, int servings,ArrayList
recipeIngredients2, double totalRecipeCalories)
//<-- use appropriate data type for the ArrayList and the servings arguments
{
this.recipeName = recipeName;
this.serving = serving;
this.recipeIngredients = recipeIngredients2;
this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() {
System.out.println("Recipe Name: " + this.recipeName);
System.out.println("Servings: " + this.serving);
System.out.println("Total Calories: " + this.totalRecipeCalories);
int i = 0;
System.out.println(" The ingredients of the Reciepe:");
for (Ingredient ing : recipeIngredients){
i++;
ing.printItemDetails();
}
}
//method createNewRecipe() that returns a Recipe
public static Recipe createNewRecipe(String recipeName, int serving)
{
return new Recipe(recipeName, serving);
}
// add Ingredient in recipe
public boolean addIngredient(Ingredient ingredient){
return this.recipeIngredients.add(ingredient);
}
}
Ingredient.java
import java.util.ArrayList;
//define the class Ingredient
public class Ingredient
{
//declare the variables
String nameOfIngredient;
float numberCups;
int numberCaloriesPerCup;
double totalCalories;
private ArrayList ingredientList;
//default constructor
public Ingredient() {
}
//constructor
public Ingredient(String nameOfIngredient, float numberCups,int numberCaloriesPerCup, double totalCalories) {
this.nameOfIngredient = nameOfIngredient;
this.numberCups = numberCups;
this.numberCaloriesPerCup = numberCaloriesPerCup;
this.totalCalories = totalCalories;
}
//getter and setter methods of the instance variables
public String getNameOfIngredient()
{
return nameOfIngredient;
}
public void setNameOfIngredient(String nameOfIngredient)
{
this.nameOfIngredient = nameOfIngredient;
}
public float getNumberCups()
{
return numberCups;
}
public void setNumberCups(float numberCups)
{
this.numberCups = numberCups;
}
public int getNumberCaloriesPerCup()
{
return numberCaloriesPerCup;
}
public void setNumberCaloriesPerCup(int numberCaloriesPerCup)
{
this.numberCaloriesPerCup = numberCaloriesPerCup;
}
public double getTotalCalories()
{
return totalCalories;
}
public void setTotalCalories(double totalCalories)
{
this.totalCalories = totalCalories;
}
//definition of the method addIngredient()
// it takes string and returns Ingredient object
public Ingredient addIngredient(String ingredient) {
Ingredient i = new Ingredient();
i.ingredientList.add(ingredient);
return i;
}
//definition of the method printItemDetails()
// prints the details of the Ingredient.
public void printItemDetails() {
System.out.println("Ingredient Name: " + this.nameOfIngredient);
System.out.println("Number of Cups: " + this.numberCups);
System.out.println("Total Calories: " + this.totalCalories);
System.out.println("Total Calories Per Cup: " + this.numberCaloriesPerCup);
System.out.println();
}
}
Recipe_Test.java
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 ingredientName = scnr.next();
if (ingredientName.toLowerCase().equals("end"))
{
addMoreIngredients = false;
}
else
{
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
totalRecipeCalories = ingredientCalories * ingredientAmount;
Ingredient ingredient1 = new Ingredient(ingredientName,ingredientAmount,ingredientCalories,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();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres a breakdown of your Java code how to make it work and some improvements to streamline the functionality Explanation of Your Code 1 FinalProject2...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started