Due Thursday, March 10, 11:59 P.M.
- Overview
- Assignment
- Hints
- Extra Credit
The purpose of this lab is to introduce you to file processing and
formatted I/O in Fortran.
Top
Write a Fortran program called calc_interest that does the following:
- Prompts the user for a file from which to read information about
credit card accounts.
- Saves this file to a backup.
- Calculates the monthly interest on each account, and modifies the
balance accordingly.
- Overwrites the new information to the original filename.
If you encounter an error in the input file (as described below), you
should restore your backup to the original filename, and then terminate
program execution.
The credit card information file has the following format:
- Each line of the file is either a credit card type specification
line or a credit card account specification line. Any other type of
line is a syntax error.
- Credit card type specification lines must match one of the following forms:
CARD TYPE AMEX
CARD TYPE DISCOVER
CARD TYPE MASTERCARD
CARD TYPE VISA
- Credit card account specification lines have a 16-character,
left-justified field with the credit card number, followed by a space,
followed by a dollar sign, followed by a 10-character, right-justified
field with the account balance using two decimal points of accuracy,
followed by a space, followed by a 5-character, right-justified field
with the apr using two decimal places, followed by a percent sign and a
space, followed by the expiration date in mm/dd/yyyy format, where each
part of the date is padded with zeros if necessary. Every credit card
specification line should satisfy the requirements for the most recently
specfied card type (see below). For example, given the input file:
CARD TYPE VISA
4506179898962477 $ 43496.12 1.04% 11/04/2004
4811406725534780 $ 33050.79 3.63% 02/23/2006
4297814912190 $ 93863.89 11.71% 01/21/2010
4496751055738 $ 48995.99 0.37% 02/26/2005
4871281391683744 $ 89259.21 17.19% 01/13/2004
4285092316432 $ 36897.21 7.55% 07/19/2004
4426837266603 $ 66839.93 16.68% 06/11/2010
4387709372400 $ 80579.90 15.47% 06/24/2008
4696974502990255 $ 84896.05 8.87% 12/19/2006
4025904354564622 $ 9923.03 15.19% 02/15/2008
4817344081430933 $ 32199.98 16.36% 10/22/2010
CARD TYPE MASTERCARD
5401055476535031 $ 56465.88 15.32% 06/08/2006
5254832387322304 $ 83920.30 17.55% 05/16/2008
5255282738228125 $ 70486.18 11.79% 11/01/2007
5466961287594533 $ 49742.97 8.87% 10/03/2010
5471272423643480 $ 12835.34 7.75% 08/02/2005
5250514861550479 $ 30521.60 14.68% 06/07/2008
5397028239089487 $ 66690.59 7.91% 11/15/2003
the first 11 cards should satisfy the requirements for VISA, and the
remainder should satisfy the requirements for MASTERCARD.
The bulk of your work for this lab will be detecting syntax and data
errors in the input file. Your program should detect the following errors:
- Invalid credit card number check digit (bonus 5 points)
- Invalid credit card number prefix
- Invalid credit card number length
- Invalid characters in credit card number
- Invalid credit card type
- Unspecified card types
- Invalid data type (character instead of real, etc.)
- Invalid spacing of input lines (wrong field width, extra spaces between fields, etc.)
- Invalid apr (negative numbers)
- Invalid date
- Truncated lines
See this
reference on credit card numbers for help determining what constitutes
a valid check digit, prefix, and length for each credit card type.
Top
- To start out, just make a prompt for a filename, and use a character
variable to copy the contents of this file to standard output if
it exists. If the file does not exist or cannot be opened, continue
looping until a valid filename is found.
- Now add some code to move everything to a backup file. You may
either modify the loop you wrote above, or insert the following code
into your program:
USE ifport ! this line goes after your PROGRAM statement and before your IMPLICIT none statement
...
RENAME(oldname, newname) ! this line goes wherever you want the backup to occur
where oldname and newname are character strings containing the old and new filenames.
This has exactly the same effect as the UNIX shell command
mv oldname newname
You will want to test that the RENAME function returns a value
of zero to make sure it completes successfully. The purpose of the USE
command is tell the compiler you want to include the ifport module, which
provides a definition of RENAME.
- Now go back and check whether each line in your character variable
is a card type specification line, and reject any invalid card types.
Your code should ignore any other lines at this point.
- Add some code that attempts to use a format to read in any lines you
were ignoring in the previous step. You will need a backspace statement,
a formatted input statement using nonadvancing I/O, another backspace
statement, and another formatted input statement using advancing I/O.
Use the return value of the first input statement to detect any errors
in the formatting of each line. After reading in each line, write them
out again using the same format. Note that since credit card numbers
may be up to 16 digits long and integers can only hold numbers up to
2**31=2147483648, you will need to store the credit card numbers in a
character variable. The other data can all be stored in variables of the
appropriate type. Although it is possible to tell the compiler to use
64-bit integers (which can hold numbers up to 2**63=9223372036854775808,
i.e. 18 arbitrary digits) using the "-integer_size 64"
flag, this solution is unsatisfactory since
- Now calculate the interest and modify the balances.
- Now add some code to check whether credit cards contain non-numeric data.
- Add check digit support. This will require you to convert the
character representation of the digits of the credit card number to
integers. The ACHAR and IACHAR functions will help
you here. Remember that your textbook has a copy of the ASCII collating
sequence, which is also available using the command man ascii.
You should assume the last digit is the check digit.
- Add checking for the card type-dependent errors, i.e. invalid credit
card number lengths and prefixes.
- Add tests for the remaining data errors. Make sure the apr is
positive, interest is calculated only for positive balance, and check
for valid dates. Be sure your date test works correctly for leap years.
- Try out your program on these Sample Input Files
- Test some more to make sure your program catches any other errors not included
in one of the input files I provided you with.
Top
For an additional 15 points, write a program called ccgen that generates random, error-free input
files for your interest calculator.
Top
Back to the course information page