Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Need Help with Simple Recipe Program

$
0
0
Hello. Looking for some help on a project where I need to make a recipe book with the follow features:
1. add a recipe
2. list all recipes
3. display a single recipe
4. Search and return recipes with a single ingredient.

I tried below using a hashmap, for the recipe name and ingredients:

import java.util.HashMap;
import java.util.Scanner;
public class Recipe 
{
		String name;
		String ingred;
		
		public Recipe(String recipeName, String ingredients) {
		  	name = recipeName;
		  	ingred = ingredients;
		}
		 
	    Recipe(){
	    }
	    
	    public static HashMap<String,String> addRecipe(){
	    	
	    	Scanner kbd =  new Scanner(System.in);
			System.out.println("How many recipes do you want to add?");
			int number= kbd.nextInt();
			HashMap<String,String> recipes = new HashMap<String,String>(number);
			
			  for( int i = 1; i<=number; i++){
			    
			    System.out.println("Enter a name for recipe number "+i);
			  	String recipeName = kbd.next();
			  	System.out.println("Enter the ingreditents for recipe number "+i);
			  	String ingredients = kbd.next();
			  	
			  	recipes.put(recipeName,ingredients);
			  	}
			Recipe.main(null);
			return recipes;
	      }
	      
		 public static void print(HashMap<String,String> recipes){
			System.out.print(recipes);
			Recipe.main(null);
		 }
		 
		 public static void displayRecipe(HashMap<String,String> recipes)
		 {
			 Scanner kbd =  new Scanner(System.in);
			 System.out.println ("Enter the recipe name:");
			 String displayName = kbd.next();
			 System.out.println (recipes.get(displayName));
			 Recipe.main(null);
		 }
		  
        public static void main(String [] args) {
		      Scanner kbd = new Scanner(System.in);
		      System.out.println ("Welcome to the Recipe Manager.");
			  System.out.println ("Please type the number of the operation you would "
				            + "like to perform:");
			  System.out.println ("1. Add a New Recipe");
		      System.out.println ("2. List All Stored Recipes");
			  System.out.println ("3. Display Recipe");
			  System.out.println ("4. Search for Recipes by Ingredient");
			  System.out.println ("5. Close the Recipe Application");
			  int answer = kbd.nextInt();
		      
		      if (answer == 1) { // add contacts
		        Recipe.addRecipe();
		      } 
		      
		      else if (answer == 2) { 
		    	Recipe.print();
		      }
		      
		      else if (answer == 3) {
		    	 Recipe.displayRecipe();
		    	 
		      } 
		      else if (answer == 4) {
		    	  //read ingredient from keyboard
		    	  //check keys for ingredient
		      } 
		      
		      else if (answer == 5) {System.exit(0);
		      } 
		      
		      else {
		        System.out.println("Error. Please Enter Number between 1 and 5.");
		      }
}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>