3
\$\begingroup\$

sorry for asking this question. Some of you will probably find it very easy but I just can't figure out how to do it.

I want to write a custom Recipe Manager. It has to check the recipes. Therefor I create an ArrayList<Item[]> recipes = new ArrayList();

The Item Array store 4 items(3 inputs and one output):Item[] items = {Item1, Item2, Item3, Item4};

I want to get a function that does this:

  1. I give it a Item and it gives me an ArrayList containing all the Item[]s containing that Item as the 0th index and a Boolean that is true when there is any Item[] containing the Item.

  2. Then I give (if needed to another function) an second Item. It returns me a new ArrayList containing all of the previous Item[]s that containe the Item and a Boolean similiar to 1 but for the 1th index.

  3. Analog to 2. (I think you get what I mean).

The 3 points have to be handled in another method call. Thank you very much in advance Nova

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I hope this function gives you a raw idea on how it might work:

// a couple of items to help out with the imagination
Item apple = new Item("Apple");
Item sugar = new Item("Sugar");
Item peach = new Item("Peach");
Item marmelade = new Item("Marmelade");
Item banana= new Item("Banana");
Item milk= new Item("Milk");
Item shake = new Item("Banana Shake");

// as an example: 2 recipes for better imagination
Item[] marmeladeRecipe= {apple, peach, sugar, marmelade};
Item[] shakeRecipe = {banana, milk, sugar, shake};

// a list of all the recipes you have
ArrayList<Item[]> baseRecipes = new ArrayList<Item[]>();
// now add the recipes to your list
baseRecipes.add(marmeladeRecipe);
baseRecipes.add(shakeRecipe);

You can now create separate methods and check for each element one after another like you mentioned in your comment below. Exemplatory for the first item it looks like this and should be anologue for the other two indices:

// this is for the 1. element or 0th  index
ArrayList<Item[]> getRecipesForFirst(Item item, ArrayList<Item> allRecipes) {
   // a list of recipes with all item as the first element
   ArrayList<Item[]> recipes = new ArrayList<Item>();
   // for each recipe in the list of all recipes...
   for(Item[] recipe in allRecipes) {
       // only adds recipe from the allRecipes list to the recipes list 
       // if item matches the first entry
       if(recipe[0].equals(item)) {
          recipes.add(recipe);
      }
   }
   return recipes;
}

One more thing to keep in mind and which I edited is that you have to give the method a new ArrayList because otherwise you would get messy results. Furthermore, this does not return a boolean which you demanded but you can simply check that when invoking the method:

// I created a made up item for better imagination
ArrayList<Item> recipesForFirst = getRecipesForFirst(banana, baseRecipes);
// equivalent to your demanded boolean
if(!recipesForFirst.isEmpty()) {
    // loop is needed because there might be multiple recipes
    for(Item[] recipe : recipesForFirst) {
        // create a new list that now checks for second item but
        // only in recipes that have the first item
        ArrayList<Item> recipesForSecond = getRecipesForSecond(recipe[1], recipesForFirst);
        if(!recipesForSecond.isEmpty()) {
            for(Item[] reci : recipesForSecond) {
                ArrayList<Item> recipesForThird = getRecipesForThird(reci[2], recipesForSecond);
                if(!getRecipesForThird.isEmpty()) {
                    for(Item[] r : recipesForThird) {
                        // do something
                    }
                }
            }
        }
    }
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I want do a kind of crafting table. I give it 3 items and then I get the result. So there are 2 possibilities: 1. I let the table take all three items, then test if it is a valid recipe and else it'll give you back your items. 2. You can only input items that are part of (still) valid recipes. So if you have items a,b,c and d and you have a recipe a+b+c and b+c+d it will accept as the first item a or b. If you picked the "a"-recipe you can then only insert b and then c. And from a design point of view I diceded that I would like to do the second option \$\endgroup\$ Jul 4, 2016 at 13:39
  • \$\begingroup\$ I updated my answer, just write another comment if something is unclear :) \$\endgroup\$
    – ByteBiter
    Jul 6, 2016 at 22:51
  • \$\begingroup\$ Could you please explain the "in" in the for loop? I've never seen it anywhere. \$\endgroup\$ Jul 9, 2016 at 9:14
  • \$\begingroup\$ Basically, it is a loop that iterates through a list. \$\endgroup\$
    – ByteBiter
    Jul 9, 2016 at 10:32
  • \$\begingroup\$ My bad, those were supposed to be a simple for each loop. I now fixed it from Python to actual Java code. Sorry about that. \$\endgroup\$
    – ByteBiter
    Jul 9, 2016 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .