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 == "type1") count = container.countType1();
else if (type == "type2") count = container.countType2();
else if (type == "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.
Aucun commentaire:
Enregistrer un commentaire