Commit 1520874d by 张富康

省份枚举

1 parent 2e587ead
......@@ -68,6 +68,34 @@ public enum ProvinceEnum {
throw new IllegalArgumentException("未知的省份名称: " + name);
}
/**
* 根据行政区划代码获取省份名称
* @param code 6位数字编码(如:110000)
* @return 省份名称,若未找到返回 null
*/
public static String getProvinceName(String code) {
try {
ProvinceEnum province = ProvinceEnum.fromCode(code);
return province.getName();
} catch (IllegalArgumentException e) {
return null; // 或者抛出异常或返回默认值
}
}
/**
* 安全地获取省份名称(不抛异常)
* @param code 行政区划代码
* @return 省份名称,不存在则返回空字符串
*/
public static String getProvinceNameSafe(String code) {
try {
ProvinceEnum province = ProvinceEnum.fromCode(code);
return province.getName();
} catch (Exception e) {
return "";
}
}
@Override
public String toString() {
return name + " (" + code + ")";
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!