vendredi 1 juillet 2016

If statement with an unknown amount of conditions

I am working on a small application, where I am trying to have an object (Property) be put into an ArrayList<Property>. Basically I'm making a search function that operates on a Map of what to search for.

E.g.: I want to search for all Properties with name "12 Hello Lane" that were listed between 2013 and 2015.

I would then do like so:

for (Property p : properties){
    if (p.getName().equals("12 Hello Lane") && p.getListingDate().getYear() >=2013 && p.getListingDate().getYear() <= 2015){
        //Add to list
    }
}

The problem being, I am passing a Map of properties to compare, the key being the method name and the value being the desired value. In other words:

Key: "getName"
Value: "12 Hello Lane"
Key: "getYear"
Value: 2013-2015
...

My question is how do I make it so that I can use a single if statement to cover all the properties? Something like:

for (Property p : properties){
    if (p.getClass().getMethod(options.getKey()) == options.getValue()){
        //Add to list
    }
}

I don't know if I'm phrasing this correctly, but I hope you guys get my drift.

Aucun commentaire:

Enregistrer un commentaire