当前位置: 首页> 文旅> 酒店 > 存取款系统接口设计

存取款系统接口设计

时间:2025/7/9 1:49:21来源:https://blog.csdn.net/zjshuster/article/details/140358961 浏览次数:0次

题目描述:

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: