I have a set of classes that implement a particular interface and I have a set of checkboxes. I want to throw an error if no checkboxes are selected. If atleast one or more checkboxes are selected, then it should create objects associated with that checkbox.
This is how I done.
interface U { ... }
class A implements U { ... }
class B implements U { ... }
class C implements U { ... }
class Main {
//....
//....
public void findSelectedCheckBoxesAndCreateObjects() {
if(!(checkboxA.isSelected() || checkboxB.isSelected() || checkboxC.isSelected()) {
System.out.println("No checkboxes selected");
return;
}
//if any selected, create associated object
if(checkboxA.isSelected()) new A(file);
if(checkboxB.isSelected()) new B(file);
if(checkboxC.isSelected()) new C(file);
}
}
Now I have 3 problems.
- This is just a sample code. Original has 8 checkboxes and classes with more coming.
- I can't keep adding
|| checkboxD.isSelected()
every time I have a new class for checking it. - Same thing. I can't keep adding
if(checkboxD.isSelected()) new D(file);
for every class.
It is very inelegant. Can I have some kind of loop that removes the redundant code?
Please give me your suggestions. Thank you.