大厂鄙视题,如下(鄙人斗胆一写,如有误导欢迎沟通指正)
话不多说,直接上代码吧
封装枚举
interface CustomerType {
// 获取折扣
Double getDiscount();
// 根据 用户类型code获取 实例
static CustomerType getDiscountBy(String code, CustomerType[] values) {
for (CustomerType value : values) {
if(value.getCode().equals(code))
return value;
}
throw new IllegalArgumentException("参数不合法");
}
String getCode();
}
enum ECustomerType_A implements CustomerType {
SUPER_VIP("超级VIP用户", "A001", 0.7),
VIP("VIP用户", "A002", 0.9),
NORMAL("普通用户", "A003", 1.0),
;
private String desc;
private String code;
private Double discount;
ECustomerType_A(String desc, String code, Double discount) {
this.desc = desc;
this.code = code;
this.discount = discount;
}
public String getDesc() {
return desc;
}
@Override
public String getCode() {
return code;
}
@Override
public Double getDiscount() {
return discount;
}
}
enum ECustomerType_B implements CustomerType {
JP("金牌", "B001", 0.65),
YP("银牌", "B002", 0.75),
TP("铜牌用户", "B003", 0.85),
NORMAL("普通用户", "B004", 1.0),
;
private String desc;
private String code;
private Double discount;
ECustomerType_B(String desc, String code, Double discount) {
this.desc = desc;
this.code = code;
this.discount = discount;
}
public String getDesc() {
return desc;
}
@Override
public String getCode() {
return code;
}
@Override
public Double getDiscount() {
return discount;
}
}
enum ECustomerType_C implements CustomerType {
HGHY("皇冠", "C001", 0.8),
NORMAL("普通用户", "C002", 1.0),
;
private String desc;
private String code;
private Double discount;
ECustomerType_C(String desc, String code, Double discount) {
this.desc = desc;
this.code = code;
this.discount = discount;
}
public String getDesc() {
return desc;
}
@Override
public String getCode() {
return code;
}
@Override
public Double getDiscount() {
return discount;
}
}
定义公司接口及编写具体实现
public interface Company {
Double getDiscount(String code);
}
class CompanyA implements Company {
@Override
public Double getDiscount(String code) {
CustomerType discountBy = CustomerType.getDiscountBy(code, ECustomerType_A.values());
Double discount = discountBy.getDiscount();
return discount;
}
}
class CompanyB implements Company {
@Override
public Double getDiscount(String code) {
CustomerType discountBy = CustomerType.getDiscountBy(code, ECustomerType_B.values());
Double discount = discountBy.getDiscount();
return discount;
}
}
class CompanyC implements Company {
@Override
public Double getDiscount(String code) {
CustomerType discountBy = CustomerType.getDiscountBy(code, ECustomerType_C.values());
Double discount = discountBy.getDiscount();
return discount;
}
}
业务层具体使用代码
// 业务方直接使用的接口
interface CompanyPrice {
// 根据公司类型、用户类型、原价格 获取 现在价格
Double get(String companyName, String userType, Double price);
}
class CompanyPriceImpl implements CompanyPrice {
private static HashMap companyHashMap = new HashMap<>();
static {
companyHashMap.put("A",new CompanyA());
companyHashMap.put("B",new CompanyB());
companyHashMap.put("C",new CompanyC());
}
// 参数说明:公司类型(A、B、C)、用户类型code、价格
// 公司类型、用户类型code可以进一步用枚举规范,略...
@Override
public Double get(String companyName, String userTypeCode, Double price) {
Company company = companyHashMap.get(companyName);
if(company == null){
throw new IllegalArgumentException("参数不合法");
}
Double discount = company.getDiscount(userTypeCode);
BigDecimal oldPrice = new BigDecimal(price.toString());
BigDecimal discountBigD = new BigDecimal(discount.toString());
BigDecimal multiply = oldPrice.multiply(discountBigD);
Double newPrice = multiply.doubleValue();
return newPrice;
}
}