|
| 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