Introduction
Welcome to the Digital Recipe Book program! This application allows you to create a simple yet functional recipe book using Java programming. The purpose of this program is to store recipes, display them, and provide an easy way to manage different recipes. You can add new recipes, view existing ones, and organize your favorite dishes all in one place!
Objective
The main objective of this project is to build a basic digital recipe book that can store and manage recipes efficiently. The program will allow you to add, view, and list recipes. It provides a hands-on experience in Java programming and helps understand the use of classes, arrays, and simple user input handling.
Recipe Book Code (Java)
import java.util.ArrayList; import java.util.Scanner; class Recipe { String name; String ingredients; String instructions; public Recipe(String name, String ingredients, String instructions) { this.name = name; this.ingredients = ingredients; this.instructions = instructions; } public void displayRecipe() { System.out.println("Recipe Name: " + name); System.out.println("Ingredients: " + ingredients); System.out.println("Instructions: " + instructions); System.out.println("--------------------------------------------------"); } } public class RecipeBook { private ArrayList recipes; public RecipeBook() { recipes = new ArrayList<>(); } public void addRecipe(String name, String ingredients, String instructions) { Recipe newRecipe = new Recipe(name, ingredients, instructions); recipes.add(newRecipe); } public void viewRecipes() { if (recipes.isEmpty()) { System.out.println("No recipes available."); } else { for (Recipe recipe : recipes) { recipe.displayRecipe(); } } } public static void main(String[] args) { RecipeBook myRecipeBook = new RecipeBook(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Welcome to the Recipe Book!"); System.out.println("1. Add a new recipe"); System.out.println("2. View all recipes"); System.out.println("3. Exit"); System.out.print("Choose an option: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume newline if (choice == 1) { System.out.print("Enter recipe name: "); String name = scanner.nextLine(); System.out.print("Enter ingredients: "); String ingredients = scanner.nextLine(); System.out.print("Enter instructions: "); String instructions = scanner.nextLine(); myRecipeBook.addRecipe(name, ingredients, instructions); } else if (choice == 2) { myRecipeBook.viewRecipes(); } else if (choice == 3) { System.out.println("Goodbye!"); break; } else { System.out.println("Invalid option, please try again."); } } scanner.close(); } }
Program Structure and Explanation
The program consists of two main parts: the Recipe
class and the RecipeBook
class. Here’s a breakdown:
- Recipe Class: This class represents a single recipe. It contains attributes such as the recipe name, ingredients, and instructions. The method
displayRecipe()
is used to print out the details of the recipe. - RecipeBook Class: This class manages a collection of recipes. It allows the user to add new recipes and view existing ones. Recipes are stored in an
ArrayList
. - Main Method: The main method provides a simple menu for the user to interact with. It allows the user to add new recipes, view all recipes, or exit the program.
How to Run the Program
- Ensure you have Java installed on your system. If not, download and install it from the official Java Downloads page.
- Save the code in a file named
RecipeBook.java
. - Open a terminal or command prompt, navigate to the folder where the file is saved, and compile the program using the command:
javac RecipeBook.java
- After successful compilation, run the program using the command:
java RecipeBook
- Follow the on-screen prompts to add and view recipes.