Hello please help with creating a user-defined method with control structure that calculates the cost of the recipe. I also need a test file to provide sample output. Any assistance would be greatly appreciated.
so far I have:
/* * 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 steppingstonesTesting; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; /** * * @author rijackson */ public class Recipe1 { private String recipeName; private int servings; private List<String> recipeIngredients; private int totalRecipeCalories; // default constructor. public Recipe1() { this.recipeName = ""; this.servings = 0; this.recipeIngredients = new ArrayList<String>(); this.totalRecipeCalories = 0; } // overloaded constructor. public Recipe1(String recipeName, int servings, List<String> recipeIngredients, int totalRecipeCalories) { this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = totalRecipeCalories; } // accessors and mutators public String getRecipeName() { return recipeName; } public void setRecipeName(String recipeName) { this.recipeName = recipeName; } public int getServings() { return servings; } public void setServings(int servings) { this.servings = servings; } public List<String> getRecipeIngredients() { return recipeIngredients; } public void setRecipeIngredients(List<String> recipeIngredients) { this.recipeIngredients = recipeIngredients; } public int getTotalRecipeCalories() { return totalRecipeCalories; } public void setTotalRecipeCalories(int totalRecipeCalories) { this.totalRecipeCalories = totalRecipeCalories; } public void printRecipe() { int singleServingCalories = (totalRecipeCalories / servings); System.out.println("Recipe: " + recipeName); System.out.println("Serves: " + servings); System.out.println("Ingredients: "); for (int i = 0; i < recipeIngredients.size(); i++) { String ingredient = recipeIngredients.get(i); System.out.println(ingredient); } System.out.println("Each Serving Has " + singleServingCalories + " Calories."); } // // static method to create new recipe. public static Recipe1 createNewRecipe() { Scanner sc = new Scanner(System.in); System.out.print("\nEnter recipe name: "); String recipeName = sc.nextLine(); System.out.print("\nEnter number of servings: "); int numServings = sc.nextInt(); sc.nextLine(); // next line in the input. System.out.println("\nEnter all ingredients (separated by commas): "); List<String> ingredients = Arrays.asList(sc.nextLine().split(",")); System.out.println("\nEnter total calories of the recipe: "); int totalCalories = sc.nextInt(); //user input for total calories sc.nextLine(); // next line in the input. // create new recipe object and return it. return new Recipe1(recipeName, numServings, ingredients, totalCalories); } }
and this for a different test file
package steppingstonesTesting; /* * 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. */ import java.util.Arrays; public class RecipeTest { public static void main(String[] args) { // create one hardcoded recipe. String[] ingredients = {"Rice", "Chicken", "Chilli", "Onion", "Tomato", "Masala", "Ginger", "Garlic"}; Recipe1 masalachickenRecipe = new Recipe1("Chicken Masala", 2, Arrays.asList(ingredients), 1400); masalachickenRecipe.printRecipe(); // create a new recipe by taking input from the user. Recipe1 createdRecipe = Recipe1.createNewRecipe(); createdRecipe.printRecipe(); } }