简体   繁体   English

如何使 2 个 object 实例使用银行帐号属性进行通信?

[英]How to make 2 object instances communicate using a bank account number property?

I am a beginner tinkering with classes.我是一个修补类的初学者。

I am trying to create 2 separate bank instances and have them communicate using one of their properties.我正在尝试创建 2 个单独的银行实例,并让它们使用它们的属性之一进行通信。 The property is a bank account number.该属性是一个银行帐号。

One bank instance is meant to be able to transfer a set amount of money to another bank instance using its bank account number as a form of verification.一个银行实例旨在能够使用其银行帐号作为一种验证形式将一定数量的资金转移到另一个银行实例。

Before showing the code, please understand that I am experimenting and learning here.在展示代码之前,请理解我是在这里实验和学习的。 I am aware that the Bank Class should not be extending the BankAccount class.我知道银行 Class 不应扩展 BankAccount class。

Have a look at this code:看看这段代码:

class BankAccount{
        constructor(balance)
        {
                this.balance;
        }
        withdraw(amount)
        {
                this.balance -= amount;
        }
        deposit(amount)
        {
                this.balance += amount;
        }
}

class Bank extends BankAccount{
        constructor(balance,accNum)
        {
                super(balance);
                this.accNum = accNum;
        }
        transfer(fromAccNum,toAccNum,amount)
        {
                if(this.accNum === fromAccNum)
                        super.withdraw(amount);
                toAccNum.BankAccount.deposit(amount);
        }
}

The BankAccount class simply has a balance and two methods that update the balance should a withdrawal or deposit be made. BankAccount class 仅具有余额和两种在提款或存款时更新余额的方法。

The Bank class adds in one extra property to the mix, the bank account number and this is what makes this challenge interesting.银行 class 在组合中添加了一个额外的属性,即银行帐号,这就是使这一挑战变得有趣的原因。

Let us say I am to create two instances of the Bank class like this:假设我要像这样创建银行 class 的两个实例:

var acc01 = new Bank(10000,12345); // a balance of 10 000 and account number 12345
var acc02 = new Bank(1000,54321); // a balance of 1000 and a account number 54321

Using these two separate account I would like to make a call to the transfer method and have it send money from acc01 to acc02.使用这两个单独的帐户,我想调用 transfer 方法并让它从 acc01 向 acc02 汇款。

Something like this:像这样的东西:

acc01.transfer(acc01.accNum,acc02.accNum,1000);

The above line simply passes in the accNum 12345 as the first argument, this is the account number we would like to withdraw from and send to the other account number 54321. The second argument 54321 is the account number we would like to send money to.上面的行只是将 accNum 12345 作为第一个参数传入,这是我们要从中提取并发送到另一个帐号 54321 的帐号。第二个参数 54321 是我们要汇款到的帐号。 The third argument is the amount that we would like to send.第三个参数是我们想要发送的金额。

Results expected after calling the transfer method on acc01:在 acc01 上调用 transfer 方法后预期的结果:

console.log(acc01.balance); // balance should be updated to 9000
console.log(acc02.balance); // balance should be updated to 2000

I can't seem to figure out how to make this work?我似乎无法弄清楚如何使这项工作? Can someone please come to my rescue here??有人可以在这里救我吗?

You can have a BankManager class that holds the Bank instances and manages the transactions.您可以拥有一个保存银行实例并管理交易的 BankManager class。

class BankManager{
    constructor()
    {
       this.accounts = [
           new Bank(10000, 12345),
           new Bank(1000, 54321)
       ];
    }
    transfer(toAcc, fromAcc, amount)
    {
       if (fromAcc.balance >= amount) {
           fromAcc.withdraw(amount);
           toAcc.deposit(amount);
       }
    }
    transferByNum(toAccNum, fromAccNum, amount)
    {
       const toAcc = this.accounts.find(acc => acc.accNum === toAccNum);
       const fromAcc = this.accounts.find(acc => acc.fromAcc === fromAccNum);
       this.transfer(toAcc, fromAcc, amount);
    }
}

I think your problem is that transfer method accepts 'from' and 'to', in addition to having its own acct number.我认为您的问题是 transfer 方法除了拥有自己的帐户号外,还接受“from”和“to”。 This means that there are cases where transfer method is called on completely unrelated acct.这意味着在某些情况下,会在完全不相关的帐户上调用 transfer 方法。

You want to refactor it to您想将其重构为

    transfer(toAcc,amount)
        {
                super.withdraw(amount);
                toAcc.deposit(amount);
        }

Also notice that you need the actual instance of acct, not just acct number, since you don't have a mechanism to retrieve the bank account object based on acct number.另请注意,您需要实际的帐户实例,而不仅仅是帐户编号,因为您没有基于帐户编号检索银行帐户 object 的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM