top of page
Search
deonnahardaway

Object Oriented Application Development

Updated: May 9, 2021

In this course I used the Java computer programming language to create an application that manages a collection of items. The goal was to create a Recipe manager that would allow a user to input, store and organize information, and would also calculate ingredient amounts and servings. This project not only taught me how to code in Java, but also the overall process of Application Development. I used the Netbeans for creating this program. Netbeans is an Integrated Development Envir


Planning the Application: Pseudocode


Before beginning the coding process for the Recipe Manager Program it is necessary to outline the program being created. The first step in outlining the steps in this process is writing the Pseudocode. Pseudocode is the initial walk-through of the program and a valuable step in object-oriented application development. The Recipe Manager Program will define three major classes:

· The Recipe Box Class will manage the recipe collection through the Main Menu.

· The Recipe List Class will allow the User to view add, edit, or delete a recipe.

· The Ingredients Class will allow the user to view, add, edit or delete ingredients.


Pseudocode for Recipe Manager:


Recipe Box/Main Menu

Begin program

Menu displays options “Recipes” and “Ingredients”


Recipe List

User selects Recipes

List of Recipes is displayed

Options to ADD Recipe or VIEW existing Recipe are displayed

IF User clicks on selected Recipe to VIEW

THEN Recipe Information is displayed

User has two options: DELETE Recipe, or EDIT Recipe

IF User selects DELETE Recipe

THEN Confirm User wants to delete recipe

IF DELETE is selected THEN program deletes Recipe

IF Option to EDIT Recipe is selected

THEN list options to alter Recipe information

User edits Recipe information

New Recipe information is SET


IF Option to ADD New Recipe to Recipe List is selected

THEN Prompt User to GET Recipe Name

SET Recipe Name

Prompt User to GET Recipe Ingredients

Prompt User to GET Number of Cups (can be decimal)

SET Ingredients

Recipe Calories are calculated

Calories SET for the Recipe

Recipe is saved to the Recipe List


Ingredients List

User selects “Ingredients” from Main Menu

List of Ingredients is displayed

Options to ADD Ingredient or VIEW existing Ingredient by clicking the Ingredient are displayed

IF User clicks on selected Ingredient to VIEW

THEN Ingredient Information is displayed

User has two options: DELETE Ingredient, or EDIT Ingredient

IF User selects DELETE Ingredient

THEN Confirm User wants to delete Ingredient

IF DELETE is selected THEN program deletes Ingredient

IF Option to EDIT Ingredient is selected

THEN list options to alter Ingredient information

User edits Ingredient information

New Ingredient information is SET


IF Option to ADD New Ingredient to Ingredient List is selected

THEN Prompt User to GET Ingredient Name

SET Ingredient Name

Prompt User to GET Number of Calories per Cup

SET Calories per Cup

Ingredient is saved to the






Ingredients Code


The Ingredients Class prompts the User for input. Once the necessary information is entered, the program checks that input is valid. If the input is valid, the program will add the input to the ingredients Array List. If it is not valid, it will prompt the User for again for the correct information. This class uses private variables, so it is necessary to write accessors and mutators to add items to the Array List.


/*

* 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 recipebox_finished;


import java.util.ArrayList;

import java.util.Scanner;


/**

*

* @author hardaway

*/

public class Ingredient {


/**

* @param args the command line arguments

*/

//creating an Ingredients class for the Ingredients Array List to use with

//Milestone2_RecipeTest class

//creating private variables so they cannot be directly accessed and modified

private String nameOfIngredient;

private Double ingredientAmount;

private int ingredientCalories;

private double totalCalories;

private double ingredientCost;

private double unitMeasurement;

private ArrayList<Double> recipeCost = new ArrayList<Double>();

//creating public accessors and mutators to be used by the Test Class


/**

*

* @param nameOfIngredient

*/

public void setNameOfIngredient(String nameOfIngredient){

this.nameOfIngredient = nameOfIngredient;

}


/**

*

* @param ingredientAmount

*/

public void setIngredientAmount(double ingredientAmount){

this.ingredientAmount = ingredientAmount;

}


/**

*

* @param ingredientCalories

*/

public void setIngredientCalories(int ingredientCalories) {

this.ingredientCalories = ingredientCalories;

}


/**

*

* @param totalCalories

*/

public void setTotalCalories(double totalCalories){

this.totalCalories = totalCalories;

}


/**

*

* @param ingredientCost

*/

public void setIngredientCost(double ingredientCost){

this.ingredientCost = ingredientCost;

}


/**

*

* @param unitMeasurement

*/

public void setUnitMeasurement(double unitMeasurement){

this.unitMeasurement = unitMeasurement;

}

/**

*

* @param recipeCost

*/

public void setRecipeCost(ArrayList<Double> recipeCost) {

this.recipeCost = recipeCost;

}


/**

*

* @return

*/

public String getNameOfIngredient(){

return nameOfIngredient;

}


/**

*

* @return

*/

public double getIngredientAmount(){

return ingredientAmount;

}


/**

*

* @return

*/

public int getIngredientCalories(){

return ingredientCalories;

}


/**

*

* @return

*/

public double getTotalCalories(){

return totalCalories;

}


/**

*

* @return

*/

public double getIngredientCost(){

return ingredientCost;

}


/**

*

* @return

*/

public double getUnitMeasurement(){

return unitMeasurement;

}


/**

*

* @return

*/

public ArrayList<Double> getRecipeCost(){

return recipeCost;

}


/**

*

*/

public Ingredient(){

this.ingredientAmount = 0.0;

this.ingredientCalories = 0;

this.nameOfIngredient = "";

this.totalCalories = 0.0;

this.ingredientCost = 0.0;

this.unitMeasurement = 0.0;

}


/**

*

* @param ingredientAmount

* @param ingredientCalories

* @param nameOfIngredient

* @param totalCalories

* @param ingredientCost

* @param unitMeasurement

*/

public void Ingredient(double ingredientAmount, int ingredientCalories, String nameOfIngredient,

double totalCalories, double ingredientCost, double unitMeasurement){

this.ingredientAmount = ingredientAmount;

this.ingredientCalories = ingredientCalories;

this.nameOfIngredient = nameOfIngredient;

this.totalCalories = totalCalories;

this.ingredientCost = ingredientCost;

this.unitMeasurement = unitMeasurement;

}


/**

*

* @return

*/

public double RecipeCost() {

double sum = 0;

for(int i = 0; i < recipeCost.size(); i++)

sum += recipeCost.get(i);

return sum;

}

/**

*

* @param tempNameOfIngredients

* @return

*/

public Ingredient addIngredient(String tempNameOfIngredients) {

Scanner scnr = new Scanner(System.in);

float tempingredientAmount = 0;

int tempingredientCalories = 0;

double temptotalCalories = 0.0;

final var MAX_UNITS = 1000;

final var MAX_CALS = 1000;

String tempnameOfIngredient = "";

double tempingredientCost = 0.0;

String tempunitMeasurement = "";

ArrayList<Double> temprecipeCost = new ArrayList<>();

System.out.println("Enter the name of the ingredient: ");

tempnameOfIngredient = scnr.nextLine();

System.out.println("Enter the unit of measurement for the ingredient(Tbsp, tsp, gram, cup(s), ounces etc.): ");

tempunitMeasurement = scnr.nextLine();

System.out.println("Enter the number of units(between 1-1000, can be a decimal): ");

//The user is being asked to enter a number betwen 1 and 1000.

//The number is scanned to determine if it is an double before moving on.


if (scnr.hasNextFloat()){

tempingredientAmount = scnr.nextFloat();

/*Next the integer entered by the User will be checked to make sure

*it falls between 1 and 1000. The logical operator "&&" is used so

*the integer must satisfy both expressions. If it does not, the second

*branch will execute and the User will be asked for a different number.

*/

if ((tempingredientAmount >= 1) && (tempingredientAmount <= MAX_UNITS)){

System.out.println(tempingredientAmount + " is a valid number of units!");

}

else {

System.out.println(tempingredientAmount + " is not a valid number of units!");

System.out.println("Please enter another number of units between 1 and 1000: ");

tempingredientAmount = scnr.nextInt();

}

/*If the User enters a valid integer the first branch will execute

*below. If the User again fails to enter a valid integer, the second

*branch will execute, and the User will be out of attempts.

*/

if ((tempingredientAmount >= 1) && (tempingredientAmount <= MAX_UNITS)){

System.out.println(tempingredientAmount + " is a valid number of units!");

}

else if (tempingredientAmount < 1){

System.out.println(tempingredientAmount + " is less than 1. You are out of attempts.");

}

else {

System.out.println(tempingredientAmount + " is greater than 1000. You are out of attempts.");

}

//if the User enters a string (example: "hello") instead of an integer, they will

//receive the error message.

} else {

System.out.println("Attempt failed.");

}

System.out.println("Please enter the number of calories per Unit (between 1-1000): ");

if (scnr.hasNextInt()){

tempingredientCalories = scnr.nextInt();

if ((tempingredientCalories >= 1) && (tempingredientCalories <= MAX_CALS)){

System.out.println(tempingredientCalories + "is a valid number of calories per unit!");

}

else {

System.out.println(tempingredientCalories + " is not a valid number of units!");

System.out.println("Please enter another number of units between 1 and 1000: ");

tempingredientCalories = scnr.nextInt();

}

/*If the User enters a valid integer the first branch will execute

*below. If the User again fails to enter a valid integer, the second

*branch will execute, and the User will be out of attempts.

*/

if ((tempingredientCalories >= 1) && (tempingredientCalories <= MAX_CALS)){

System.out.println(tempingredientCalories + " is a valid number of units!");

}

else if (tempingredientCalories < 1){

System.out.println(tempingredientCalories + " is less than 1. You are out of attempts.");

}

else {

System.out.println(tempingredientCalories + " is greater than 1000. You are out of attempts.");

}

}

else {

System.out.println("That is not a valid number of calories.");

}


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

tempingredientCost = scnr.nextDouble();

temprecipeCost.add(tempingredientCost);

temptotalCalories = tempingredientAmount * tempingredientCalories;


System.out.println("This Recipe uses: ");

System.out.println("Ingredient: " + tempnameOfIngredient);

System.out.println("Ingredient Amount: "+ tempingredientAmount + tempunitMeasurement);

System.out.println("Total Calories: " + temptotalCalories);

System.out.println("Cost: " + temprecipeCost);

return null;

}

}




Recipe Code

The Recipe Class uses private variables also to protect the code from being changed by the User. User input is added to Array List the by using accessors and mutators. Cutoms methods have been written to calculate total recipe calories as well as calories for individual servings.


/*

* 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 recipebox_finished;


import java.util.Scanner;

import java.util.ArrayList;


/**

*

* @author hardaway

*/

public class Recipe {

//creating a class with variables, methods and constructor

//private variables cannot be accessed directly to protect them from being modified.


private String recipeName;

private int servings;

private ArrayList<Ingredient> recipeIngredients= new ArrayList();

private double totalRecipeCalories;


//Create mutators to set the values of public fields


/**

*

* @param recipeName

*/

public void setRecipeName(String recipeName) {

this.recipeName = recipeName;

}


/**

*

* @param servings

*/

public void setServings(int servings) {

this.servings = servings;

}


/**

*

* @param recipeIngredients

*/

public void setRecipeIngredients(ArrayList<Ingredient> recipeIngredients) {

this.recipeIngredients = recipeIngredients;

}


/**

*

* @param totalRecipeCalories

*/

public void setTotalRecipeCalores(double totalRecipeCalories) {

this.totalRecipeCalories = totalRecipeCalories;

}



/*Add accessors and mutators for Ingredient Cost and total Recipe Cost

*/

//Create accessors to get the value of public fields


/**

*

* @return

*/

public String getRecipeName() {

return recipeName;

}


/**

*

* @return

*/

public int getServings() {

return servings;

}


/**

*

* @return

*/

public ArrayList<Ingredient> getRecipeIngredients() {

return recipeIngredients;

}


/**

*

* @return

*/

public double getTotalRecipeCalories() {

return totalRecipeCalories;

}


//Use contructors to initialize all fields


/**

*

*/

public Recipe() {

this.recipeName = "";

this.servings = 0;

this.recipeIngredients = new ArrayList<>();

this.totalRecipeCalories = 0;

//Add ingredientCost and totalRecipeCost to constructors

}


/**

*

* @param recipeName

* @param servings

* @param recipeList

* @param totalRecipeCalories

*/

public Recipe(String recipeName, int servings, ArrayList<Recipe> recipeList, double totalRecipeCalories) {

this.recipeName = recipeName;

this.servings = servings;

this.recipeIngredients = recipeIngredients;

this.totalRecipeCalories = totalRecipeCalories;

}

//add method for calculating sum of array elements ingredientCost

//set sum equal to totalRecipeCost


/**

*

*/

public void printRecipe() {

//calculate the calories per single serving using the total calories and

//number of servings

double singleServingCalories;

singleServingCalories = totalRecipeCalories / servings; //can int = double?? might change later


//method to add new recipe to the list of recipes using the createNewRecipe

//print Recipe information

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

System.out.println("Number of Servings: " + servings);

System.out.println("Number Calories per serving: " + singleServingCalories);

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

for (int i = 0; i< recipeIngredients.size ();i++) {

recipeIngredients.get(i);

System.out.println(i);

}

}

//now that class Recipe class has been created we can being main but


Recipe Box


The purpose of the Recipe box code is to allow a User to view a list of Recipes held in the listofRecipes Array List, select a Recipe, and print the details for that Recipe. The User may also add a new Recipe to the Array List using the CreateNewRecipe Method.


/*

* 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 recipebox_finished;


/**

*

* @author hardaway

*/

import java.util.Scanner;

import java.util.ArrayList;


/**

*

* @author hardaway

*/

public class RecipeBox {


private ArrayList<Recipe> listOfRecipes;

//created an array list of recipes for the recipe box from stepping stone 5

//accessor and mutator for the recipe array list


/**

*

* @return

*/

public ArrayList<Recipe> getListOfRecipes() {

return listOfRecipes;

}


/**

*

* @param listOfRecipes

*/

public void setListOfRecipes(ArrayList<Recipe> listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

//constructors for the recipe box


/**

*

*/

public RecipeBox() {

this.listOfRecipes = new ArrayList();

}


/**

*

* @param listOfRecipes

*/

public void RecipeBox(ArrayList<Recipe> listOfRecipes) {

this.listOfRecipes = listOfRecipes;

}

//method to print recipe details


/**

*

* @param selectedRecipeName

*/

public void printAllRecipeDetails(String selectedRecipeName) {

for (int i = 0; i < listOfRecipes.size(); i++) {

if (listOfRecipes.get(i).getRecipeName().equals(selectedRecipeName)) {

listOfRecipes.get(i).printRecipe();

}

}

}

//method to print all recipe names


/**

*

*/

public void printAllRecipeNames() {

for (Recipe currentRecipe : listOfRecipes) {

System.out.println(currentRecipe.getRecipeName());

}

}


//method to add new recipe to the list of recipes using the createNewRecipe


/**

*

*/

public void addnewRecipe(){

Recipe tmpRecipe = new Recipe().createNewRecipe();

listOfRecipes.add(tmpRecipe);

}

/**

*

* @param args

*/

public static void main(String[] args) {

RecipeBox myRecipeBox = new RecipeBox();

Scanner menuScnr = new Scanner(System.in);

System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All Recipe Details\n" + "3. Print All Recipe Names\n" + "\nPlease select a menu item:");

while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {

int input = menuScnr.nextInt();

if (input == 1) {

myRecipeBox.addnewRecipe();

} else if (input == 2) {

System.out.println("Which recipe?\n");

String selectedRecipeName = menuScnr.next();

myRecipeBox.printAllRecipeDetails(selectedRecipeName);

} else if (input == 3) {

myRecipeBox.printAllRecipeNames();

}

else {

System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All Recipe Details\n" + "3. Print All Recipe Names\n" + "\nPlease select a menu item:");


}

}

}

}


Output

This is a screenshot from the Netbeans application. It shows an example of the output from the Recipe Manager program.




4 views0 comments

Recent Posts

See All

Comments


bottom of page