java -通过bean中的某个元素信息获得实体全部信息

业务需求:

每一个省份都有省份编码、省份号、省份名三个信息,省份的所有信息都是已知并且固定的。通过某一个元素获得整个省份的所有信息。

业务代码:

package com.cyd.context;

/**
 * 省份信息实体类
 */
public class ProvinceInfo {

    private String provinceCode; // 省份编码

    private Integer provinceNumber; // 省份号

    private String provinceName; // 省份名

    private static final String EXCEPTION_INFORMATION = "省份信息不存在";

    private ProvinceInfo(String provinceCode, Integer provinceNumber, String provinceName) {
        this.provinceCode = provinceCode;
        this.provinceNumber = provinceNumber;
        this.provinceName = provinceName;
    }

    /**
     * 通过省份编码获得省份所有信息
     */
    public static ProvinceInfo ofProvinceCode(String provinceCode) {

        switch (provinceCode) {
            case "LN":
                return liaoNing();
            case "ZJ":
                return zheJiang();
            case "SH":
                return shangHai();
            default:
                throw new UnsupportedOperationException(EXCEPTION_INFORMATION);
        }
    }


    /**
     * 通过省份号获得省份所有信息
     */
    public static ProvinceInfo ofProvinceNumber(String provinceNumber) {

        switch (provinceNumber) {
            case "66":
                return liaoNing();
            case "22":
                return zheJiang();
            case "33":
                return shangHai();
            default:
                throw new UnsupportedOperationException(EXCEPTION_INFORMATION);
        }
    }


    /**
     * 通过省份号获得省份所有信息
     */
    public static ProvinceInfo ofProvinceNumber(int provinceNumber) {

        switch (provinceNumber) {
            case 66:
                return liaoNing();
            case 22:
                return zheJiang();
            case 33:
                return shangHai();
            default:
                throw new UnsupportedOperationException(EXCEPTION_INFORMATION);
        }
    }


    private static ProvinceInfo liaoNing(){
        return new ProvinceInfo("LN",66,"辽宁");
    }

    private static ProvinceInfo zheJiang(){
        return new ProvinceInfo("ZJ",22,"浙江");
    }

    private static ProvinceInfo shangHai(){
        return new ProvinceInfo("SH",33,"上海");
    }

    @Override
    public String toString() {
        return "ProvinceInfo{" +
                "provinceCode='" + provinceCode + '\'' +
                ", provinceNumber=" + provinceNumber +
                ", provinceName='" + provinceName + '\'' +
                '}';
    }
}

你可能感兴趣的:(java,开发语言)