-3

For example, I have:

Action enum:

public enum Action{
    Pour, Mix, Shake, Add;

    public Ingredient ingredient;

    public String toString() {
        return super.toString() + " " + this.ingredient.toString();
    }
}

Code example:

Ingredient [] ingredients1 = {
            new DrinkingIngredient("Vodka", 50, 20),
            new DrinkingIngredient("Tomato Juice", 120, 0),
            new DrinkingIngredient("lemon juice", 10, 0),
            new Component("Ice", 380),
            new Component("Celery", 15),

        };

        Action act1 = Action.Add;
        act1.ingredient = ingredients1[0];
        Action act2 = Action.Mix;
        act2.ingredient = ingredients1[1];
        Action act3 = Action.Mix;
        act3.ingredient = ingredients1[2];
        Action act4 = Action.Shake;
        Action act5 = Action.Add;
        act5.ingredient = ingredients1[3];
        Action act6 = Action.Add;
        act6.ingredient = ingredients1[4];

I know this is not properly the best example I could do. Sorry about that, but I can explain that all Action enums have the same property value as the last one act6. So I can assume that the same enums in Java cannot live at once. Am I right? What should I do in that situation?

And yeah, after that:

System.out.print(act1);

I get: "Add Celery" just like the last one act6 instead "Add Vodka" as I expected

Bor is
  • 75
  • 9
  • 4
    Giving `enum` values public mutable fields is a terrible idea. And why do you expect your results to be different? There is only **one** object for `Action.Add`, as that is how `enum`s work. `act1 == act5 == act6`. – bcsb1001 Nov 18 '17 at 01:04
  • 3
    "What should I do in that situation?" Not use an enum. – Oleg Nov 18 '17 at 01:15
  • 1
    I also should mention that you should never use a to string of Enums. If you ever plan on obfuscating your executable the enum names will be entirely lost and replaced with simple letters, therefore breaking your to string. I get that you probably aren't going to be using any of this as production code, but avoiding enum to string is a good thing to get in the habit of. – Krythic Nov 18 '17 at 01:20
  • 1
    You should review the [Java Tutorial on Enum Types](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) – D.B. Nov 18 '17 at 01:44

1 Answers1

-1

The direct answer to your question is "No, you cannot get multiple enums". As you have discovered, it is the nature of enums that only one instance of a given enum constant ever exists. So you have just one Action.Add, one Action.Mix, etc. If you have an Ingredient as part of an Action, you can then only ever represent one combination of an action and ingredient. So, you can have two "Add" steps, but they both have to add the same ingredient. You can "Add Vodka" twice, or you can "Add Celery" twice, but you can't both "Add Vodka" and "Add Celery".

The solution to your problem is to separate the ingredient from the action. You need an object that can represent a recipe step: a combination of an action and the ingredient to use in performing that action:

public class RecipeStep {
    public final Action action;
    public final Ingredient ingredient;
    public RecipeStep(Action a, Ingredient i){
        this.action = a;
        this.ingredient = i;
    }
    public String toString(){
        return Action.toString() + 
            (ingredient != null 
                ? (" " + ingredient.toString()) 
                : ""
             );
    }
}

Then you make up your recipe as a list of RecipeStep instances:

RecipeStep step1 = new RecipeStep(Action.Add, ingredients1[0]);
RecipeStep step2 = new RecipeStep(Action.Mix, ingredients1[1]);
RecipeStep step3 = new RecipeStep(Action.Mix, ingredients1[2]);
RecipeStep step4 = new RecipeStep(Action.Shake, null);
RecipeStep step5 = new RecipeStep(Action.Add,ingredients1[3]);
RecipeStep step6 = new RecipeStep(Action.Add,ingredients1[4]);
Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21