Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

preface

Today’s topic is medium, but I feel it is not as simple as a meaningless topic… Simple problems also need to find the corresponding ideas, but this problem is to follow the requirements of the problem to define several methods can be solved, there is no design method involved, no nutrition.

A daily topic

Today’s topic is 2043. Simple banking system, medium difficulty

  • Your task is to design a program for a popular bank that automates all incoming transactions (transfers, deposits, and withdrawals). The bank has n accounts numbered from 1 to N. The initial balance of each account is stored in a balance array of integers with subscripts starting at 0, where the initial balance of the (I + 1) account is balance[I].

  • You execute all valid transactions. The transaction is valid if all of the following conditions are met:

  • The number of specified accounts is between 1 and n, and

  • The amount of money required to withdraw or transfer is less than or equal to the account balance.

  • Implement Bank class:

  • Bank(long[] balance) initializes this object with a balance array of integers with subscripts starting at 0.

  • Boolean Transfer (int Account1, int account2, Long Money) Transfers money dollars from account numbered account1 to account numbered — account2. Return true if the transaction was successful, false otherwise.

  • Boolean deposit(int Account, long Money) Deposit money dollars into an account numbered as Account. Return true if the trade is successful; Otherwise, return false.

  • Withdraw (int account, long money) If the transaction succeeds, – returns true; Otherwise, return false.

 

Example:

Input:"Bank"."withdraw"."transfer"."deposit"."transfer"."withdraw"[[[]10.100.20.50.30]], [3.10], [5.1.20], [5.20], [3.4.15], [10.50] output: [null.true.true.true.false.false[b]new Bank([10.100.20.50.30]);
bank.withdraw(3.10);    // Return true, account 3 has a balance of $20, so you can withdraw $10.
                         // Account 3 balances $20 - $10 = $10.
bank.transfer(5.1.20); // Return true, account 5 has a balance of $30, so $20 can be transferred.
                         // Account 5 has a balance of $30 - $20 = $10 and account 1 has a balance of $10 + $20 = $30.
bank.deposit(5.20);     // Return true to deposit $20 into account 5.
                         // The balance of account 5 is $10 + $20 = $30.
bank.transfer(3.4.15); // Return false, the current balance of account 3 is $10.
                         // Can't transfer $15.
bank.withdraw(10.50);   // Return false, the transaction is invalid because account 10 does not exist.
Copy the code

 

Tip:

  • n == balance.length
  • 1 <= n, account, account1, account2 <= 105
  • 0 <= balance[i], money <= 1012
  • Transfer, deposit and withdraw functions can be called at most 104 times each

Answer key

simulation

I feel that today’s topic is a completely unnutritious topic, which does not involve any algorithm. As long as the logic is written according to the topic, it can be completed. The topic may look very long, but the codes involved are very simple judgments.

First of all, we can use a class to describe the Bank class, so that the following method definition will be more intuitive, compared to es5 tied to prototype writing.

The first step is to define the class, and then save the array that represents the account and its corresponding amount to the class’s properties

class Bank {
  constructor(balance) {
    this.balance = balance; }}Copy the code

Then the question needs a transfer function, which can realize the transfer between two different accounts. If the amount is insufficient, false will be returned if the amount fails, and true will be returned if the amount is successful. This is a simple increase logic, which will be directly added to the code without too much explanation here

transfer(account1, account2, money) {
    if (
      account1 <= this.balance.length &&
      account2 <= this.balance.length &&
      this.balance[account1 - 1] >= money
    ) {
      this.balance[account1 - 1] -= money;
      this.balance[account2 - 1] += money;
      return true;
    }
    return false;
}
Copy the code

Add the specified amount of money to an account. Check whether the account exists and return true if it does, false if it does not

deposit(account, money) {
    if (account <= this.balance.length) {
        this.balance[account - 1] += money;
        return true;
    }
    return false;
}
Copy the code

And then there’s the withdrawal method, where you take a specified amount out of an account, and you make two judgments, whether the account exists, and whether there’s that much money in the account, and you subtract the corresponding value and return true if you don’t return false

withdraw(account, money) {
    if (account <= this.balance.length &&
         this.balance[account - 1] >= money) {
      this.balance[account - 1] -= money;
      return true;
    }
    return false;
}
Copy the code

Put the above three methods into the class here to get the Bank class that the problem needs, because the problem is too simple, I won’t put all the code.