这是我为银行交易类编写的转账方法:
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)
我通过将接收器类的实例传递给传输函数并在该实例上执行存款来解决了该问题。如果接收者的实例未传递给传输方法,则接收者将设置为
None
。这是代码:
def transfer(self,sender_name, sender_account_number, receiver_name, receiver_account_number, amount,receiver_instance = None):
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)
):
if receiver_instance != None:
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"