蚂蚁开放性笔试题--最短时间的支付方式组合

本文介绍了一种通过缓存和并发处理来优化支付方式可用性检查的方法,利用多线程和缓存技术减少响应时间。

题目

用户有多种支付方式(余额、红包、优惠券,代金券等),假如每种支付方式通过调用远程服务获取可用性。
在外部资源环境不变情况下,请设计程序以最短响应时间获得尽可能多的可用支付方式列表。

假定支付方式可用性咨询接口定义:PaymentRemoteSerivce
接口方法:ConsultResult isEnabled(String paymentType);
返回结果:

public class ConsultResult {

public ConsultResult (boolean isEnable,String  errorCode){
    this.isEnable = isEnable;
    this.errorCode= errorCode;
}

/** 咨询结果是否可用*/
private boolean isEnable;

/** 错误码 */
private String errorCode;

public boolean getIsEnable(){
    return isEnable;
}

public String getErrorCode(){
    return errorCode;
}
}

解题思路

1、进行远程调用
2、远程调用后存缓存
3、发生变化通过socket或者mq进行通知,刷新缓存
4、定时刷新缓存、防止某时刻大量请求过来而没得缓存

开干

0、定义支付方式常量

package payWay.other;


/**
 * 支付方式常量
 * 余额、红包、优惠券,代金券等
 * @author sirwsl
 */
public class Constants {
   
   
    /**
     * 余额
     */
    public  static final String BALANCE = "10";

    /**
     * 红包
     */
    public static final String RED_ENVELOPE = "20";

    /**
     * 优惠券
     */
    public static final String COUPONS = "30";

    /**
     * 代金券
     */
    public static final String VOUCHERS = "40";

    /**
     * 其他
     */
    public static final String OTHER = "50";

}

1、定义支付方式枚举值

package payWay.other;


/**
 * 余额、红包、优惠券,代金券等
 * @author sirwsl
 */

public enum PayWayEnum {
   
   

    //性别枚举
    BALANCE("余额", Constants.BALANCE),
    RED_ENVELOPE("红包", Constants.RED_ENVELOPE),
    COUPONS("优惠券",Constants.COUPONS),
    VOUCHERS("代金券",Constants.VOUCHERS),
    OTHER("其他",Constants.VOUCHERS);

    private String name;

    private String value;

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

    public static PayWayEnum matchByValue(String value) {
   
   
        for (PayWayEnum item : PayWayEnum.values()) {
   
   
            if (item.value.equals(value)) {
   
   
                return item;
            }
        }
        return OTHER;
    }

    public static PayWayEnum matchByName(String name) {
   
   
        for (PayWayEnum item : PayWayEnum.values()) {
   
   
            if (item.name.equals(name)) {
   
   
                return item;
            }
        }
        return OTHER;
    }

    public String getName() {
   
   
        return name;
    }

    public void setName(String name) {
   
   
        this.name = name;
    
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sirwsl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值