存取款系统接口设计

题目描述:

设计一个存取款接口,入参是账户数组balances 与存取款请求体数组requests
对于取款要求判断:
当前余额不足,返回余额不足帐号
之前的取款时间在24之前的,在24小时之后返回上次取款额度的百分之2并向下取整。

example1:
String[] requests = {“withdraw 1613327630 2 480”,
“withdraw 1613327644 2 800”,
“withdraw 1614105244 1 100”,
“deposit 1614108844 2 200”,
“withdraw 1614108845 2 150”};
int[] balances = {1000, 1500};
返回: {900, 295}

example2:
String[] requests = {“withdraw 1613327630 2 480”,
“withdraw 1613327644 2 800”,
“withdraw 1614105244 1 1000”,
“deposit 1614108844 2 200”,
“withdraw 1614108845 2 150”};
int[] balances = {1000, 500};
返回 {-1}
注明:1号账户余额不足

思路

we can use an array preWithdrawNum to present the last withdraw account nums, each time we deal with the current request, we can judge whether the current time bigger than the last withdraw request 's time, if so, we can deal with the cashback logic of giving 2 percent of the last withdraw amount

 static int[] solution(int[] balances, String[] requests) {
        int[] preWithdrawNum = new int[requests.length];
        Arrays.fill(preWithdrawNum, -1);
        for (int i = 0; i < requests.length; i++) {
            String req = requests[i];
            String[] items = req.split(" ");
            String func = items[0];
            Integer num = Integer.valueOf(items[2]) - 1, amount = Integer.valueOf(items[3]);
            long time = Long.valueOf(items[1]);
            if (items.length != 4) {
                return new int[]{-num};
            }
            for (int j = 0; j < preWithdrawNum.length; j++) {
                if (preWithdrawNum[j] != -1 ) {
                    String[] lastItems = requests[j].split(" ");
                    long lastTimePlus24h = Long.valueOf(lastItems[1]) + 24 * 60 * 60;
                    Integer currentAccountN = preWithdrawNum[j] ;
                    if (time > lastTimePlus24h ) {
                        balances[currentAccountN] += Math.floor(Integer.valueOf(lastItems[3]) * 0.02);
                        preWithdrawNum[j] = -1;
                    }
                }
            }

            if ("withdraw".equals(func)) {
                if (balances[num] < amount) {
                    return new int[]{-i};
                }
                balances[num] -= amount;
                preWithdrawNum[i] = num;
            }
            if ("deposit".equals(func)) {
                balances[num] += amount;
            }
        }
        return balances;
    }

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值