Java

 

 

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

  1. Ensure you have Java installed on your system. If not, download and install it from the official Java Downloads page.
  2. Save the code in a file named RecipeBook.java.
  3. 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
  4. After successful compilation, run the program using the command:
    java RecipeBook
  5. Follow the on-screen prompts to add and view recipes.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)