在查找特定事例时,如何简化过多 IF 语句的使用

How to simplify the use of too many IF statements when finding specific cases

提问人:Thomas Humphreys 提问时间:10/6/2023 最后编辑:ShadowThomas Humphreys 更新时间:10/6/2023 访问量:53

问:

我有代码,用户将从 5 个下拉菜单中选择不同的温度计,这些可以是:“无”、“名称”、“经理”、“访问级别”、“居民”和“位置”,这些将与搜索以获取输入值的 jtextfields 匹配。但是,为了获得这些值,我基本上做了很多if语句来确定值的组合,例如Name,Manager和Location一起将给出SQL语句:query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND propertyManager = ? AND propertyLocation = ?";

这导致了 if 语句的混乱,有什么方法可以简化它吗?

它有效,但是要实际创建它,我必须为不同的组合做数百行,并且以后很难扩展。但是,这是我当前的一些代码:

`if (searchingForName == true) {
          if (searchingForManager == true) {
              if (searchingForAccessLevel == true) { // final lvl 1
                   query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND propertyManager = ? AND accessLevelRequired = ?"; 
                   query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyName = ? AND propertyManager = ? AND accessLevelRequired = ?"; 
              }else if ((searchType1.equals("Inhabitant") || searchType2.equals("Inhabitant") || searchType3.equals("Inhabitant"))) { // final lvl 2
                query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND propertyManager = ? AND propertyInhabitant = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyName = ? AND propertyManager = ? AND propertyInhabitant = ?"; 
            }else if ((searchType1.equals("Location") || searchType2.equals("Location") || searchType3.equals("Location"))) { // finaly lvl 3
                query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND propertyManager = ? AND propertyLocation = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyName = ? AND propertyManager = ? AND propertyLocation = ?"; 
            }
          }  else if ((searchType1.equals("AccessLevel") || searchType2.equals("AccessLevel") || searchType3.equals("AccessLevel"))) { // sub lvl 2
               if ((searchType1.equals("Inhabitant") || searchType2.equals("Inhabitant") || searchType3.equals("Inhabitant"))) { //final lvl 1
                query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND accessLevelRequired = ? AND propertyInhabitant = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyName = ? AND accessLevelRequired = ? AND propertyInhabitant = ?"; 
            }else if ((searchType1.equals("Location") || searchType2.equals("Location") || searchType3.equals("Location"))) { // final lvl 2
                query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND accessLevelRequired = ? AND propertyLocation = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyName = ? AND accessLevelRequired = ? AND propertyLocation = ?"; 
            }
          } else if ((searchType1.equals("Inhabitant") || searchType2.equals("Inhabitant") || searchType3.equals("Inhabitant"))) { // sub lvl 3
               if ((searchType1.equals("Location") || searchType2.equals("Location") || searchType3.equals("Location"))) { // final lvl 1
                query = "SELECT * FROM propertyInfo WHERE propertyName = ? AND propertyInhabitant = ? AND propertyLocation = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyName = ? AND propertyInhabitant = ? AND propertyLocation = ?"; 
          }
               
      //}
      }else if ((searchType1.equals("Manager") || searchType2.equals("Manager") || searchType3.equals("Manager"))) { // main lvl 2
          
          
          if ((searchType1.equals("AccessLevel") || searchType2.equals("AccessLevel") || searchType3.equals("AccessLevel"))) { // sub lvl 1
              
              
            if ((searchType1.equals("Inhabitant") || searchType2.equals("Inhabitant") || searchType3.equals("Inhabitant"))) {// final lvl 1
                   query = "SELECT * FROM propertyInfo WHERE propertyManager = ? AND accessLevelRequired = ? AND propertyInhabitant = ?"; 
                   query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE   propertyManager = ? AND accessLevelRequired = ? AND propertyInhabitant = ?"; 
                   
            }else if ((searchType1.equals("Location") || searchType2.equals("Location") || searchType3.equals("Location"))) { // final lvl 2
                query = "SELECT * FROM propertyInfo WHERE propertyManager = ? AND accessLevelRequired = ? AND propertyLocation = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyManager = ? AND accessLevelRequired = ? AND propertyLocation = ?"; 
            }
          }  else if ((searchType1.equals("Inhabitant") || searchType2.equals("Inhabitant") || searchType3.equals("Inhabitant"))) { // sub lvl 2
                if ((searchType1.equals("Location") || searchType2.equals("Location") || searchType3.equals("Location"))) { // final lvl 1
                query = "SELECT * FROM propertyInfo WHERE propertyManager = ? AND propertyInhabitant = ? AND propertyLocation = ?"; 
                query2 = "SELECT COUNT(*) AS recordCount FROM propertyInfo WHERE  propertyManager = ? AND propertyInhabitant = ? AND propertyLocation = ?"; 
            }
          }
        `  } 
      }
Java 简化

评论

0赞 mcarn 10/6/2023
您可以尝试倒车,如果像 softwareengineering.stackexchange.com/questions/47789/ 一样......
1赞 Akina 10/6/2023
xyproblem.info提供问题本身。
3赞 Progman 10/6/2023
您是否尝试过根据您拥有的条件动态构建准备好的语句?
0赞 Hovercraft Full Of Eels 10/6/2023
不相关,但从不做,而是总是做。它只是更干净,风险更小。if (foo == true) {if (foo) {

答:

2赞 M. Pour 10/6/2023 #1

您可以通过基于所选参数动态构造 SQL 查询来简化此过程。下面是一个更结构化的方法:

// Define the base query
String query = "SELECT * FROM propertyInfo WHERE 1 = 1";

// Initialize a list to store query parameters
List<Object> parameters = new ArrayList<>();

// Check if Name is selected
if (searchingForName) {
    query += " AND propertyName = ?";
    parameters.add(nameValue);
}

// Check if Manager is selected
if (searchingForManager) {
    query += " AND propertyManager = ?";
    parameters.add(managerValue);
}

// Check if AccessLevel is selected
if (searchingForAccessLevel) {
    query += " AND accessLevelRequired = ?";
    parameters.add(accessLevelValue);
}

// Check if Inhabitant is selected
if (searchingForInhabitant) {
    query += " AND propertyInhabitant = ?";
    parameters.add(inhabitantValue);
}

// Check if Location is selected
if (searchingForLocation) {
    query += " AND propertyLocation = ?";
    parameters.add(locationValue);
}

对于每个选定的参数(例如,Name、Manager 等),您可以动态地将相应的条件添加到查询字符串中,并将参数值添加到参数列表中。最后,您可以使用带有参数的预准备语句执行查询。

祝你有一个好的!

评论

0赞 Thomas Humphreys 10/6/2023
该代码看起来非常好,并且是我正在寻找的,但问题是我不知道数组列表是如何工作的,以及如何将其输出到实际代码中,您能否链接一篇文章以便我了解数组列表?
0赞 Old Dog Programmer 10/7/2023
@ThomasHumphreys 这不是一个教程,但 Oracle 在线提供了类和接口的 API。查看 ArrayList API 。如果你想找到教程,你可以谷歌搜索它们
1赞 Reilas 10/7/2023
@ThomasHumphreys,这里是关于集合框架Java 教程,其中包括 ArrayList 类。 Trail: Collections(Java™ 教程)