私は、データベースをスキャンしてデータを取得するクラスを実装しており、データベースの内容に応じてテーブルにデータを表示します。しかし、データベースからの可能な復帰をすべて満たすために、JTable
がどのように情報を表示するかによって制約を受けるため、エラーが発生する可能性のある多くのif else
ステートメントを自己書き込みにします。多くのif文を避ける方法
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);
}
}
このクラスをよりうまく実装できましたか?
Iドンそれらの間にコードの巨大なブロックがあるので、他にも多くのif - elseが見えます。 [mcve]を投稿してください。押し込みも役に立たない。 – AxelH
コードをインデントしてください!コードがあなたのように乱れているときに何が起こっているのかを理解することは非常に難しいです! –
[デザインパターン](https://en.wikipedia.org/wiki/Design_Patterns)について読むことを強くお勧めします。 –