Need help creating adding a driver application to the program for these two mile
ID: 3881241 • Letter: N
Question
Need help creating adding a driver application to the program for these two milestones
Milestone 1
*/
public class Ingredient {
private String nameOfIngredient; //The String name of the ingredient
private int numberCups; //The amount of te ingredient, can be fractional
private int numberCaloriesPerCup; //The amount of unit
private String unitofMeasure; //The amount of calories
public Ingredient(String nameOfIngredient, int numberCups, int numberCaloriesPerCup, String unitofMeasure) {
this.nameOfIngredient = nameOfIngredient;
this.numberCups = numberCups;
this.numberCaloriesPerCup = numberCaloriesPerCup;
this.unitofMeasure = unitofMeasure;
}
public String getNameOfIngredient() {
return nameOfIngredient;
}
public int getNumberCups() {
return numberCups;
}
public int getNumberCaloriesPerCup() {
return numberCaloriesPerCup;
}
public double getTotalCalories() {
return numberCaloriesPerCup * numberCups;
}
@Override
public String toString() {
Milestone 2
import java.util.Scanner;
import java.util.ArrayList;
public class SteppingStone5_Recipe {
private String recipeName;
private double totalRecipeCalories;
private ArrayList<String> recipeIngredients;
private int servings;
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
}
public SteppingStone5_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:");
for (String ingredient : recipeIngredients) {
System.out.println(ingredient);
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
public static void main(String[] args) {
SteppingStone5_Recipe recipe1 = createNewRecipe();
recipe1.printRecipe();
}
public static SteppingStone5_Recipe createNewRecipe() {
double totalRecipeCalories = 0;
ArrayList<String> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
Scanner scnr = new Scanner(System.in);
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();
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 {
recipeIngredients.add(ingredientName);
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);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
scnr.close();
return recipe1;
}
}
Explanation / Answer
public class Ingredient {
private String nameOfIngredient; //The String name of the ingredient
private int numberCups; //The amount of te ingredient, can be fractional
private int numberCaloriesPerCup; //The amount of unit
private String unitofMeasure; //The amount of calories
public Ingredient(String nameOfIngredient, int numberCups, int numberCaloriesPerCup, String unitofMeasure) {
this.nameOfIngredient = nameOfIngredient;
this.numberCups = numberCups;
this.numberCaloriesPerCup = numberCaloriesPerCup;
this.unitofMeasure = unitofMeasure;
}
public String getNameOfIngredient() {
return nameOfIngredient;
}
public int getNumberCups() {
return numberCups;
}
public int getNumberCaloriesPerCup() {
return numberCaloriesPerCup;
}
public double getTotalCalories() {
return numberCaloriesPerCup * numberCups;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Ingredient [nameOfIngredient=" + nameOfIngredient + ", numberCups=" + numberCups + ", numberCaloriesPerCup="
+ numberCaloriesPerCup + ", unitofMeasure=" + unitofMeasure + "]";
}
}
import java.util.ArrayList;
/**
* Recipe class :
* create one recipe
*
*/
//in your SteppingStone5_Recipe class you use Ingredient array as string change it to Ingredient
public class SteppingStone5_Recipe {
//variables
private ArrayList<Ingredient> recipeIngredients;
private String recipeName;
private int servings;
private double totalRecipeCalories;
//constructor
public SteppingStone5_Recipe(String recipeName,int servings) {
this.recipeName = recipeName;
this.recipeIngredients = new ArrayList<>();
this.servings = servings;
this.totalRecipeCalories = 0;
}
//get IngredientList
public ArrayList<Ingredient> getIngredientList() {
return recipeIngredients;
}
//set IngredientList
public void setIngredientList(ArrayList<Ingredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
/**
* @return the recipeName
*/
public String getRecipeName() {
return recipeName;
}
/**
* @param recipeName the recipeName to set
*/
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
/**
* @return the servings
*/
public int getServings() {
return servings;
}
/**
* @param servings the servings to set
*/
public void setServings(int servings) {
this.servings = servings;
}
/**
* @return the totalRecipeCalories
*/
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
/**
* @param totalRecipeCalories the totalRecipeCalories to set
*/
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
// add Ingredient in recipe
public boolean addIngredient(Ingredient ingredient){
this.totalRecipeCalories = this.totalRecipeCalories + ingredient.getTotalCalories();
return this.recipeIngredients.add(ingredient);
}
// remove Ingredient in recipe
public boolean removeIngredient(Ingredient ingredient){
if (this.recipeIngredients.remove(ingredient)){
this.totalRecipeCalories = this.totalRecipeCalories - ingredient.getTotalCalories();
return true;
}
else
return false;
}
// remove all Ingredient in recipe
public boolean removeAllIngredient(){
return this.recipeIngredients.removeAll(recipeIngredients);
}
// print Ingredients in recipe
public void printRecipe(){
int singleServingCalories = (int) (totalRecipeCalories/servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
for(Ingredient ingredient: recipeIngredients) {
System.out.println(ingredient);
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
//create new recipe
public static SteppingStone5_Recipe createNewRecipe(String recipeName,int servings){
return new SteppingStone5_Recipe(recipeName,servings);
}
}
/**
* SteppingStone5_Recipe_Test class :
* collection of recipe
*
*/
public class SteppingStone_Recipe_Test {
private ArrayList<SteppingStone5_Recipe> recipes;
public SteppingStone_Recipe_Test() {
this.recipes = new ArrayList<>();
}
//add a Recipe in collection
public void addItem(SteppingStone5_Recipe recipe){
recipes.add(recipe);
}
//delete a Recipe in collection
public void deleteItem(SteppingStone5_Recipe recipe){
recipes.remove(recipe);
}
//print all recipes
public void printAllRecipies(){
int i = 0;
for(SteppingStone5_Recipe rec: recipes){
i++;
System.out.println("Recipe " + i);
rec.printRecipe();
}
}
public static void main(String[] args) {
//create some demo recipes
SteppingStone5_Recipe recipe1 = SteppingStone5_Recipe.createNewRecipe("Pizza", 4);
//you can also take value using scanner
Ingredient ingredient1 = new Ingredient("Anchovies",2,300,"gram");
recipe1.addIngredient(ingredient1);
SteppingStone5_Recipe recipe2 = SteppingStone5_Recipe.createNewRecipe("Ramen", 2);
Ingredient ingredient2 = new Ingredient("Noodles",5,200,"gram");
recipe2.addIngredient(ingredient2);
// add in box
SteppingStone_Recipe_Test box = new SteppingStone_Recipe_Test();
box.addItem(recipe1);
box.addItem(recipe2);
box.printAllRecipies();
}
}
Sample Output:
Recipe 1
Recipe: Pizza
Serves: 4
Ingredients:
Ingredient [nameOfIngredient=Anchovies, numberCups=2, numberCaloriesPerCup=300, unitofMeasure=gram]
Each serving has 150 Calories.
Recipe 2
Recipe: Ramen
Serves: 2
Ingredients:
Ingredient [nameOfIngredient=Noodles, numberCups=5, numberCaloriesPerCup=200, unitofMeasure=gram]
Each serving has 500 Calories.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.