已优化点:
info、code需要注释
方法调用不得违反得墨忒耳定律
方法应该只有一个退出口
匹配到结果后立即退出循环
public enum DyProductType {
/**
* 团购类型
*/
GROUPON("1","团购"),
/**
* 代金券类型
*/
VOUCHER("2","代金券"),
/**
* 次卡类型
*/
TIMES_CARD("3","次卡"),
/**
* 储值卡类型
*/
STORE_MONEY_CARD("4","储值卡"),
/**
* 周期卡类型
*/
PERIOD_CARD("5","周期卡"),
/**
* 预约品类型
*/
APPOINTMENT("6","预约品"),
/**
* 提货券类型
*/
RETAIL_PICK_UP("7","提货券");
/**
* @param 枚举编码
*/
private String code;
/**
* @param 枚举信息
*/
private String info;
DyProductType(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
/**
* @description: 根据枚举信息获取枚举编码
* @author: ***
* @date: 2025/3/10 9:22
* @param: [info]
* @return: java.lang.String
**/
public static String getCodeByInfo(String info){
String result = null;
for (DyProductType type : DyProductType.values()){
if (info.equals(type.info)){
result = type.getCode();
break;
}
}
return result;
}
/**
* @description: 根据枚举编码获取枚举信息
* @author: ***
* @date: 2025/3/10 9:22
* @param: [info]
* @return: java.lang.String
**/
public static String getInfoByCode(String code){
String result = null;
for (DyProductType type : DyProductType.values()){
if (code.equals(type.code)){
result = type.getInfo();
break;
}
}
return result;
}
}