enum枚举应用记录

昨天写的条件匹配被吐槽太low,直接在serviceImpl实现类里面加了写死的中文条件匹配,应该用enum枚举,这样的代码更高效简洁。

贴上前辈改的条件查询,里面的priceList.stream().collect(Collectors.groupingBy(x -> x.getLoadStandard()));不太懂,会有关于这一块详细的学习记录。

serviceImpl实现类相关代码块实现

List<String> loadStandList = new ArrayList<>();
                loadStandList.add(LoadStandardEnum.USUAL.getCode()); // 标准
                loadStandList.add(LoadStandardEnum.UNUSUAL_SMALL.getCode()); // 非标小
                loadStandList.add(LoadStandardEnum.UNUSUAL_LARGE.getCode()); // 非标大

                for (int i = 0; i < len; i++) {
                    bid = bidList.get(i);
                    // 报价
                    String price = null;
                    String totalTrainNum = null;
                    String offerCapacity = null;
                    List<TBidModel> priceList = bid.gettBidModelsThree();
                    Map<String, List<TBidModel>> priceMap = priceList
                            .stream()
                            .collect(Collectors.groupingBy(x -> x.getLoadStandard()));

                    String tempStr1 = "";
                    String tempStr2 = "";
                    String tempStr3 = "";
                    String tempStr4 = "";

                    for(String loadStand: loadStandList) {
                        List<TBidModel> list = priceMap.get(loadStand);
                        if (EmptyUtil.isEmpty(list)) {
                            tempStr1 += "N/";
                            tempStr2 += "N/";
                            tempStr3 += "N/";
                            tempStr4 += "N/";
                        } else {
                            TBidModel tbm = list.get(0);
                            if (hiredType == hiredYesCode) {
                                price = settleType == taxYesCode ? tbm.getOfferHiredPriceTax(): tbm.getOfferHiredPriceTaxNo();
                            } else {
                                price = settleType == taxYesCode ? tbm.getOfferPriceTax(): tbm.getOfferPriceTaxNo();
                            }

                            totalTrainNum = tbm.getTotalTrainNum();
                            offerCapacity = tbm.getCapacity();

                            tempStr1 += price + "/";
                            tempStr2 += offerCapacity + "/";
                            tempStr3 += totalTrainNum + "/";

                            if (DecimalUtil.isNumeric(offerCapacity) && DecimalUtil.isNumeric(totalTrainNum)) {
                                tempStr4 +=  DecimalUtil.divide(offerCapacity, totalTrainNum, 2) + "/";
                            } else {
                                tempStr4 += "N/";
                            }
                        }
                    }

                    tempStr1 = tempStr1.substring(0, tempStr1.length() - 1);
                    tempStr2 = tempStr2.substring(0, tempStr2.length() - 1);
                    tempStr3 = tempStr3.substring(0, tempStr3.length() - 1);
                    tempStr4 = tempStr4.substring(0, tempStr4.length() - 1);

                    row.put("bidderName" + i , bid.getBidderCompanyName());
                    row.put("offerPrice" + i, tempStr1);
                    row.put("offerCapacity" + i, tempStr2);
                    row.put("totalTrainNum" + i, tempStr3);
                    row.put("vehicleCapacity" + i, tempStr4);
                }

enum类的实现

public enum LoadStandardEnum {
    USUAL("10", "标准"),
    UNUSUAL_SMALL("20", "非标小"),
    UNUSUAL_LARGE("30", "非标大");

    private String code;
    private String value;

    LoadStandardEnum(String code, String value) {
        this.code = code;
        this.value = value;
    }

    public static String getValue(String code) {
        for (LoadStandardEnum statusEnum : LoadStandardEnum.values()) {
            if (statusEnum.code.equals(code)) {
                return statusEnum.value;
            }
        }
        return null;
    }

    public String getCode() {
        return code;
    }

    public String getValue() {
        return value;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值