java.lang.Object
g2001_2100.s2043_simple_bank_system.Bank

public class Bank extends Object
2043 - Simple Bank System.<p>Medium</p> <p>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 <code>n</code> accounts numbered from <code>1</code> to <code>n</code>. The initial balance of each account is stored in a <strong>0-indexed</strong> integer array <code>balance</code>, with the <code>(i + 1)<sup>th</sup></code> account having an initial balance of <code>balance[i]</code>.</p> <p>Execute all the <strong>valid</strong> transactions. A transaction is <strong>valid</strong> if:</p> <ul> <li>The given account number(s) are between <code>1</code> and <code>n</code>, and</li> <li>The amount of money withdrawn or transferred from is <strong>less than or equal</strong> to the balance of the account.</li> </ul> <p>Implement the <code>Bank</code> class:</p> <ul> <li><code>Bank(long[] balance)</code> Initializes the object with the <strong>0-indexed</strong> integer array <code>balance</code>.</li> <li><code>boolean transfer(int account1, int account2, long money)</code> Transfers <code>money</code> dollars from the account numbered <code>account1</code> to the account numbered <code>account2</code>. Return <code>true</code> if the transaction was successful, <code>false</code> otherwise.</li> <li><code>boolean deposit(int account, long money)</code> Deposit <code>money</code> dollars into the account numbered <code>account</code>. Return <code>true</code> if the transaction was successful, <code>false</code> otherwise.</li> <li><code>boolean withdraw(int account, long money)</code> Withdraw <code>money</code> dollars from the account numbered <code>account</code>. Return <code>true</code> if the transaction was successful, <code>false</code> otherwise.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong> [&ldquo;Bank&rdquo;, &ldquo;withdraw&rdquo;, &ldquo;transfer&rdquo;, &ldquo;deposit&rdquo;, &ldquo;transfer&rdquo;, &ldquo;withdraw&rdquo;] [<a href="10,-100,-20,-50,-30">10, 100, 20, 50, 30</a>, [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]</p> <p><strong>Output:</strong> [null, true, true, true, false, false]</p> <p><strong>Explanation:</strong></p> <p>Bank bank = new Bank([10, 100, 20, 50, 30]);</p> <p>bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10. // Account 3 has $20 - $10 = $10.</p> <p>bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20. // Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.</p> <p>bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5. // Account 5 has $10 + $20 = $30.</p> <p>bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10, // so it is invalid to transfer $15 from it.</p> <p>bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == balance.length</code></li> <li><code>1 <= n, account, account1, account2 <= 10<sup>5</sup></code></li> <li><code>0 <= balance[i], money <= 10<sup>12</sup></code></li> <li>At most <code>10<sup>4</sup></code> calls will be made to <strong>each</strong> function <code>transfer</code>, <code>deposit</code>, <code>withdraw</code>.</li> </ul>
  • Constructor Details

    • Bank

      public Bank(long[] balance)
  • Method Details

    • transfer

      public boolean transfer(int account1, int account2, long money)
    • deposit

      public boolean deposit(int account, long money)
    • withdraw

      public boolean withdraw(int account, long money)