优化这段Java代码:if (("S".equals(dept) && "Y".equals(bagInfo.getBagSOpen()) && !"Y".equals(bagInfo.getBagSFinished())) || ("C".equals(dept) && "Y".equals(bagInfo.getBagCOpen()) && !"Y".equals(bagInfo.getBagCFinished())) || ("G".equals(dept) && "Y".equals(bagInfo.getBagGOpen()) && !"Y".equals(bagInfo.getBagGFinished()))) { record.setBagState("0"); record.setBagDescribe("需开包"); } else if (("S".equals(dept) && "Y".equals(bagInfo.getBagSOpen()) && "Y".equals(bagInfo.getBagSFinished())) || ("C".equals(dept) && "Y".equals(bagInfo.getBagCOpen()) && "Y".equals(bagInfo.getBagCFinished())) || ("G".equals(dept) && "Y".equals(bagInfo.getBagGOpen()) && "Y".equals(bagInfo.getBagGFinished()))) { record.setBagState("0"); record.setBagDescribe("开包完成"); } else if ((("S".equals(dept) && "N".equals(bagInfo.getBagSOpen()) && "N".equals(bagInfo.getBagSFinished())) || ("C".equals(dept) && "N".equals(bagInfo.getBagCOpen()) && "N".equals(bagInfo.getBagCFinished())) || ("G".equals(dept) && "N".equals(bagInfo.getBagGOpen()) && "N".equals(bagInfo.getBagGFinished())))) { record.setBagState("0"); record.setBagDescribe("正常"); } else { record.setBagState("0"); record.setBagDescribe("未知"); }
时间: 2023-12-18 11:02:04 浏览: 171
可以将代码中的重复部分提取出来,使用一个方法来判断是否符合条件并设置 record 的值。具体实现如下:
```
private void setRecordBagState(String dept, BagInfo bagInfo, Record record) {
if (("Y".equals(getBagOpen(dept, bagInfo)) && !"Y".equals(getBagFinished(dept, bagInfo)))) {
record.setBagState("0");
record.setBagDescribe("需开包");
} else if (("Y".equals(getBagOpen(dept, bagInfo)) && "Y".equals(getBagFinished(dept, bagInfo)))) {
record.setBagState("0");
record.setBagDescribe("开包完成");
} else if (("N".equals(getBagOpen(dept, bagInfo)) && "N".equals(getBagFinished(dept, bagInfo)))) {
record.setBagState("0");
record.setBagDescribe("正常");
} else {
record.setBagState("0");
record.setBagDescribe("未知");
}
}
private String getBagOpen(String dept, BagInfo bagInfo) {
switch (dept) {
case "S":
return bagInfo.getBagSOpen();
case "C":
return bagInfo.getBagCOpen();
case "G":
return bagInfo.getBagGOpen();
default:
return "";
}
}
private String getBagFinished(String dept, BagInfo bagInfo) {
switch (dept) {
case "S":
return bagInfo.getBagSFinished();
case "C":
return bagInfo.getBagCFinished();
case "G":
return bagInfo.getBagGFinished();
default:
return "";
}
}
```
然后在原代码中调用 `setRecordBagState` 方法即可:
```
setRecordBagState(dept, bagInfo, record);
```
阅读全文