存取款系统接口设计

题目描述:

设计一个存取款接口,入参是账户数组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;
    }

相关推荐

  1. 取款系统接口设计

    2024-07-12 00:48:03       20 阅读
  2. 怎样值以及取值

    2024-07-12 00:48:03       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 00:48:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 00:48:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 00:48:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 00:48:03       69 阅读

热门阅读

  1. SpringBoot 自定义异常返回数据格式

    2024-07-12 00:48:03       21 阅读
  2. ubuntu 安装cups和爱普生打印机

    2024-07-12 00:48:03       19 阅读
  3. 服务器怎么进PE系统?

    2024-07-12 00:48:03       25 阅读
  4. 还在代码中写HttpUtil?是时候说再见啦

    2024-07-12 00:48:03       24 阅读
  5. selenium常用方法

    2024-07-12 00:48:03       24 阅读
  6. (九)Docker 的网络通信

    2024-07-12 00:48:03       22 阅读
  7. 【力扣】每日一题—第242题,有效的字母异位词

    2024-07-12 00:48:03       20 阅读
  8. Go单元测试

    2024-07-12 00:48:03       23 阅读
  9. C# IOC容器、依赖注入和控制反转

    2024-07-12 00:48:03       24 阅读
  10. 使用C++编写TCP服务端程序

    2024-07-12 00:48:03       24 阅读
  11. 什么是AQS

    2024-07-12 00:48:03       20 阅读