I need to count a specific Object, but I'll know which object only at runtime. Right now I have something like
public class Details {
private String typeOfObjectRequired;
private int numberOfObjectRequired;
}
And in another class I have
public class Container {
private List<Type1> type1List;
private List<Type2> type2List;
private Type3 type3Object;
public int countType1() {
return type1List.size();
}
public int countType2() {
return type2List.size();
}
public int countType3() {
return type3Object.getNumberOfSomething();
}
}
Now I'm doing like this (in a third class that has both Details and Container as attributes)
public boolean hasNumberOfObjectRequired() {
int count = 0;
String type = details.getTypeOfObjectRequired();
if(type.equals("type1")) count = container.countType1();
else if (type.equals("type2")) count = container.countType2();
else if (type.equals("type3")) count = container.countType3();
if (count > details.getNumberOfObJectRequired) return true;
return false;
}
Is there a better way to do this? I don't like to have so many if, also because I have more than just 3 different types.
EDIT: Right now I have 5 different types, and I always need only one of them. Basically I want to call different methods based on the String