如何避免这么多if else语句

时间:2017-05-03 07:42:04

标签: java swing jtable

我实现了一个在数据库中扫描数据的类,它根据数据库中的内容在表中显示数据。但是,为了满足数据库的所有可能返回并受JTable显示信息的限制,我发现自己编写了很多if else语句,这些语句容易出错。

package googo.pmms.project2.financialStatement;

import googo.pmms.project2.frameHelper.ReportsModelData;
import java.util.List;
import java.util.Map;
import javax.swing.JTable;
import javax.swing.table.TableRowSorter;

public class TheBalanceSheet implements BalanceSheet {
    ReportsModelData model1;
    String title;
    List<String> tableHeaders;
    List<Object> dataBody;

    Map<Integer, List<Object>> data;

    BalanceSheetUtility bUtility = new BalanceSheetUtility();

    public TheBalanceSheet() {
    }

    public boolean createBalanceSheet(JTable table, String materialDate) {    
        boolean bs = true;    
        AssetAccount asset = new AssetAccount(materialDate);

        LiabilityEquityAccounts liabEqui = new LiabilityEquityAccounts(materialDate);

        if (!asset.isAssetsEmpty()) {    
            this.setBodyMap(0, asset.mainAssetTitle());
            if (!asset.isCurrentAssetsEmpty()) {
                this.setBodyMap(1, asset.currentAssetTitle());
                this.setBodyMap(2, asset.currentAssets());
                this.setBodyMap(3, asset.totalCurrentAssets());

                if (!asset.isNonCurrentAssetsEmpty()) {
                    this.setBodyMap(4, asset.nonCurrentAssetTitle());
                    this.setBodyMap(5, asset.nonCurrentAssets());
                    this.setBodyMap(6, asset.totalNonCurrentAssets());
                    this.setBodyMap(7, this.totalAssets());

                    if (!liabEqui.isLiabilitiesEmpty()) {

                        this.setBodyMap(8, liabEqui.mainLiabilitiesEquityTitle());

                        if (!liabEqui.isCurrentLiabilityEmpty()) {

                            this.setBodyMap(9, liabEqui.currentLiabilitiesTitle());
                            this.setBodyMap(10, liabEqui.currentLiabilities());
                            this.setBodyMap(11, liabEqui.totalCurrentLiabilities());

                            if (!liabEqui.isNonCurrentLiabilityEmpty()) {
                                this.setBodyMap(12, liabEqui.nonCurrentLiabilitiesTitle());
                                this.setBodyMap(13, liabEqui.nonCurrentLiabilities());
                                this.setBodyMap(14, liabEqui.totalNonCurrentLiabilities());
                                this.setBodyMap(15, this.totalLiabilities());

                                if (!liabEqui.isEquityEmpty()) {
                                    this.setBodyMap(16, liabEqui.equityTitle());
                                    this.setBodyMap(17, liabEqui.equity());
                                    this.setBodyMap(18, liabEqui.totalEquity());
                                    this.setBodyMap(19, this.totalLiabilitiesEquity());
                                }

                            } else if (!liabEqui.isEquityEmpty()) {
                                this.setBodyMap(12, liabEqui.equityTitle());
                                this.setBodyMap(13, liabEqui.equity());
                                this.setBodyMap(14, liabEqui.totalEquity());
                                this.setBodyMap(15, this.totalLiabilitiesEquity());
                            }    
                        } else if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                            this.setBodyMap(9, liabEqui.nonCurrentLiabilitiesTitle());
                            this.setBodyMap(10, liabEqui.nonCurrentLiabilities());
                            this.setBodyMap(11, liabEqui.totalNonCurrentLiabilities());
                            this.setBodyMap(12, this.totalLiabilities());

                            if (!liabEqui.isEquityEmpty()) {
                                this.setBodyMap(13, liabEqui.equityTitle());
                                this.setBodyMap(14, liabEqui.equity());
                                this.setBodyMap(15, liabEqui.totalEquity());
                                this.setBodyMap(16, this.totalLiabilitiesEquity());
                            }
                        } else {
                      }
                    }
                } else { 
                    this.setBodyMap(4, this.totalAssets());

                    if (!liabEqui.isLiabilitiesEmpty()) {
                      this.setBodyMap(5, liabEqui.mainLiabilitiesEquityTitle());
                    if (!liabEqui.isCurrentLiabilityEmpty()) {
                        this.setBodyMap(6, liabEqui.currentLiabilitiesTitle());
                        this.setBodyMap(7, liabEqui.currentLiabilities());

                            this.setBodyMap(8, liabEqui.totalCurrentLiabilities());

                            if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                                this.setBodyMap(9, liabEqui.nonCurrentLiabilitiesTitle());

                                this.setBodyMap(10, liabEqui.nonCurrentLiabilities());

                                this.setBodyMap(11, liabEqui.totalNonCurrentLiabilities());

                                this.setBodyMap(12, this.totalLiabilities());

                                if (!liabEqui.isEquityEmpty()) {

                                    this.setBodyMap(13, liabEqui.equityTitle());
                                    this.setBodyMap(14, liabEqui.equity());
                                    this.setBodyMap(15, liabEqui.totalEquity());
                                    this.setBodyMap(16, this.totalLiabilitiesEquity());
                                }

                            } else if (!liabEqui.isEquityEmpty()) {
                                this.setBodyMap(9, liabEqui.equityTitle());
                                this.setBodyMap(10, liabEqui.equity());
                                this.setBodyMap(11, liabEqui.totalEquity());
                                this.setBodyMap(12, this.totalLiabilitiesEquity());
                            }

                        } else if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                            this.setBodyMap(6, liabEqui.nonCurrentLiabilitiesTitle());
                            this.setBodyMap(7, liabEqui.nonCurrentLiabilities());
                            this.setBodyMap(8, liabEqui.totalNonCurrentLiabilities());
                            this.setBodyMap(9, this.totalLiabilities());

                            if (!liabEqui.isEquityEmpty()) {
                                this.setBodyMap(10, liabEqui.equityTitle());
                                this.setBodyMap(11, liabEqui.equity());
                                this.setBodyMap(12, liabEqui.totalEquity());
                                this.setBodyMap(13, this.totalLiabilitiesEquity());
                            }
                        } else if (!liabEqui.isEquityEmpty()) {
                            this.setBodyMap(6, liabEqui.equityTitle());
                            this.setBodyMap(7, liabEqui.equity());
                            this.setBodyMap(8, liabEqui.totalEquity());
                            this.setBodyMap(9, this.totalLiabilitiesEquity());
                        }

                    }
                }

            } else if (!asset.isNonCurrentAssetsEmpty()) {

                this.setBodyMap(1, asset.nonCurrentAssetTitle());
                this.setBodyMap(2, asset.nonCurrentAssets());
                this.setBodyMap(3, asset.totalNonCurrentAssets());
                this.setBodyMap(4, this.totalAssets());

                if (!liabEqui.isLiabilitiesEmpty()) {

                    this.setBodyMap(5, liabEqui.mainLiabilitiesEquityTitle());

                    if (!liabEqui.isCurrentLiabilityEmpty()) {

                        this.setBodyMap(6, liabEqui.currentLiabilitiesTitle());
                        this.setBodyMap(7, liabEqui.currentLiabilities());
                        this.setBodyMap(8, liabEqui.totalCurrentLiabilities());
                        if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                            this.setBodyMap(9, liabEqui.nonCurrentLiabilitiesTitle());
                            this.setBodyMap(10, liabEqui.nonCurrentLiabilities());
                            this.setBodyMap(11, liabEqui.totalNonCurrentLiabilities());
                            this.setBodyMap(12, this.totalLiabilities());

                            if (!liabEqui.isEquityEmpty()) {
                                this.setBodyMap(13, liabEqui.equityTitle());
                                this.setBodyMap(14, liabEqui.equity());
                                this.setBodyMap(15, liabEqui.totalEquity());
                                this.setBodyMap(16, this.totalLiabilitiesEquity());
                            }
                        } else if (!liabEqui.isEquityEmpty()) {
                            this.setBodyMap(9, liabEqui.equityTitle());
                            this.setBodyMap(10, liabEqui.equity());
                            this.setBodyMap(11, liabEqui.totalEquity());
                            this.setBodyMap(12, this.totalLiabilitiesEquity());
                        }

                    } else if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                        this.setBodyMap(6, liabEqui.nonCurrentLiabilitiesTitle());
                        this.setBodyMap(7, liabEqui.nonCurrentLiabilities());
                        this.setBodyMap(8, liabEqui.totalNonCurrentLiabilities());
                        this.setBodyMap(9, this.totalLiabilities());

                        if (!liabEqui.isEquityEmpty()) {
                            this.setBodyMap(10, liabEqui.equityTitle());
                            this.setBodyMap(11, liabEqui.equity());
                            this.setBodyMap(12, liabEqui.totalEquity());
                            this.setBodyMap(13, this.totalLiabilitiesEquity());
                        }

                    } else if (!liabEqui.isEquityEmpty()) {

                        this.setBodyMap(6, liabEqui.equityTitle());
                        this.setBodyMap(7, liabEqui.equity());
                        this.setBodyMap(8, liabEqui.totalEquity());
                        this.setBodyMap(9, this.totalLiabilitiesEquity());

                    }
                }
            }

        } else if (!liabEqui.isLiabilitiesEmpty()) {

            this.setBodyMap(0, liabEqui.mainLiabilitiesEquityTitle());

            if (!liabEqui.isCurrentLiabilityEmpty()) {
                this.setBodyMap(1, liabEqui.currentLiabilitiesTitle());
                this.setBodyMap(2, liabEqui.currentLiabilities());
                this.setBodyMap(3, liabEqui.totalCurrentLiabilities());

                if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                    this.setBodyMap(4, liabEqui.nonCurrentLiabilitiesTitle());
                    this.setBodyMap(5, liabEqui.nonCurrentLiabilities());
                    this.setBodyMap(6, liabEqui.totalNonCurrentLiabilities());

                    this.setBodyMap(7, this.totalLiabilities());

                    if (!liabEqui.isEquityEmpty()) {

                        this.setBodyMap(8, liabEqui.equityTitle());
                        this.setBodyMap(9, liabEqui.equity());
                        this.setBodyMap(10, liabEqui.totalEquity());
                        this.setBodyMap(11, this.totalLiabilitiesEquity());
                    }

                } else if (!liabEqui.isEquityEmpty()) {

                    this.setBodyMap(4, liabEqui.equityTitle());
                    this.setBodyMap(5, liabEqui.equity());
                    this.setBodyMap(6, liabEqui.totalEquity());
                    this.setBodyMap(7, this.totalLiabilitiesEquity());
                }
            } else if (!liabEqui.isNonCurrentLiabilityEmpty()) {

                this.setBodyMap(1, liabEqui.nonCurrentLiabilitiesTitle());
                this.setBodyMap(2, liabEqui.nonCurrentLiabilities());
                this.setBodyMap(3, liabEqui.totalNonCurrentLiabilities());

                this.setBodyMap(4, this.totalLiabilities());

                if (!liabEqui.isEquityEmpty()) {

                    this.setBodyMap(5, liabEqui.equityTitle());
                    this.setBodyMap(6, liabEqui.equity());
                    this.setBodyMap(7, liabEqui.totalEquity());
                    this.setBodyMap(8, this.totalLiabilitiesEquity());
                }

            } else if (!liabEqui.isEquityEmpty()) {
            }
            this.setBodyMap(1, liabEqui.equityTitle());
            this.setBodyMap(2, liabEqui.equity());
            this.setBodyMap(3, liabEqui.totalEquity());
            this.setBodyMap(4, this.totalLiabilitiesEquity());

        } else {

            bs = false;
        }

        model1 = new ReportsModelData(getBodyMap(), getTableHeaders());
        table.setModel(model1);

        TableRowSorter<ReportsModelData> sorter = new TableRowSorter<>(model1);
        table.setRowSorter(sorter);

        return bs;
    }

    private List totalAssets() {

    }

    private List totalLiabilities() {

    }

    private List totalEquity() {

    }

    private List totalLiabilitiesEquity() {

    }

    @Override
    public String getTitle(String date) {
        return title;
    }

    @Override
    public List<String> getTableHeaders() {
        return tableHeaders;
    }

    @Override
    public List<Object> getBodyList() {
        return dataBody;
    }

    @Override
    public Map<Integer, List<Object>> getBodyMap() {
        return data;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setTableHeader(String header) {
        tableHeaders.add(header);
    }

    @Override
    public void setBodyList(String body) {
        dataBody.add(body);
    }

    @Override
    public void setBodyMap(Integer index, List<Object> body) {
        data.put(index, body);
    }

}

我怎样才能更好地实现这个类?

2 个答案:

答案 0 :(得分:4)

你强迫自己使用所有这些嵌套语句,因为冗余的索引键会不断改变位置。使用列表而不是地图应解决大部分问题:

private List<List<Object>> data = new ArrayList<>();

public boolean createBalanceSheet(JTable table, String materialDate) {
    //...
    if (!asset.isAssetsEmpty()) {
        data.add(asset.mainAssetTitle());
    }

    if (!asset.isCurrentAssetsEmpty()) {
        data.add(asset.currentAssetTitle());
        data.add(asset.currentAssets());
        data.add(asset.totalCurrentAssets());
    }

    if (!asset.isNonCurrentAssetsEmpty()) {
        data.add(asset.nonCurrentAssetTitle());
        data.add(asset.nonCurrentAssets());
        data.add(asset.totalNonCurrentAssets());
        data.add(this.totalAssets());
    }

    // and so on...
}

如果您绝对必须返回Map<Integer, List<Object>>,您可以轻松转换列表以生成相同内容:

@Override
public Map<Integer, List<Object>> getBodyMap() {
    Map<Integer, List<Object>> result = new HashMap<>();
    for (int i = 0; i < data.size(); i++) {
        result.put(i, data.get(i));
    }
    return result;
}

答案 1 :(得分:3)

典型的答案是:查看Single Responsbility Principle和多态。另一个在这里发挥作用的规则是:Tell don't ask

含义:您的代码要求其他对象提供一些内部状态信息。基于该信息,该代码可以做出决策。 &#34;更多OO&#34;方法是:只是告诉其他对象自己做某事。它最了解其内部状态。

除此之外;您的问题无法轻易解决。你必须先看看&#34;整体&#34;事情。基本上你有一些&#34;输入数据&#34;和一套规则&#34;确定如何构建&#34;输出&#34;从那个输入。一个真正的解决方案是建模这些元素(输入,输出,规则),并允许您在表格中表达所需的关系&#34;例如。