In: Computer Science
Objective Work with strings
Work with files
Work with formatted output
This program will display a very basic handling of transactions for a checking account. The program will read in a data file with all the past transactions. Use FileUtil.selectOpenFile or input() to get a file name from the user. The format of the file will be very basic, just a date (as a string in the format of MM/DD), a single letter which indicates the type of transaction (‘B’ for beginning balance, ‘C’ for a check, ‘D’ for a deposit, or ‘W’ for a withdrawl) and an amount (floating point value). The transaction type may be upper or lower case, but there are string methods which make this a nonissue. The first line of the data WILL BE the beginning balance. The remaining lines may be for any type of transaction other than specifying the beginning balance. All lines will have three values, separated by commas, and in the order specified. An sample data file might be:
10/01,B,1000.00
10/02,C,100.00
10/03,C,9.00
10/04,C,50.00
10/04,W,20.00
You do not need to write a program to create the data file, you can use any text editor. You can write a program to create the file if you want, but you don’t need to turn it (don’t turn it in ??) and you won’t receive extra credit for it. After reading each line, break it up into its inidivual pieces of information. The split() method would likely be the easiet way to do so, but you can use the find() or index() methods as well. Write the information from the data file to the display in a neat, column-aligned format with an updated balance on each line. Expand the single letter for the transaction type into complete text, as demonstrated below. Make sure each dollar amount has exactly 2 places after the decimal point. Also include today’s date at the top of the table. A simple search using “Python” and “today’s date” will provide the info on how to access the current date from the system clock. We also did this in an example in class, so that might be the quickest place to check.
A sample of the output, using the data from above, might be: Checkbook balance as of 10/26 10/01 Beginning Balance 1000.00 10/02 Check 100.00 900.00 10/03 Check 9.00 891.00 10/04 Withdrawal 20.00 871.00 10/05 Check 50.00 821.00
Here I am giving solution using two ways (chnages only in output display)
1. Using prettytable to create table in output
2. Without using prettytable
1. Using prettytable to create table in output
Here i am using prettytable package of python to create a table. So first install the package using below instruction:
pip install prettytable
Code:
from prettytable import PrettyTable # Used to create a
table
from datetime import datetime # used to get current date
x = PrettyTable() # create tabel
x.field_names = ["Date (MM/DD)", "Type of Transaction", "Amount", "Balance"] # Give name of column in table
# Create dictionary of transaction type
trans_type = {'B' : 'Beginning Balance', 'C' : 'Check', 'D' :
'Deposit', 'W': 'Withdrawal', 'b' : 'Beginning Balance', 'c' :
'Check', 'd' : 'Deposit', 'w': 'Withdrawal'}
# Take filename from user
filename = input('Enter File Name ')
# Try to handle file not found error
try:
with open(filename) as fp: # Open a file
line = fp.readline() # Read line from file
while line:
line = line.rstrip('\n') # Remove newline chacater from line
data = line.split(',') # Split the line data
if data[1] == 'B' or data[1] == 'b': # if transaction type is
'B' or 'b'
x.add_row([data[0],trans_type[data[1]],'-----',data[2]]) # Add data
into table row
bal = float(data[2]) # Store the balance into bal variable
elif data[1] == 'C' or data[1] == 'c': # if transaction type is 'C'
or 'c'
x.add_row([data[0],trans_type[data[1]],data[2], bal -
float(data[2])]) # Add data into table row
bal = bal - float(data[2]) # Update the balance
elif data[1] == 'D' or data[1] == 'd': # if transaction type is 'D'
or 'd'
x.add_row([data[0],trans_type[data[1]],data[2],bal +
float(data[2])]) # Add data into table row
bal = bal + float(data[2]) # Update the balance
elif data[1] == 'W' or data[1] == 'w': # if transaction type is 'W'
or 'w'
x.add_row([data[0],trans_type[data[1]],data[2],bal -
float(data[2])]) # Add data into table row
bal = bal - float(data[2]) # Update the balance
line = fp.readline() # Read line from file
# Print the message with current month and day
print('\n\n\t\tCheckbook Balance as
of',str(datetime.now().month)+'/'+str(datetime.now().day))
print(x) # Print table
except FileNotFoundError: # If file not found
print("File does not exist") # Print a message
Output:
2. Without using prettytable
from datetime import datetime # used to get current date
# Create dictionary of transaction type
trans_type = {'B' : 'Beginning Balance', 'C' : 'Check', 'D' :
'Deposit', 'W': 'Withdrawal', 'b' : 'Beginning Balance', 'c' :
'Check', 'd' : 'Deposit', 'w': 'Withdrawal'}
# Take filename from user
filename = input('Enter File Name ')
# Print the message with current month and day
print('\n\n\t\tCheckbook Balance as
of',str(datetime.now().month)+'/'+str(datetime.now().day))
print("\nDate (MM/DD)", '\t',"Type of Transaction", '\t',"Amount",
'\t',"Balance\n")
print('------------------------------------------------------------------------\n')
# Try to handle file not found error
try:
with open(filename) as fp: # Open a file
line = fp.readline() # Read line from file
while line:
line = line.rstrip('\n') # Remove newline chacater from line
data = line.split(',') # Split the line data
if data[1] == 'B' or data[1] == 'b': # if transaction type is
'B' or 'b'
print(data[0],'\t\t',trans_type[data[1]],'\t','
','\t\t',data[2])
bal = float(data[2]) # Store the balance into bal variable
elif data[1] == 'C' or data[1] == 'c': # if transaction type is 'C'
or 'c'
print(data[0],'\t\t',trans_type[data[1]],'\t\t ',data[2],'\t\t',bal
- float(data[2]))
bal = bal - float(data[2]) # Update the balance
elif data[1] == 'D' or data[1] == 'd': # if transaction type is 'D'
or 'd'
print(data[0],trans_type[data[1]],data[2],bal +
float(data[2]))
bal = bal + float(data[2]) # Update the balance
elif data[1] == 'W' or data[1] == 'w': # if transaction type is 'W'
or 'w'
print(data[0],'\t\t',trans_type[data[1]],'\t ',data[2],'\t\t',bal -
float(data[2]))
bal = bal - float(data[2]) # Update the balance
line = fp.readline() # Read line from file
except FileNotFoundError: # If file not found
print("File does not exist") # Print a message
Output:
;Please thumbs up for my solution.