dimanche 17 novembre 2019

How to write more optimized and generic instead of 200 if condition using JAVA?

I'm working on a report code and I need to produce this report output.

A car object has 50 strings, 50 integers, 50 dates, 50 big decimal fields. Some of these values are full.

I took these controls off by typing 200 if but I am looking for a more optimized and generic version.

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, rowIndex, cellIndex, carDTO.getCustomEntryString2());
        } else if (carDTO.getCustomEntryString3() != null) {
            ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryString3());
        }

        ...

         else if (carDTO.getCustomEntryString50() != null) {
            ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryString50());
        }
    } else if (carContent.getVariableType().equals(VariableType.BIGDECIMAL)) {
        if (carDTO.getCustomEntryBigDecimal1() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal1());
        } else if (carDTO.getCustomEntryBigDecimal2() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal2());
        } else if (carDTO.getCustomEntryBigDecimal3() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal3());
        }

        ...

         else if (carDTO.getCustomEntryBigDecimal50() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal50());
        }

    } else if (carContent.getVariableType().equals(VariableType.INTEGER)) {
         if (carDTO.getCustomEntryInteger1() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger1());
        } else if (carDTO.getCustomEntryInteger2() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger2());
        } else if (carDTO.getCustomEntryInteger3() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger3());
        }

        ...

         else if (carDTO.getCustomEntryInteger50() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger50());
        }

    } else if (carContent.getVariableType().equals(VariableType.ZONEDDATETIME)) {
        if (carDTO.getCustomEntryDate1() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate1());
        } else if (carDTO.getCustomEntryDate2() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate2());
        } else if (carDTO.getCustomEntryDate3() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate3());
        }

        ...

         else if (carDTO.getCustomEntryDate50() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate50());
        } 
    }

}

I took these controls off by typing 200 if but I am looking for a more optimized and generic version.

For example;

carDTO.getCustomEntryString1 () = "test1",

carDTO.getCustomEntryString2 () = "test2",

carDTO.getCustomEntryString3 () = "test3"

The fields carDTO.getCustomEntryString1 (), carDTO.getCustomEntryString2 (), carDTO.getCustomEntryString3 () are full, but after entering the first if in the code I wrote for the report, it leaves the condition and is filled with same data. "carDTO.getCustomEntryString1()" that is the value "test1" is written.

|-----------------------|----------------------|-----------------------|
| getCustomEntryString1 |getCustomEntryString2 | getCustomEntryString3 |
|-----------------------|----------------------|-----------------------|
|       test1           |         test1        |    test1              |
|-----------------------|----------------------|-----------------------|

How can I solve this problem and write a more optimized version?

How to write more optimized and generic instead of 200 if?

Aucun commentaire:

Enregistrer un commentaire