|
| 1 | +/* |
| 2 | +
|
| 3 | +2043. Simple Bank System |
| 4 | +
|
| 5 | +You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered from 1 to n. The initial balance of each account is stored in a 0-indexed integer array balance, with the (i + 1)th account having an initial balance of balance[i]. |
| 6 | +
|
| 7 | +Execute all the valid transactions. A transaction is valid if: |
| 8 | +
|
| 9 | +The given account number(s) are between 1 and n, and |
| 10 | +The amount of money withdrawn or transferred from is less than or equal to the balance of the account. |
| 11 | +Implement the Bank class: |
| 12 | +
|
| 13 | +Bank(long[] balance) Initializes the object with the 0-indexed integer array balance. |
| 14 | +boolean transfer(int account1, int account2, long money) Transfers money dollars from the account numbered account1 to the account numbered account2. Return true if the transaction was successful, false otherwise. |
| 15 | +boolean deposit(int account, long money) Deposit money dollars into the account numbered account. Return true if the transaction was successful, false otherwise. |
| 16 | +boolean withdraw(int account, long money) Withdraw money dollars from the account numbered account. Return true if the transaction was successful, false otherwise. |
| 17 | + |
| 18 | +
|
| 19 | +Example 1: |
| 20 | +
|
| 21 | +Input |
| 22 | +["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"] |
| 23 | +[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]] |
| 24 | +Output |
| 25 | +[null, true, true, true, false, false] |
| 26 | +
|
| 27 | +Explanation |
| 28 | +Bank bank = new Bank([10, 100, 20, 50, 30]); |
| 29 | +bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10. |
| 30 | + // Account 3 has $20 - $10 = $10. |
| 31 | +bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20. |
| 32 | + // Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30. |
| 33 | +bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5. |
| 34 | + // Account 5 has $10 + $20 = $30. |
| 35 | +bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10, |
| 36 | + // so it is invalid to transfer $15 from it. |
| 37 | +bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist. |
| 38 | + |
| 39 | +
|
| 40 | +Constraints: |
| 41 | +n == balance.length |
| 42 | +1 <= n, account, account1, account2 <= 105 |
| 43 | +0 <= balance[i], money <= 1012 |
| 44 | +At most 104 calls will be made to each function transfer, deposit, withdraw. |
| 45 | +
|
| 46 | +*/ |
| 47 | + |
| 48 | +class Bank { |
| 49 | + constructor(balances) { |
| 50 | + this.balances = balances; |
| 51 | + } |
| 52 | + |
| 53 | + _accountHasInsufficientFunds = (account, minBalance) => this._getBalance(account) < minBalance; |
| 54 | + _creditAccount = (account, amount) => (this.balances[account - 1] += amount); |
| 55 | + _debitAccount = (account, amount) => (this.balances[account - 1] -= amount); |
| 56 | + _getBalance = (account) => this.balances[account - 1]; |
| 57 | + _isInvalidAccount = (account) => account <= 0 || account > this.balances.length; |
| 58 | + |
| 59 | + transfer = (fromAccount, toAccount, amount) => { |
| 60 | + if (this._isInvalidAccount(fromAccount)) return false; |
| 61 | + if (this._isInvalidAccount(toAccount)) return false; |
| 62 | + if (this._accountHasInsufficientFunds(fromAccount, amount)) return false; |
| 63 | + this._debitAccount(fromAccount, amount); |
| 64 | + this._creditAccount(toAccount, amount); |
| 65 | + return true; |
| 66 | + }; |
| 67 | + |
| 68 | + deposit = (account, amount) => { |
| 69 | + if (this._isInvalidAccount(account)) return false; |
| 70 | + this._creditAccount(account, amount); |
| 71 | + return true; |
| 72 | + }; |
| 73 | + |
| 74 | + withdraw = (account, amount) => { |
| 75 | + if (this._isInvalidAccount(account)) return false; |
| 76 | + if (this._accountHasInsufficientFunds(account, amount)) return false; |
| 77 | + this._debitAccount(account, amount); |
| 78 | + return true; |
| 79 | + }; |
| 80 | +} |
0 commit comments