Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Recipe Book: Arrays and Objects/Types

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Recipe Book: Arrays and Objects/Types

    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:

    public class Recipe {
     
    	public Recipe(String name, String tag, String author, String recipe) {
     
     
     
    	}
     
    }

    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.


  2. #2
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recipe Book: Arrays and Objects/Types

    You have a good start here. A couple things to consider: Since you don't know how many recipes they may enter, and Array may not be a good choice. An ArrayList may suit this better. Secondly, you have created the new object just fine with:
    recipes[currentRecipeNumber] = new Recipe(name, tag, author, recipe);

    Though in your Recipe class, you have no fields to store the data. Do a little research on private/public data members, and getter/setter methods.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Recipe Book: Arrays and Objects/Types

    Thanks a lot for the help, man! I'll do some research, and get back to you.

Similar Threads

  1. arrays of card objects
    By Dekpo in forum Member Introductions
    Replies: 1
    Last Post: August 28th, 2012, 08:48 PM
  2. Replies: 0
    Last Post: May 10th, 2012, 07:59 PM
  3. Problem with arrays and custom objects
    By MonkeyBeast in forum Collections and Generics
    Replies: 1
    Last Post: January 31st, 2012, 01:50 PM
  4. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM
  5. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM