Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc303b0

Browse files
authoredOct 17, 2021
Create 2043-simple-bank-system.js
1 parent 8344680 commit fc303b0

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
 

‎2043-simple-bank-system.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @param {number[]} balance
3+
*/
4+
const Bank = function(balance) {
5+
this.n = balance.length
6+
balance.unshift(0)
7+
this.b = balance
8+
9+
};
10+
11+
/**
12+
* @param {number} account1
13+
* @param {number} account2
14+
* @param {number} money
15+
* @return {boolean}
16+
*/
17+
Bank.prototype.transfer = function(account1, account2, money) {
18+
let res = true
19+
if(account1 > this.n || account1 < 1) return false
20+
if(account2 > this.n || account2 < 1) return false
21+
if(this.b[account1]< money) return false
22+
this.b[account1] -= money
23+
this.b[account2] += money
24+
return true
25+
};
26+
27+
/**
28+
* @param {number} account
29+
* @param {number} money
30+
* @return {boolean}
31+
*/
32+
Bank.prototype.deposit = function(account, money) {
33+
if(account > this.n || account < 1) return false
34+
this.b[account] += money
35+
return true
36+
};
37+
38+
/**
39+
* @param {number} account
40+
* @param {number} money
41+
* @return {boolean}
42+
*/
43+
Bank.prototype.withdraw = function(account, money) {
44+
if(account > this.n || account < 1) return false
45+
if(this.b[account] < money) return false
46+
this.b[account] -= money
47+
return true
48+
};
49+
50+
/**
51+
* Your Bank object will be instantiated and called as such:
52+
* var obj = new Bank(balance)
53+
* var param_1 = obj.transfer(account1,account2,money)
54+
* var param_2 = obj.deposit(account,money)
55+
* var param_3 = obj.withdraw(account,money)
56+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.