Results 1 to 7 of 7

Thread: Simple question

  1. #1
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default Simple question

    I am making a database of recipes, I have my constructor file in here, I am trying to talk to the recipe array list outside of here in my main program, but I can't figure out how to get it to work. >.<.

    I just want to be able to see, and use the arraylist in ways such as this in my main program to be able to display the text out, but not ever need to edit it.

    Java Code:
    if(recipes.contains(TEST1)) {
                recipes.indexOf(TEST1).getIngrediant1();
            }

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package myrecipebook;
    
    
    import java.util.ArrayList;
    /**
     *
     * @author sue
     */
    public class Recipe {
    
        private final Recipe TEST1 = new Recipe("Cookies", 8, "Chocolate", 4, "Flour", 2, "Sugar", 1, "Butter", 1);
        private final Recipe TEST2 = new Recipe("Vegetable Soup", 6, "Onion", 2, "Potato", 2, "Cabbage", 1, "Milk", 1);
        private final Recipe TEST3 = new Recipe("Cake", 9, "Cream", 4, "Frosting", 2, "Apples", 1, "Strawberries", 2, "Milk", 2);    
        
        private String name;
        private int numberOfIngreds;
        private String ingred1, ingred2, ingred3, ingred4, ingred5, ingred6, ingred7, ingred8;
        private int amt1, amt2, amt3, amt4, amt5, amt6, amt7, amt8;
        
        public ArrayList<Recipe> recipes = new ArrayList<Recipe>();
        
        private void setRecipes() {
            recipes.add(TEST1);
            recipes.add(TEST2);
            recipes.add(TEST3);
        }
        
        public void Recipe() {}
        Recipe(String n, int num, String i1, int a1) {
            name = n;numberOfIngreds = num;
            ingred1 = i1;
            amt1 = a1;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2) {
            name = n;
            numberOfIngreds = num;
            ingred1 = i1; ingred2 = i2;
            amt1 = a1; amt2 = a2;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2, String i3, int a3) {
            name = n;
            numberOfIngreds = num;
            ingred1 = i1; ingred2 = i2;  ingred3 = i3;
            amt1 = a1; amt2 = a2; amt3 = a3;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2, String i3, int a3, String i4, int a4) {
            name = n;
            ingred1 = i1; ingred2 = i2;  ingred3 = i3; ingred4 = i4;
            amt1 = a1; amt2 = a2; amt3 = a3; amt4 = a4;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2, String i3, int a3, String i4, int a4,
                String i5, int a5) {
            name = n;
            numberOfIngreds = num;
            ingred1 = i1; ingred2 = i2;  ingred3 = i3; ingred4 = i4; ingred5 = i5;
            amt1 = a1; amt2 = a2; amt3 = a3; amt4 = a4; amt5 = a5;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2, String i3, int a3, String i4, int a4,
                String i5, int a5, String i6, int a6) {
            name = n;
            numberOfIngreds = num;
            ingred1 = i1; ingred2 = i2;  ingred3 = i3; ingred4 = i4; ingred5 = i5;
            ingred6 = i6;
            amt1 = a1; amt2 = a2; amt3 = a3; amt4 = a4; amt5 = a5; amt6 = a6;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2, String i3, int a3, String i4, int a4,
                String i5, int a5, String i6, int a6, String i7, int a7) {
            name = n;
            numberOfIngreds = num;
            ingred1 = i1; ingred2 = i2;  ingred3 = i3; ingred4 = i4; ingred5 = i5;
            ingred6 = i6; ingred7 = i7; 
            amt1 = a1; amt2 = a2; amt3 = a3; amt4 = a4; amt5 = a5; amt6 = a6;
            amt7 = a7;
        }
        Recipe(String n, int num, String i1, int a1,
                String i2, int a2, String i3, int a3, String i4, int a4,
                String i5, int a5, String i6, int a6, String i7, int a7,
                String i8, int a8) {
            name = n;
            numberOfIngreds = num;
            ingred1 = i1; ingred2 = i2;  ingred3 = i3; ingred4 = i4; ingred5 = i5;
            ingred6 = i6; ingred7 = i7;  ingred8 = i8;
            amt1 = a1; amt2 = a2; amt3 = a3; amt4 = a4; amt5 = a5; amt6 = a6;
            amt7 = a7; amt8 = a8;
        }
    
        public String getIngrediant1() {
            return ingred1;
        }
        
        public String getIngrediant2() {
            return ingred2;
        } 
        
        public String getIngrediant3() {
            return ingred3;
        } 
        
        public String getIngrediant4() {
            return ingred4;
        } 
        
        public String getIngrediant5() {
            return ingred5;
        } 
        
        public String getIngrediant6() {
            return ingred6;
        } 
        
        public String getIngrediant7() {
            return ingred7;
        } 
        
        public String getIngrediant8() {
            return ingred8;
        } 
        
        public int getAmount1() {
            return amt1;
        } 
        
        public int getAmount2() {
            return amt2;
        } 
        
        public int getAmount3() {
            return amt3;
        } 
        
        public int getAmount4() {
            return amt4;
        } 
        
        public int getAmount5() {
            return amt5;
        } 
        
        public int getAmount6() {
            return amt6;
        } 
        
        public int getAmount7() {
            return amt7;
        } 
        
        public int getAmount8() {
            return amt8;
        } 
    
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default

    Why do you have your Recipe objects and List of Recipes in the Recipe class? Think about the real world. does each Chair in your house have a List of every other Chair? Or maybe you have a separate Inventory list that keeps track of your furniture.

    On the other hand, why not have a List of Ingredients? You can also tidy up all your constructors by using a feature valled varargs.
    Java Code:
    Recipe(String... ingredients) { // note the dots
        loop over ingredients {
            add to ingredient list
        }
    }
    The varargs parameter is an array so you can treat it as you would a normal array.

  3. #3
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Why do you have your Recipe objects and List of Recipes in the Recipe class? Think about the real world. does each Chair in your house have a List of every other Chair? Or maybe you have a separate Inventory list that keeps track of your furniture.
    Ohh, that makes much more sense now. ._.

    Quote Originally Posted by Junky View Post
    On the other hand, why not have a List of Ingredients? You can also tidy up all your constructors by using a feature valled varargs.
    Java Code:
    Recipe(String... ingredients) { // note the dots
        loop over ingredients {
            add to ingredient list
        }
    }
    The varargs parameter is an array so you can treat it as you would a normal array.
    What exactly do the dot's do? I have never seen that before. Does that make it so that I only needed to have one Recipe constructor, and do something like this? How does it know when the ingredients/amount has run out.

    When I declare it, do I need to do String[] ingredients, or..?

    Java Code:
    Recipe(String ... ingredients, int ... amt)
    For the looping, and adding, I would assume that I would need to do something like this.

    Java Code:
    if(ingredients.contains("Sugar", "Milk", "Etc") 
    //add them.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default

    Here's an example:
    Java Code:
    import java.util.ArrayList;
    
    class Test {
        ArrayList<String> list = new ArrayList<String>();
        
        Test(String... args) {
            for(String s : args) {
                list.add(s);
            }
        }
    
        public String toString() {
            return list.toString();
        }
    
        public static void main(String[] args) {
            Test t1 = new Test("one", "two", "three");
            Test t2 = new Test("one", "two", "three", "four", "five");
            System.out.println(t1);
            System.out.println(t2);
        }
    }
    The constructor also uses an enhanced for loop which you may not have seen before. As you can see all you need to do is list all the parameters in the call to the constructor. It can be 1, 2, 3 or as many as you like (until you run out of memory). The JVM handles the rest.

  5. #5
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Ohh, that makes alot more sense on the ... thing. But that loop, how does it know when to stop looping? Does args, a place holder, and I need to put in a int so that it will loop how ever many times the int says?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default

    No, you do not need to use any int to control the loop. It has its own inbuilt control. Another name for the loop is the "for each" loop. In English it is saying "for each" String in args enter the loop. As soon as it runs out of "things" in the array (or List) the loop stops. You can take it at face value that if your array has 4 items it will loop 4 times, 5 items 5 time etc. Or if you are more inquisitive you can take a look at the Iterable and Iterator interfaces.

  7. #7
    Qsc
    Qsc is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Ohh, ok that makes perfect sense now. xD. Thank you so much. :D.

Similar Threads

  1. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 05:35 PM
  2. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 08:51 PM
  3. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 05:32 PM
  4. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 07:33 PM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 01:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •