Hey guys,
Getting back into Java. Never really worked much with Objects, new types, and the sort. What I'm trying to do is make a recipe book program that will help me sort my recipes. What I have so far is just up to entering a recipe. My problem is that I'm not quite sure how to create a new type. What I've done here is create a class Recipe and have the constructor have some strings you need. Then when I create the array for the recipes, I find the pieces of info I need using a scanner, then set the current piece of array with those pieces of info.
Here is the main class:
import java.util.Scanner; public class RecipeBookMain { static boolean hasRunBefore = false; static Recipe[] recipes = new Recipe[99]; static int currentRecipeNumber = 0; /* This Number is always 1 less than the actual number of recipes */ static Scanner scan = new Scanner(System.in); public static void main(String args[]) { String input; if(hasRunBefore == false) { System.out.print("Welcome to RBook! This program is a recipe book thats helps you put your recipes into an electronic format, and sort them so you can find them later. If you would like to view a tutorial, please enter 'Tutorial'. Otherwise, please enter a command."); RunCommand(); } } public static void Tutorial() { System.out.println("\nRBook uses a command system. If you would like to enter a recipe, enter 'recipe' on the main screen then follow the prompts to enter your recipe. When you save a recipe, there is a Notepad file with your recipe in it if you would like to move it without moving the program. If you would like to search for recipes, enter 'search'. It will then ask if you would like to search for names or tags. Tags are used when you want to have a certain category for your recipe, like vegetarian, etc. If you want to reset your recipe book, enter 'reset'."); RunCommand(); } public static void RunCommand() { String input; System.out.print("\nWhat would you like to do?"); input = scan.nextLine(); switch(input.toLowerCase()) { case "tutorial": Tutorial(); RunCommand(); case "recipe": EnterRecipe(); } } public static void EnterRecipe() { String name; String tag; String author; String recipe; System.out.print("Name?"); name = scan.nextLine(); System.out.print("Tag?"); tag = scan.nextLine(); System.out.print("Author?"); author = scan.nextLine(); System.out.print("Recipe?"); recipe = scan.nextLine(); recipes[currentRecipeNumber] = new Recipe(name, tag, author, recipe); System.out.print(recipes[currentRecipeNumber].name); /* This is a debug line while I figure out what's wrong */ } }
And here is my Recipe class:
I am probably missing something, or doing something wrong. This is my first time working with stuff like this. Can someone help me?
Thanks,
-Silent
P.S: In the main class, the .name in the last line (System.out.print(recipes[currentRecipeNumber].name) has an error. It says it can't find it. So I just need to know how to create a type so that it can be created and referenced.