我正在实现一个类,该类扫描数据库中的数据,并根据数据库中的内容在表中显示数据。但是,为了满足数据库中所有可能的返回值并受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);
    }

}


我怎样才能更好地实施这一节课?

最佳答案

典型的答案是:观察Single Responsbility Principle和多态性。在此起作用的另一条规则:Tell don't ask

含义:您的代码正在要求其他对象提供一些内部状态信息。基于这些信息,该代码可以做出决策。 “更多面向对象”方法是:告诉其他对象自己做某事。它最了解其内部状态。

除此之外;您的问题无法轻易解决。您必须迈出一步,看看“整个”事情。基本上,您有一些“输入数据”和一组“规则”来确定如何从该输入构建“输出”。真正的解决方案将对这些元素(输入,输出,规则)进行建模,并允许您以“表格”形式表达所需的关系。

10-08 19:07