title: java compare multiple values and find best match
body: <p>I need to find best matched employee salary in the DB records as:</p>
  
  <pre><code>Name:   City:     State:
  
  A       (null)    (null)
  
  A       (null)    DEL
  
  (null)  (null)    (null)
  
  A        SAKET    DEL
  </code></pre>
  
  <p>Match order should be:</p>
  
  <p><strong>1.</strong> NAME = name, STATE = state, CITY = city </p>
  
  <p><strong>2.</strong> NAME = name, STATE = state , CITY = NULL</p>
  
  <p><strong>3.</strong> NAME = name, STATE = NULL, CITY = NULL</p>
  
  <p><strong>4.</strong> NAME = NULL, STATE = NULL, CITY = NULL</p>
  
  <p>Means if in a row where all attributes matches – it should be selected, if we do not have that kind of data we should go to next best option like select state and city as NULL, etc.</p>
  
  <p>My code as below, is giving me correct results but I need a more efficient way.</p>
  
  <pre><code>    private static BigDecimal getsalaryForBestMatch(ResultSet results, EmployeeRq request) throws Exception{
      BigDecimal salary = null;
      BigDecimal salaryWithState = null;
      BigDecimal salaryWithName = null;
      BigDecimal salaryWithNoMatch = null;
       while (results.next()) {
  
          String billerName = results.getString("EMP_NAME") != null ? results.getString("EMP_NAME").trim() : null;
          String city = results.getString("CITY") != null ? results.getString("CITY").trim() : null;
          String state = results.getString("STATE") != null ? results.getString("STATE").trim() : null;
  
          BigDecimal salaryRslt = null;
  
          if(results.getString("SALARY") != null){
              salaryRslt = BigDecimal.valueOf(results.getDouble("SALARY"));               
          }  
          if(billerName != null &amp;&amp; !billerName.equals("") &amp;&amp; billerName.equals(request.getBillPaymentsalaryCalculateInfo().getBillerName())){
              if(city != null &amp;&amp; !city.equals("") &amp;&amp; city.equals(request.getMsgRqHdr().getCity()) &amp;&amp;
                      state != null &amp;&amp; !state.equals("") &amp;&amp; state.equalsIgnoreCase(request.getMsgRqHdr().getstate())){
                  salary = salaryRslt;
                  break;
              } else if((city == null || city.equals("")) &amp;&amp; state != null &amp;&amp; !state.equals("") &amp;&amp;
                      state.equalsIgnoreCase(request.getMsgRqHdr().getState())){
                  salaryWithState = salaryRslt;                   
              } else if((city == null || city.equals("")) &amp;&amp; (state == null || state.equals(""))){
                  salaryWithName = salaryRslt;                    
              }
          } else if((billerName == null || billerName.equals("")) &amp;&amp; (city == null || city.equals("")) &amp;&amp; 
                  (state == null || state.equals(""))){
              salaryWithNoMatch = salaryRslt;             
          }
       }
  
       if(salary != null){
           return salary;
       } else if(salaryWithState != null){
           salary = salaryWithState;
       } else if(salaryWithName != null){
           salary = salaryWithName;
       } else if(salaryWithNoMatch != null){
           salary = salaryWithNoMatch;
       } 
  
       return salary;
  
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: I dont want to use 3 extra variables: salaryWithState, salaryWithName,  salaryWithNoMatch.</p>