I use the enumeration to identify several options that can be added to the product (custom logo, colors, etc.). Each option has several options (identifier, description in two languages, where changes are made in the process, price, etc.). It has methods for further defining a change (for example, which color should be used) or for overriding certain parameters. A single product can have either zero or one or more options stored in an ArrayList. Also, one option can be applied more than once.
While I use the option only once, everything is in order. But in case I use it more than once, it seems that everyone gets the same options.
Code example:
TestClass.java:
import java.util.ArrayList; public class TestClass { public static void main(String[] args) { ArrayList<TestEnum> enums = new ArrayList<>(); TestEnum enumVar; enumVar = TestEnum.TEST1; enums.add(enumVar); enumVar = null; enumVar = TestEnum.TEST2; enums.add(enumVar); enumVar = null; enumVar = TestEnum.TEST2; enumVar.setOp1("new op21"); enumVar.setOp2("new op22"); enumVar.setOp3("new op23"); enums.add(enumVar); enumVar = null; enums.forEach((element) -> { System.out.println("op1: " + element.getOp1() + "; op2: " + element.getOp2() + "; op3: " + element.getOp3()); /* Expecting: op1: op11; op2: op12; op3: op13 op1: op21; op2: op22; op3: op23 op1: new op21; op2: new op22; op3: new op23 Output: op1: op11; op2: op12; op3: op13 op1: new op21; op2: new op22; op3: new op23 op1: new op21; op2: new op22; op3: new op23 */ }); } }
TestEnum.java:
public enum TestEnum { TEST1("op11", "op12", "op13"), TEST2("op21", "op22", "op23"), TEST3("op31", "op32", "op33"), TEST4("op41", "op42", "op43"), TEST5("op51", "op52", "op53"); private String op1; private String op2; private String op3; TestEnum(String op1, String op2, String op3) { this.op1 = op1; this.op2 = op2; this.op3 = op3; } public void setOp1(String op1) { this.op1 = op1; } public String getOp1() { return this.op1; } public void setOp2(String op2) { this.op2 = op2; } public String getOp2() { return this.op2; } public void setOp3(String op3) { this.op3 = op3; } public String getOp3() { return this.op3; } }
Is it possible to do what I mean with an enumeration?
If so, what am I doing wrong? Maybe it is possible to create a copy of the listing in a certain state?