lundi 25 avril 2016

Mutiple values testing, how to replace many else if statements?

In my android app, I am testing some strings (that I got back from another activity) to see if they are empty. If not, I build my url from these values, example : (EDITED)

    //country only
    if (!fetchData_country.isEmpty() && fetchData_city.isEmpty() && fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()){
        query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + URL_LANGUAGE;
        Log.i("INFO", "Url country only : " + query_url);
    }
    //country & price min
    else if(!fetchData_country.isEmpty() && fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) {
        query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE;
        Log.i("INFO", "Url country & price_min : " + query_url);
    }

    //city & price_min
    else if(fetchData_country.isEmpty() && !fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) {
        query_url = URL_PRODUCT + "?" + URL_SEARCH_CITY + id_city + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE;
        Log.i("INFO", "Url city & price_min : " + query_url);
    }

    // country & city & pmin
    else if(!fetchData_country.isEmpty() && !fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) {
        query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country +  "&" + URL_SEARCH_CITY + id_city +  "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE;
        Log.i("INFO", "Url country & city & pmin : " + query_url);
    }//many values to test still... 

I wonder how I can improve these testing parts, thanks for helping !

Aucun commentaire:

Enregistrer un commentaire