fix this code so i dont get errors: package final_project; import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Pattern; /** * * @author hudael-haj_snhu */ class Ingredient {
fix this code so i dont get errors:
package final_project;
import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Pattern;
/** * * @author hudael-haj_snhu */ class Ingredient { /** * @param args the command line arguments * unitMeasurement as String * nameOfIngredient as String * ingredientAmount as float * numberOfCaloriesPerCup as integer * totalCalories as double */ /*Declaring the parameters required in this class */ private String unitMeasurement; //accepts the unit measurement as cups, ounces, etc. private String nameOfIngredient; //accepts the name of ingredient private float ingredientAmount; // accepts the amount of ingredient private int numberOfCaloriesPerCup; // accepts the number of calories private double totalCalories; // accepts the total calories /*default constructor*/ public Ingredient(){ super(); } /*Parameterized constructor*/ /** * @param nameOfIngredient * @param ingredientAmount * @param unitMeasurement * @param numberOfCaloriesPerCup * @param totalCalories */ public Ingredient(String nameOfIngredient, float ingredientAmount, String unitMeasurement, int numberOfCaloriesPerCup, double totalCalories){ super(); this.nameOfIngredient = nameOfIngredient; this.ingredientAmount = ingredientAmount; this.unitMeasurement = unitMeasurement; this.numberOfCaloriesPerCup = numberOfCaloriesPerCup; this.totalCalories = totalCalories; } /*To get the Name of Ingredient*/ public String getNameOfIngredient(){ return nameOfIngredient; } /*To set the Name of Ingredient*/ public void setNameOfIngredient(String nameOfIngredient){ this.nameOfIngredient = nameOfIngredient; } /*To get the Ingredient Amount*/ public float getIngredientAmount(){ return ingredientAmount; } /*To set the Ingredient Amount*/ public void setIngredientAmount(float ingredientAmount){ this.ingredientAmount = ingredientAmount; } /*To get the Unit Measurement*/ public String getUnitMeasurement(){ return unitMeasurement; } /*To set the Unit Measurement*/ public void setUnitMeasurement(String unitMeasurement){ this.unitMeasurement = unitMeasurement; } /*To get the Number of Calories Per Cup*/ public int getNumberOfCaloriesPerCup(){ return numberOfCaloriesPerCup; } /*To set the Number of Calories Per Cup*/ public void setNumberOfCaloriesPerCup(int numberOfCaloriesPerCup){ this.numberOfCaloriesPerCup = numberOfCaloriesPerCup; } /*To get the Total Calories*/ public double getTotalCalories(){ return totalCalories; } /** * Calculates the total calories * @return */ public double totalCalories(){ /*Calculate the total calories that a ingredient has with the number of cups used and the number of calories per cup*/ return (double)(ingredientAmount * numberOfCaloriesPerCup); } /* * Boolean Method to validate the name of the ingredient. * This method accepts String as input because the ingredient name is a String. * parameter tmpNameOfIngredient is being passed from the main method to get validated. * returns true or false */ public boolean validateNameOfIngredient(String tmpNameOfIngredient){ String regex ="^[a-zA-Z 0-9-]+$";// Regular Expression to validate the name /* If statement to verify the name with the regex as below * if matches --> set the name to the variable and return true * if not matches --> return false */ if(Pattern.matches(regex, tmpNameOfIngredient)){ setNameOfIngredient(tmpNameOfIngredient); return true; } else{ return false; } } /* * Boolean Method to validate the Ingredient Amount * This method accepts float as input because the ingredient amount is a float * parameter tmpIngredientAmount is being passed from the main method to get validated. * returns true or false. */ public boolean validateIngredientAmount(float tmpIngredientAmount){ /* If statement to verify the ingredient amount is greater than 0 * if value is greater than or equal to 0 --> set the ingredientAmount to the variable and return true * if value is less than 0 --> return false */ if(tmpIngredientAmount >= 0){ setIngredientAmount(tmpIngredientAmount); return true; } else{ return false; } } /* * Boolean Method to validate the Unit Measurement. * This method accepts String as input because the unit measurement is a String. * parameter tmpUnitMeasurement is being passed from the main method to get validated. * returns true or false. */ public boolean validateUnitMeasurement(String tmpUnitMeasurement){ String regex ="^[a-zA-Z 0-9-]+$";// Regular Expression to validate the unit measurement /* If statement to verify the unit measurement with the regex as below * if matches --> set the unit measurement to the variable and return true * if not matches --> return false */ if(Pattern.matches(regex, tmpUnitMeasurement)){ setUnitMeasurement(tmpUnitMeasurement); return true; } else{ return false; } } /* * Boolean Method to validate the Number of Calories per Cup. * This method accepts Integer as input because the Number of Calories Per Cup is an Integer. * parameter tmpNumberOfCaloriesPerCup is being passed from the main method to get validated. * returns true or false. */ public boolean validateNumberOfCaloriesPerCup(int tmpNumberOfCaloriesPerCup){ /* If statement to verify the number of calories per cup is greater than 0 * if value is greater than or equal to 0 --> set the numberOfCaloriesPerCup to the variable and return true * if value is less than 0 --> return false */ if(tmpNumberOfCaloriesPerCup >= 0){ setNumberOfCaloriesPerCup(tmpNumberOfCaloriesPerCup); return true; } else{ return false; } } /* * Main Method to run and validates the variables * This will finally display the Total Calories for an ingredient name and the number of cups. * @param args */ public static void main(String[] args) { Ingredient in = new Ingredient(); // creating ingredient object String tmpUnitMeasurement; String tmpNameOfIngredient; float tmpIngredientAmount; int tmpNumberOfCaloriesPerCup; boolean result; int i = 0, j =0 ,k =0, l =0;// Variables for initializing in while loop for different variables /* Start of Name of Ingredient variable input and validation*/ Scanner scnrName = new Scanner(System.in); // Scanner to fetch the Name of Ingredient /*Prompts for user to enter the ingredient's name*/ System.out.println("Please enter the name of the ingredient: "); tmpNameOfIngredient = scnrName.nextLine().trim(); // Assigns the IngredientName to the temporary variable. /*Temporary variable of Ingredient Name is being passed to the Validation method*/ result = in.validateNameOfIngredient(tmpNameOfIngredient); /* while loop used to print the messages as per below * If the validation method returns false then the application will prompt for the user to enter the name again. * Application prompts twice if the input is not correct and then finally exits. */ while(result == false){ if(i>=1){ System.out.println("Entered Name of Ingredient: " + tmpNameOfIngredient + " is not a valid string. Sorry you are out of attempts!" ); return; } else{ System.out.println("Entered Name of Ingredient: " + tmpNameOfIngredient + " is not a valid string" ); System.out.println("Please enter the name of the ingredient: "); tmpNameOfIngredient = scnrName.nextLine().trim(); result = in.validateNameOfIngredient(tmpNameOfIngredient); } i++; }
Scanner scnrAmount = new Scanner(System.in);// Scanner to fetch the Ingredient Amount /*Prompts for user to enter the number of cups for the ingredient*/ System.out.println("Please enter the number of cups of " + in.nameOfIngredient + " we'll need: "); String stringIngredientAmount; //To get the Ingredient Amount as a string which can be used to verify if there are whitespaces in the variable stringIngredientAmount =scnrAmount.nextLine(); // Assigns the Ingredient Amount to the temporary variable. /*To verify if the entered Ingredient Amount is float or not with the regex */ if(Pattern.matches("^[+-]?([0-9]*[.])?[0-9]+$", stringIngredientAmount)){ /*Parse the string, convert it to float and store to the temporary variable*/ tmpIngredientAmount = Float.parseFloat(stringIngredientAmount.trim()); /*Temporary variable of Ingredient Amount is being passed to the Validation method*/ result = in.validateIngredientAmount(tmpIngredientAmount); /* while loop used to print the messages as per below * If the validation method returns false then the application will prompt for the user to enter the ingredient amount again. * Application prompts twice if the input is not correct and then finally exits. */ while(result== false){ if(j>=1){ System.out.println("Entered value for Ingredient Amount: "+ tmpIngredientAmount +" is a negative number. Sorry you are out of attempts!"); return; } else{ System.out.println("Entered value for Ingredient Amount: "+ tmpIngredientAmount +" is a negative number" ); System.out.println("Please enter the number of cups of " + in.nameOfIngredient + " we'll need: "); stringIngredientAmount = scnrAmount.nextLine(); if(Pattern.matches("^[+-]?([0-9]*[.])?[0-9]+$", stringIngredientAmount)){ tmpIngredientAmount = Float.parseFloat(stringIngredientAmount.trim()); result = in.validateIngredientAmount(tmpIngredientAmount); } else{ System.out.println("Entered number of Cups: "+stringIngredientAmount+ " is not valid float value. Sorry you are out of attempts!"); return; } } j++; } } else{ System.out.println("Entered number of Cups: "+stringIngredientAmount+ " is not valid float value"); System.out.println("Please enter the number of cups of " + in.nameOfIngredient + " we'll need: "); stringIngredientAmount = scnrAmount.nextLine(); if(Pattern.matches("^[+-]?([0-9]*[.])?[0-9]+$", stringIngredientAmount)){ tmpIngredientAmount = Float.parseFloat(stringIngredientAmount.trim()); result = in.validateIngredientAmount(tmpIngredientAmount); if(result == false){ System.out.println("Entered value for Ingredient Amount: "+ tmpIngredientAmount +" is a negative number. Sorry you are out of attempts!"); return; } } else{ System.out.println("Entered number of Cups: "+stringIngredientAmount+ " is not valid float value. Sorry you are out of attempts!"); return; } }
/*To fetch the unit of measurement for the ingredient amount*/ Scanner scnrUnitMeasurement = new Scanner(System.in);// Scanner to fetch the Unit Measurement /*Prompts for user to enter the Unit Measurement*/ System.out.println("Please enter the unit of measurement for the ingredient amount: ");// Assigns the Unit Measurement to the temporary variable. tmpUnitMeasurement = scnrUnitMeasurement.nextLine().trim(); /*Temporary variable of Unit Measurement is being passed to the Validation method*/ result = in.validateUnitMeasurement(tmpUnitMeasurement); /* while loop used to print the messages as per below * If the validation method returns false then the application will prompt for the user to enter the Unit Measurement again. * Application prompts twice if the input is not correct and then finally exits. */ while(result == false){ if(k>=1){ System.out.println("Entered Name of Unit Measurement: " + tmpUnitMeasurement + " is not a valid string. Sorry you are out of attempts!" ); return; } else{ System.out.println("Entered Name of Unit Measurement: " + tmpUnitMeasurement + " is not a valid string" ); System.out.println("Please enter the unit of measurement for the ingredient amount: "); tmpUnitMeasurement = scnrUnitMeasurement.nextLine().trim(); result = in.validateUnitMeasurement(tmpUnitMeasurement); } k++; }
/*To fetch the number of calories per cup the ingredient has*/ Scanner scnrCalories = new Scanner(System.in);// Scanner to fetch the Number of Calories Per Cup /*Prompts for user to enter the Number of Calories Per Cup*/ System.out.println("Please enter the number of calories per cup: "); String stringNumberOfCalories;//To get the Number of Calories per cup as a string which can be used to verify if there are whitespaces in the variable stringNumberOfCalories =scnrCalories.nextLine();// Assigns the Number of calories per cup to the temporary variable. /*To verify if the entered Number of calories per cup is Integer or not with the regex */ if(Pattern.matches("^d+$", stringNumberOfCalories)){ /*Parse the string, convert it to integer and store to the temporary variable*/ tmpNumberOfCaloriesPerCup = Integer.parseInt(stringNumberOfCalories.trim()); /*Temporary variable of Number of calories per cup is being passed to the Validation method*/ result = in.validateNumberOfCaloriesPerCup(tmpNumberOfCaloriesPerCup); /* while loop used to print the messages as per below * If the validation method returns false then the application will prompt for the user to enter the number of calories per cup again. * Application prompts twice if the input is not correct and then finally exits. */ while(result== false){ if(l>=1){ System.out.println("Entered value for Number of Calories per cup: "+ tmpNumberOfCaloriesPerCup +" is a negative number. Sorry you are out of attempts!"); return; } else{ System.out.println("Entered value for Number of Calories per cup: "+ tmpNumberOfCaloriesPerCup +" is a negative number" ); System.out.println("Please enter the number of calories per cup: "); stringNumberOfCalories =scnrCalories.nextLine(); if(Pattern.matches("^d+$", stringNumberOfCalories)){ tmpNumberOfCaloriesPerCup = Integer.parseInt(stringNumberOfCalories.trim()); result = in.validateNumberOfCaloriesPerCup(tmpNumberOfCaloriesPerCup); } else{ System.out.println("Entered number of calories per cup: "+ stringNumberOfCalories + " is not a valid integer value. Sorry you are out of attempts!"); return; } } l++; } } else{ System.out.println("Entered number of calories per cup: "+ stringNumberOfCalories + " is not a valid integer value"); System.out.println("Please enter the number of calories per cup: "); stringNumberOfCalories =scnrCalories.nextLine(); if(Pattern.matches("^d+$", stringNumberOfCalories)){ tmpNumberOfCaloriesPerCup = Integer.parseInt(stringNumberOfCalories.trim()); result = in.validateNumberOfCaloriesPerCup(tmpNumberOfCaloriesPerCup); if(result == false){ System.out.println("Entered number of calories per cup: "+ tmpNumberOfCaloriesPerCup + " is not a valid integer value. Sorry you are out of attempts!"); return; } } else{ System.out.println("Entered number of calories per cup: "+ stringNumberOfCalories + " is not a valid integer value. Sorry you are out of attempts!"); return; } } /* To print the details of ingredients along with the name. number of cups and the total number of calories for that ingredient*/ System.out.println(in.nameOfIngredient + " uses " + in.ingredientAmount + " cups and has " + in.totalCalories() + " calories."); }
Ingredient addIngredient(String nameOfIngredient) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
double getIngredientCost() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
class Recipe { /** * @param args the command line arguments * MIN_SERVINGS as Final INT * MAX_SERVINGS as Final INT * recipeName as String * servings as Integer * recipeIngredients as ArrayList * totalRecipeCalories as double * totalRecipeCost as double */ /*Declaring the parameters required in this class */ public static final int MIN_SERVINGS = 0; //Final variable of servings which restricts user to enter at least above this value public static final int MAX_SERVINGS = 100;//Final variable of servings which restricts user to enter value greater than this value private String recipeName; private int servings; private ArrayList recipeIngredients = new ArrayList<>();//stores the List of values coming from Ingredient Object private double totalRecipeCalories = 0.0; private double totalRecipeCost = 0.0; /*default constructor*/ public Recipe() { super(); } /* Paramaterized Constructor*/ /** * * @param recipeName * @param servings * @param recipeIngredients * @param totalRecipeCalories * @param totalRecipeCost */ public Recipe(String recipeName, int servings, ArrayList recipeIngredients, double totalRecipeCalories,double totalRecipeCost) { super(); this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients; this.totalRecipeCalories = totalRecipeCalories; this.totalRecipeCost = totalRecipeCost; }
Recipe(String recipeName, int servings, ArrayList recipeIngredients, double totalRecipeCalories) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } /** * To get Recipe Name * @return */ public String getRecipeName() { return recipeName; } /** * Validates the Recipe Name and sets the Recipe Name * @param recipeName * @throws Exception */ public void setRecipeName(String recipeName) throws Exception { if(recipeName.trim().length()==0){ String msg = "Recipe Name cannot be empty"; throw new Exception(msg); }else{ this.recipeName = recipeName; } } /** * To get the servings * @return */ public int getServings() { return servings; } /** * Validates the Servings and sets the Servings Value * @param servings * @throws Exception */ public void setServings(int servings) throws Exception { if(MIN_SERVINGS < servings && servings <= MAX_SERVINGS){ this.servings = servings; }else { String msg = "Servings is not between 0 and 50 " + servings; throw new Exception(msg); } } /** * To get the Recipe Ingredients from the Array List * @return */ public ArrayList getRecipeIngredients() { return recipeIngredients; } /** * To set the values into the Array List * @param recipeIngredients */
public void setRecipeIngredients(ArrayList recipeIngredients) { this.recipeIngredients = recipeIngredients; } /** * To get the total calories for the recipe * @return */ public double getTotalRecipeCalories() { return totalRecipeCalories; } /** * To set the Total Calories for the recipe * @param totalRecipeCalories */ public void setTotalRecipeCalories(double totalRecipeCalories) { this.totalRecipeCalories = totalRecipeCalories; } /** * To get the Total Cost for a Recipe * @return */
public double getTotalRecipeCost() { return totalRecipeCost; } /** * To Set the total cost for a recipe * @param totalRecipeCost */ public void setTotalRecipeCost(double totalRecipeCost) { this.totalRecipeCost = totalRecipeCost; } /** * Custom Method that prints the Recipe */ public void printRecipe() { /** * Print the following recipe information: * Recipe: <> * Serves: <> * Ingredients: * <> * <> * <> * <> * <> * <> * * <> * <> * <> * <> * ... * <> * * <> * <> * <> * <> * * Each serving has <> Calories. * * HINT --> Use a for loop to iterate through the ingredients */ System.out.println("Recipe: " + recipeName); System.out.println("Serves: " + servings); System.out.println("Ingredients: " ); /* For loop to display the ingredients by looping from 0 to the size of array.*/ for (int i = 0; i < recipeIngredients.size(); i++) { Ingredient ingredient = recipeIngredients.get(i); // to get the ingredient from the array System.out.println("t" + "Ingredient " + (i+1)+ " " +"tt"+ "Ingredient Name: " + ingredient.getNameOfIngredient() + " " +"tt" + "Ingredient Amount: "+ ingredient.getIngredientAmount() + " " +"tt" + "Unit Measurement: " + ingredient.getUnitMeasurement() + " " +"tt" + "Total Calories: " + ingredient.totalCalories()); // to print the ingredient name } System.out.println("Each serving has "+ calculateTotalCalories() + " Calories."); } /** * Method that is used to make a new recipe * @return * @throws Exception */ public Recipe createNewRecipe() throws Exception{ Recipe recipe = new Recipe(); Scanner scnr = new Scanner(System.in); inputRecipeName(recipe,scnr); inputServings(recipe,scnr); inputRecipeIngredients(recipe,scnr); Recipe r = new Recipe(recipe.recipeName, recipe.servings, recipe.recipeIngredients, recipe.getTotalRecipeCalories(),recipe.totalRecipeCost); return r; } /** * Method used to get the user input for the recipe name * @param r * @param scnr * @throws Exception */ private static void inputRecipeName(Recipe r, Scanner scnr) throws Exception{ try{ System.out.println("Please enter the recipe name: "); String tmpRecipeName = scnr.nextLine(); r.setRecipeName(tmpRecipeName); }catch(Exception ex){ try{ System.out.println("Please enter the recipe name: "); String tmpRecipeName = scnr.nextLine(); r.setRecipeName(tmpRecipeName); }catch(Exception ex1){ throw(ex1); } } } /** * Method used to get the input from the user for the Servings * @param r * @param scnr * @throws Exception */ private static void inputServings(Recipe r, Scanner scnr) throws Exception{ try{ System.out.println("Please enter the number of servings: "); String stringServings = scnr.nextLine(); int tmpServings = Integer.parseInt(stringServings.trim()); r.setServings(tmpServings); }catch(Exception ex){ try{ System.out.println("Please enter the number of servings: "); String stringServings = scnr.nextLine(); int tmpServings = Integer.parseInt(stringServings.trim()); r.setServings(tmpServings); }catch(Exception ex1){ throw(ex1); } } } /** * Input used to get the ingredients and store it into the array list * @param r * @param scnr * @throws Exception */ private static void inputRecipeIngredients(Recipe r, Scanner scnr) throws Exception{ try{ String nameOfIngredient = ""; boolean addMoreIngredients = true; while(addMoreIngredients){ try{ Ingredient ingredient = new Ingredient().addIngredient(nameOfIngredient); r.recipeIngredients.add(ingredient); r.totalRecipeCalories = r.totalRecipeCalories + ingredient.totalCalories(); r.totalRecipeCost = r.totalRecipeCost + ingredient.getIngredientAmount(); }catch (Exception ex){ System.out.println("ex"); } System.out.println("Would you like to Enter Another Ingredient (Yes/No)?"); String ingr = scnr.next(); if(ingr.equalsIgnoreCase("NO")){ addMoreIngredients = false; } else if(ingr.equalsIgnoreCase("YES")){ addMoreIngredients = true; } else{ System.out.println("Please enter only Yes or No"); addMoreIngredients = true; } } }catch(Exception ex){ try{ String nameOfIngredient = ""; boolean addMoreIngredients = true; while(addMoreIngredients){ try{ Ingredient ingredient = new Ingredient().addIngredient(nameOfIngredient); r.recipeIngredients.add(ingredient); r.totalRecipeCalories = r.totalRecipeCalories + ingredient.totalCalories(); r.totalRecipeCost = r.totalRecipeCost + ingredient.getIngredientCost(); }catch (Exception ex1){ System.out.println(ex1); } System.out.println("Would you like to Enter Another Ingredient (Yes/No)?"); String ingr = scnr.next(); if(ingr.equalsIgnoreCase("NO")){ addMoreIngredients = false; } else if(ingr.equalsIgnoreCase("YES")){ addMoreIngredients = true; } else{ System.out.println("Please enter only Yes or No"); addMoreIngredients = true; } } }catch(Exception ex1){ throw(ex1); } } } /** * Method used to calculate the Total calories for the recipe * @return */ private int calculateTotalCalories(){ return (int)(Math.round(totalRecipeCalories/servings)); // Round function to get the closest number of calories }
public void calcualateRecipeCost(){ System.out.println( "The Total Cost for the Recipe is $"+ totalRecipeCost); } } class Recipe_Test { public static void main(String[] args){ Scanner in = new Scanner(System.in); while(true){ try{ Recipe recipe = new Recipe(); recipe = recipe.createNewRecipe(); recipe.printRecipe(); recipe.calcualateRecipeCost(); }catch (Exception e){ System.out.println(e); } System.out.println("Enter Another recipe (y)? "); String buf = in.next(); char c = Character.toLowerCase(buf.charAt(0)); if(c=='n') break; } System.out.println("End of Test"); } class SteppingStone5_Recipe { private String recipeName; private int cookingTime; public SteppingStone5_Recipe(String recipeName, int cookingTime) { super(); this.recipeName = recipeName; this.cookingTime = cookingTime; } public int getCookingTime() { return cookingTime; } public void setRacipeName(String recipeName) { this.recipeName = recipeName; } public void printRecipe() { System.out.println(recipeName+" cooked in "+ cookingTime+" hour"); } public String getRecipeName() { return recipeName; } } public class ReceipeBox { private ArrayList listOfRecipes;
public ArrayList getListOfRecipes() { return listOfRecipes; }
public void setListOfRecipes(ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; }
public ReceipeBox(ArrayList listOfRecipes) { this.listOfRecipes = listOfRecipes; }
public ReceipeBox() { this.listOfRecipes = new ArrayList<>(); }
void printAllRecipeDetails(String selectedRecipe) { for (Recipe recipe : listOfRecipes) { if (recipe.getRecipeName().equalsIgnoreCase(selectedRecipe)) { recipe.printRecipe(); return; } } System.out.println("No Recipe found with name: " + selectedRecipe); }
void printAllRecipeNames() { listOfRecipes.forEach((selectedRecipe) -> { System.out.println(selectedRecipe.getRecipeName()); }); }
public void addRecipe(Recipe tempRecipe) { listOfRecipes.add(tempRecipe); }
public static void main(String[] args) { ReceipeBox myRecipeBox = new ReceipeBox(); Scanner menuScnr = new Scanner(System.in); System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:"); while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) { int input = menuScnr.nextInt(); switch (input) { case 1: myRecipeBox.recipe1(); break; case 2: System.out.println("Which recipe? "); String selectedRecipeName = menuScnr.next(); myRecipeBox.printAllRecipeDetails(selectedRecipeName); break; case 3: for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) { System.out.println((j + 1) + ": " + myRecipeBox.listOfRecipes.get(j).getRecipeName()); } break; default: System.out.println(" Menu " + "1. Add Recipe " + "2. Print Recipe " + "3. Adjust Recipe Servings " + " Please select a menu item:"); break; } System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Details " + "3. Print All Recipe Names " + " Please select a menu item:"); } }
public void recipe1() { double totalRecipeCalories = 0.0; ArrayList recipeIngredients = new ArrayList(); boolean addMoreIngredients = true; Scanner scnr = new Scanner(System.in); System.out.println("Please enter the recipe name: "); // prompt user to // enter recipe // name. String recipeName = scnr.nextLine(); // checks if valid input was // entered. System.out.println("Please enter the number of servings: "); // prompt // user // to // enter // number // of // servings // for // recipe. int servings = scnr.nextInt(); // checks if valid input was entered. do { System.out .println("Please enter the ingredient name or type end if you are finished entering ingredients: "); String ingredientName = scnr.next().toLowerCase(); if (ingredientName.toLowerCase().equals("end")) { addMoreIngredients = false; } else { recipeIngredients.add(ingredientName); // creating array list // for recipe // ingredients. System.out.println("Please enter an ingredient amount: "); float ingredientAmount = scnr.nextFloat(); System.out.println("Please enter the measurement unit for an ingredient: "); String unitMeasurement = scnr.next(); System.out.println("Please enter ingredient calories: "); int ingredientCalories = scnr.nextInt(); totalRecipeCalories = (ingredientCalories * ingredientAmount); System.out.println(ingredientName + " uses " + ingredientAmount + " " + unitMeasurement + " and has " + totalRecipeCalories + " calories."); } // end else loop. } while (addMoreIngredients); Recipe recipe = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories); addRecipe(recipe); } }
766 767 768 Illegal static declaration in inner class Recipe_Test.ReceipeBox modifier 'static' is only allowed in constant variable declarations); cipe) { 769 770 (Alt-Enter shows hints) public static void main(String[] args) { 773 774 775 776 777 778 ReceipeBox myRecipeBox - new ReceipeBox (); Scanner menuScnr = new Scanner(System.in); System.out.println("Menu " + "1. Add Recipe " + "2. Print All Recipe Detail +"3. Print All Recipe Names " + " Please select a menu it while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) { int input menuScnr.nextInt(); switch (input) {
Step by Step Solution
There are 3 Steps involved in it
Step: 1
1 Compile and Run If you havent already try compiling the code using a Java compiler eg javac This w...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