Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

..... Write a program that computes the net amount of a bank account based a tra

ID: 2246602 • Letter: #

Question

.....

Write a program that computes the net amount of a bank account based a transaction log from a text file. The text file format will be either a D or a W followed by one space and a number. (D means deposit and W means withdrawal) D 500 D 300 W 100 W 900 D 300 W 200 The program should NOT allow withdrawals that would cause a negative balance (an overdraft). Print a message to indicate that the withdrawal cannot be done because it would cause an overdraft. After the information has been processed, print the total of all of the deposits the total of all of the withdrawals the current balance For the example text file shown, the output should be: Withdrawal of $900 cannot be done- OverDraft! Total Deposits: $1100 Total Withdrawals: $300 Balance: $800 Create and Call Functions to do the processing-there should not be ANY print statements in these! HandleDeposit(depAmt, balance) #both parameters are floats Returns the updated balance HandleWithdrawal(withAmt, balance) #both parameters are floats Returns the updated balance OR a message if the withdrawal can't happen UpdateBalance(amt, balance, transaction Type) #amt and curBal are float, transactionType is either "D" or "W" Returns the updated balance

Explanation / Answer

Hi,

The requirement is to read from a file that has transaction details in a simple format like D 200 or W 100 and print the appropriate messages.

First you need to create a file that has the data.

For example I have created one myself that has the data given in the question :

D 500
D 300
W 100
W 900
D 300
W 200

D denotes deposit and W denotes withdrawal.

For deposit, it is straight forward. We just need to add the deposit amount to the balance and that gives the updated balance after the deposit.

For withdrawal, we have to be careful by checking that the withdrawal amount does not exceed the balance. When balance is more than the amount to be withdrawn, then subtract the amount from the balance and that gives the resultant balance.

To implement this in python, please use the code below. It's with the proper comments.

/////////////// ************ Program Starts Here *********** ///////////////////////////////////

# import os module for file operations

import os


# Define a varible to hold the file name that has the data to be processed
fileName = "myData.txt"

# Define a variable that holds the total deposits made
totalDeposits = 0

# Define a variable that holds the total withdrawals made
totalWithdrawals = 0

# Define a method that takes in
# depositAmt : amount to be deposited
# balance : the current balance
# and returns the resultant balance after successful deposit
def HandleDeposit(depositAmt,balance):
global totalDeposits
totalDeposits = totalDeposits + depositAmt
return balance + depositAmt

# Define a method that takes in
# withAmt : amount to be withdrawn
# balance : the current balance
# and returns the resultant balance after successful withdrawal
# This method returns unmodified balance in the error case where
# withdrawal amount exceeds the current balance
def HandleWithdrawal(withAmt,balance):
# we need to explicitly let python know that we are using global variables
global totalWithdrawals
# check if the withdrawal amount exceeds balance
# if yes, print a message and return unmodified balance
if withAmt > balance:
print "Withdrawal of $",withAmt," cannot be done - OverDraft!"
return balance
else:
totalWithdrawals = totalWithdrawals + withAmt
return balance - withAmt

# Define a method that takes in the
# amt : amount to be deposited/withdrawn
# balance : the current balance
# transactionType : D for deposit and W for Withdrawal
def UpdateBalance(amt,balance,transactionType):
if transactionType == 'D':
return HandleDeposit(amt,balance)
elif transactionType == 'W':
return HandleWithdrawal(amt,balance)
  
# Check if the file exists or print an error message
if(os.path.isfile(fileName)):
# open the data file in read only mode as we dont need to modify the file
fo = open(fileName, "r")
# initialize the current balance to 0
balance = 0
# Loop thru the file data
for tempLine in fo:
# split each line to get params required
linePair = tempLine.split()
# at this point linePair will have two values ex:['W',"200"]
# convert the second value from string to int using int() operator
# call UpdateBalance with appropriate params to get the updated balance
balance = UpdateBalance(int(linePair[1]),balance,linePair[0])
print "Total Deposits $",totalDeposits
print "Total Withdrawals $",totalWithdrawals
print "Balance $",balance
fo.close()   
else:
print "file",fileName,"does not exist"

/////////////// ************ Program Ends Here *********** ///////////////////////////////////

Please keep myData.txt file in the same directory that our program resides in.

Thank You.