如果使用JAVA,如何编写更优化和通用的代码,而不是200?

我正在处理报告代码,我需要生成此报告输出。

汽车对象具有50个字符串,50个整数,50个日期,50个大十进制字段。其中一些值已满。

如果需要,我通过输入200取消了这些控件,但我正在寻找更优化和通用的版本。

if (carDTO.getcarProperty() != null) {
    if (carContent.getVariableType().equals(VariableType.STRING)) {
        if (carDTO.getcustomEntryString1() != null) {
            ReportUtils.setvalue(worksheet,rowIndex,cellIndex,carDTO.getcustomEntryString1());
        } else if (carDTO.getcustomEntryString2() != null) {
            ReportUtils.setvalue(worksheet,carDTO.getcustomEntryString2());
        } else if (carDTO.getcustomEntryString3() != null) {
            ReportUtils.setvalue(worksheet,carDTO.getcustomEntryString3());
        }

        ...

         else if (carDTO.getcustomEntryString50() != null) {
            ReportUtils.setvalue(worksheet,carDTO.getcustomEntryString50());
        }
    } else if (carContent.getVariableType().equals(VariableType.BIGDECIMAL)) {
        if (carDTO.getcustomEntryBigDecimal1() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryBigDecimal1());
        } else if (carDTO.getcustomEntryBigDecimal2() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryBigDecimal2());
        } else if (carDTO.getcustomEntryBigDecimal3() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryBigDecimal3());
        }

        ...

         else if (carDTO.getcustomEntryBigDecimal50() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryBigDecimal50());
        }

    } else if (carContent.getVariableType().equals(VariableType.INTEGER)) {
         if (carDTO.getcustomEntryInteger1() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryInteger1());
        } else if (carDTO.getcustomEntryInteger2() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryInteger2());
        } else if (carDTO.getcustomEntryInteger3() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryInteger3());
        }

        ...

         else if (carDTO.getcustomEntryInteger50() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryInteger50());
        }

    } else if (carContent.getVariableType().equals(VariableType.ZONEDDATETIME)) {
        if (carDTO.getcustomEntryDate1() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryDate1());
        } else if (carDTO.getcustomEntryDate2() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryDate2());
        } else if (carDTO.getcustomEntryDate3() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryDate3());
        }

        ...

         else if (carDTO.getcustomEntryDate50() != null) {
             ReportUtils.setvalue(worksheet,carDTO.getcustomEntryDate50());
        } 
    }

}

如果需要,我通过输入200取消了这些控件,但我正在寻找更优化和通用的版本。

例如;

carDTO.getcustomEntryString1()=“ test1”,

carDTO.getcustomEntryString2()=“ test2”,

carDTO.getcustomEntryString3()=“ test3”

字段carDTO.getcustomEntryString1(),carDTO.getcustomEntryString2(),carDTO.getcustomEntryString3()已满,但是在输入第一个(如果我为报告编写的代码中)后,它将保留条件并填充相同的数据。写入值为“ test1”的“ carDTO.getcustomEntryString1()”。

|-----------------------|----------------------|-----------------------|
| getcustomEntryString1 |getcustomEntryString2 | getcustomEntryString3 |
|-----------------------|----------------------|-----------------------|
|       test1           |         test1        |    test1              |
|-----------------------|----------------------|-----------------------|

如何解决此问题并编写更优化的版本?

如何编写更优化和通用的而不是200?

g455484794 回答:如果使用JAVA,如何编写更优化和通用的代码,而不是200?

老实说,我无法跟踪所有这些条件来回答问题的“此实现有什么问题”部分。但是,在架构方面,我认为拥有200种getter方法是一个可怕的想法。而是考虑使用HashMap并使用属性名称来检索这些字段。想象一下

之类的签名
String getStrProperty(String name)
Integer getIntProperty(String name)
Date getDateProperty(String name)
BigInteger getBIProperty(String name)

您还希望有4个以HashMap.put(Key,Val)开头的设置器。

edit:精挑细选,但您不希望使用更多的泛型实现。您需要更多的抽象实现。除非您想销毁不同类型的属性之间的类型检查并将它们全部转换为Object,否则泛型实际上不适合在这里使用。

本文链接:https://www.f2er.com/3086509.html

大家都在问