There are 3 different user input fields like Text box & Drop downs on the UI. The user can search a table with any of the three input fields or by providing different combinations of input fields to narrow the result set.I have used if else statements to implement my logic using these three fields, but the problem is the code looks very ugly with lot of if-else statements and more over if there is one more field added to the UI then I have to check for more combinations with if else statements. I really appreciate if someone throws light on how to follow best practices or better approach in these scenarios.
My Code:
public boolean filterResults(UserInputs userInputs){
String firstName = userInputs.getFirstName();
String lastName = userInputs.getLastName();
String phoneNumber = userInputs.getNumber();
//**search only by First Name**
if((firstName!=null && firstName!="")
&& (lastName==null || lastName=="" )
&& phoneNumber==null || phoneNumber ==""){
//logic to narrow down the result set based on condition met input values
list =DAO.getService(firstName);
return true;
}else if((lastName!=null && lastName!="")// **search only by Last Name**
&& (firstName== null || firstName == "")
&& (phoneNumber==null || phoneNumber == "")){
//logic to narrow down the result set based on condition met input values
list =DAO.getService(lastName); return true;
}else if((phoneNumber!=null && phoneNumber!="")// **Search Only by PhoneNumber**
&& (lastName==null || lastName=="" )
&& (firstName== null || firstName == "")){
//logic to narrow down the result set based on condition met input values
list =DAO.getService(phoneNumber); return true;
}else if((firstName!=null && firstName!="")// **Search by all params**.
&&(lastName!=null && lastName!="")
&& (phoneNumber!=null && phoneNumber!="")){
//logic to narrow down the result set based on condition met input values
list =DAO.getService(firstName, lastName, phoneNumber);
return true;
}else if((firstName!=null && firstName!="")//**Search by first&last name.**
&& (lastName!=null || lastName!="" )
&& (phoneNumber==null || phoneNumber =="")){
//logic to narrow down the result set based on condition met input values
list =DAO.getService(firstName, lastName);
return true;
}else if (.......){// **Search by last name & phone number.**
return true;
}else if (.........){// **Search by phone number & first name.**
return true;}
return false;
}