Question
For this task, you are to write code that handles the transfer of funds between two bank accounts. Instructions As this task relates to tranferring
For this task, you are to write code that handles the transfer of funds between two bank accounts.
Instructions
As this task relates to tranferring funds between bank accounts, a BankAccount class has been provided, which supports depositing and withdrawing funds. A simple interactive loop has already been implemented, which allows the user to repeatedly transfer funds from one account to another.
a) Defining the transfer_funds function
You are to write a function called transfer_funds with three parameters: the amount to transfer, the account the funds are coming from, and the account the funds are going to. It should use the appropriate instance methods on each of the accounts to update their balances as required.
b) Preventing negative balances
If you tested your solution thoroughly in the previous part, you probably came across a logic error in the program---there's nothing to stop us from transferring funds from an account, even if the account balance becomes negative! You are to fix this problem by raising an exception and preventing the account balance from becoming negative. You should do this by raising a ValueError in the appropriate place in the BankAccount class.
Hint: The order in which the withdrawal and deposit occur in transfer_funds matters---the wrong order will result in the creation of free money!
c) Exception handling
At this point, the program prevents a transfer occurring if there aren't enough funds in the "from" account. However, simply crashing a program isn't a very nice user experience. You are to modify your program so that it handles the ValueError and displays ***Transfer cancelled*** to the user. The program should otherwise continue as normal, with the user being asked whether they would like to perform another transaction. A failed transaction should not result in either account balance changing.
Hint: A little rusty at exception handling? Refer back to Topic 8 and the associated lab for examples.
Criteria
During marking we will check that your program:
Defines a function with correct parameters and name for transfer_funds. (2 marks)
Calls the appropriate methods to deposit and withdraw funds. Does not directly access the balance instance variable from within transfer_funds. (2 marks)
Must not accept amount <$0 or non numeric input (2)
Continues normally after handling the invalid input (3)
Checks to see whether the balance would become negative from the appropriate BankAccount method. (2 marks)
Raises a ValueError when the negative balance check fails. (2 marks)
Withdrawing and depositing occur in the correct order to prevent "free money". (2 marks)
Uses a try/except block to handle exceptions of a specific exception type and display an appropriate message to the user. (4 marks)
Has only one line of code (the transfer_funds function call) in the try block. (1 mark)
Continues normally after exception handling without changing either bank balance as a result of the failed transaction. (2 marks)
Executes as expected for example and other inputs. (3 marks)
Example Runs
Run 1
== Account balances == Savings: $1000.00 Debit: $ 200.00 TOTAL: $1200.00 Enter transfer amount ($): 300.50 == Account balances == Savings: $ 699.50 Debit: $ 500.50 TOTAL: $1200.00 Perform another transfer? (y/n): n
Run 2
== Account balances == Savings: $1000.00 Debit: $ 200.00 TOTAL: $1200.00 Enter transfer amount ($): 95 == Account balances == Savings: $ 905.00 Debit: $ 295.00 TOTAL: $1200.00 Perform another transfer? (y/n): y Enter transfer amount ($): 1000 ***Transfer cancelled*** == Account balances == Savings: $ 905.00 Debit: $ 295.00 TOTAL: $1200.00 Perform another transfer? (y/n): y Enter transfer amount ($): 900 == Account balances == Savings: $ 5.00 Debit: $1195.00 TOTAL: $1200.00 Perform another transfer? (y/n): n
Your code should execute as closely as possible to the example runs above. To check for correctness, ensure that your program gives the same outputs as in the examples, as well as trying it with other inputs.
#### Your Solution
class BankAccount:
def __init__(self, name, initial_balance):
self.name = name
self.balance = initial_balance
def deposit(self, amount):
self.balance = self.balance + amount
def withdraw(self, amount):
# b) Raise an exception as appropriate
self.balance = self.balance - amount
def print_balances(account_a, account_b):
print('== Account balances ==')
print(f'{account_a.name:>9s}: ${account_a.balance:7.2f}')
print(f'{account_b.name:>9s}: ${account_b.balance:7.2f}')
total_balance = account_a.balance + account_b.balance
print(f' TOTAL: ${total_balance:7.2f}')
# a) Write your transfer_funds function here
account_a = BankAccount('Savings', 1000)
account_b = BankAccount('Debit', 200)
print_balances(account_a, account_b)
another_transfer = 'y'
while another_transfer == 'y':
# c) Handle input error
amount = float(input('Enter transfer amount ($): '))
# d) Print an appropriate message if an exception is encountered
transfer_funds(amount, account_a, account_b)
print_balances(account_a, account_b)
another_transfer = input('Perform another transfer? (y/n): ')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started