如何获取类的实例

问题描述 投票:0回答:1

这是我为银行交易类编写的转账方法

def transfer(self, sender_name, sender_account_number, receiver_name, receiver_account_number, amount):
        if (
            isinstance(sender_name, str)
            and isinstance(sender_account_number, int)
            and isinstance(receiver_name, str)
            and isinstance(receiver_account_number, int)
            and isinstance(amount, float)
        ):
            sender_balance = self.get_balance(sender_account_number)
            
            # Get the receiver instance using receiver_account_number
            receiver = self.get_details(receiver_account_number)
            receiver_balance = receiver.get('balance', 0.0)

            if amount <= sender_balance and self.verify_user(receiver_name, receiver_account_number, receiver_balance):
                self.withdrawal(sender_name, sender_account_number, amount)
                receiver['balance'] += amount  # Update the receiver's balance
                return 'Transfer Completed'
            else:
                return "Transfer Failed: Insufficient Sender Account Balance or Invalid Receiver Account Details"
        else:
            return "Invalid Transfer Details"

但是我在尝试向接收者实例执行存款时一直遇到问题,我认为此错误是由于在接收者实例上使用 self 引起的,因为 self 代表发送者实例。

这是转移测试

def test_transfer(self):
        # Setting up a second account for testing transfer
        receiver_account = Bank_Transactions(name="Jane Doe", account_number=654321, account_balance=500.0)

        transfer_result = self.bank_account.transfer("John Doe", 123456, "Jane Doe", 654321, 300.0)
        self.assertEqual(transfer_result, 'Transfer Completed')
        self.assertEqual(self.bank_account.get_balance(123456), 700.0)

        # Create a separate receiver instance to verify its balance
        receiver_details = Bank_Transactions(name="Jane Doe", account_number=654321, account_balance=0.0)
        receiver_balance = receiver_details.get_balance(654321)
        self.assertEqual(receiver_balance, 800.0)

        invalid_result = self.bank_account.transfer("John Doe", 123456, "Janet Doe", 987654, 200.0)
        self.assertEqual(invalid_result, 'Transfer Failed: Insufficient Sender Account Balance or Invalid Receiver Account Details')
        self.assertEqual(self.bank_account.get_balance(123456), 700.0)

        # Verify the receiver's balance again
        receiver_balance = receiver_details.get_balance(654321)
        self.assertEqual(receiver_balance, 800.0)
class instance
1个回答
0
投票

因此,我通过将接收器类的实例传递给传输函数并在该实例上执行存款来解决该问题。如果接收者的实例未传递给传输方法,则接收者将设置为 None。这是代码

def 转账(自身,发送者名称,发送者帐户号码,接收者名称,接收者帐户号码,金额,接收者实例=无): 如果 ( isinstance(发件人名称, str) 和 isinstance(sender_account_number, int) 和 isinstance(receiver_name, str) 和 isinstance(receiver_account_number, int) 和 isinstance(金额,浮动) ): 如果receiver_instance!=无: sender_balance = self.get_balance(sender_account_number)

            # Get the receiver instance using receiver_account_number
            receiver = receiver_instance.get_details(receiver_account_number)
            receiver_balance = receiver.get('balance', 0.0)

            if amount <= sender_balance and receiver_instance.verify_user(receiver_name, receiver_account_number, receiver_balance):
                self.withdrawal(sender_name, sender_account_number, amount)
                receiver_instance.deposit(receiver_name, receiver_account_number, amount)
                return "Transfer Completed"
            else:
                return "Transfer Failed: Insufficient Sender Account Balance or Invalid Receiver Account Details"
        return "Invalid Transfer Details"
    return "Invalid Transfer Details"
© www.soinside.com 2019 - 2024. All rights reserved.