Print a Check Record

Problem Statement

Suppose we have an input of the following form. There are unknown number of input lines. The first character of each line is D, d, P, p, C or c, where D and d means deposit, P and p means payment, and C and c means a check balance should be printed.
         1    1
....5....0....5
D     +123$$$$$
d      +78$$$$$
c    $$$$$$$$$$
P    $$$$$  +50
D     +100$$$$$
D      +10$$$$$
p    $$$$$ +120
C    $$$$$$$$$$
P    $$$$$  +50
D     +150$$$$$

If the first character is a D or d, this is a deposit line. In this case, positions 6 to 10 contain the deposit amount and positions 11 to 15 contain $$$$$ so that nobody can overwrite this field. If the first character is a P or p, this is a payment line. In this case, positions 6 to 10 contain $$$$$ and positions 11 to 15 contain the payment amount. If the first character is a C or c, then positions 6 to 15 contain $$$$$$$$$$.

Write a Fortran program that reads in these input lines and prints the following table.

         1    1    2    2    3    3    4
....5....0....5....0....5....0....5....0
 Check Book Reference Listing
 ============================

 Count   Payment   Deposit   Balance
 -----   -------   -------   -------
     1   *******      +123      +123
     2   *******       +78      +201
 -----------------------------------
     3        +0      +201      +201
 -----------------------------------
     4       +50   *******      +151
     5   *******      +100      +251
     6   *******       +10      +261
     7      +120   *******      +141
 -----------------------------------
     8      +170      +311      +141
 -----------------------------------
     9       +50   *******       +91
    10   *******      +150      +241
 -----------------------------------
 Total      +220      +461      +241

This table has four columns. The first column prints the transaction numbers. The second column contains the payment amount. It should contain the amount of payment if that transaction is a payment line; otherwise, it should contain *******. The third column contains the deposit amount. It should contain the amount of deposit if that transaction is a deposit line; otherwise, it should contain *******. The fourth column contains the balance after each transaction is made.

If the input line is a C or c, then your program should print a dashed line, followed by the total payment and total deposit up to this point, and the current balance, followed by another dashed line. At the end of the report, a dashed line followed by the total payment, total deposit and current balance must be printed.

Solution

PROGRAM  CheckBook
   IMPLICIT   NONE
   CHARACTER(LEN=1)  :: Code
   CHARACTER(LEN=15) :: Line
   INTEGER           :: Payment, Deposit, Balance, Input
   INTEGER           :: Count
   INTEGER           :: Status
   CHARACTER(LEN=*), PARAMETER :: Filler      = "   *******"
   CHARACTER(LEN=*), PARAMETER :: DashLine    = "-----------------------------------"
   CHARACTER(LEN=*), PARAMETER :: TitleFormat = "(1X, A/1X, A//1X, A/1X, A)"
   CHARACTER(LEN=*), PARAMETER :: InputLine   = "(A)"
   CHARACTER(LEN=*), PARAMETER :: CheckLine   = "(1X, A/ 1X, I5, SP, 3I10/1X, A)"
   CHARACTER(LEN=*), PARAMETER :: PaymentLine = "(1X, I5, SP, I10, A, I10)"
   CHARACTER(LEN=*), PARAMETER :: DepositLine = "(1X, I5, A, SP, 2I10)"
   CHARACTER(LEN=*), PARAMETER :: LastLine    = "(1X, A/ 1X, A, SP, 3I10)"

   WRITE(*,TitleFormat)  "Check Book Reference Listing",        &
                         "============================",        &
                         "Count   Payment   Deposit   Balance", &
                         "-----   -------   -------   -------"

   Payment = 0
   Deposit = 0
   Balance = 0
   Count   = 0
   DO
      READ(*,InputLine,IOSTAT=Status)  Line
      IF (Status < 0)  EXIT
      Code  = Line(1:1)
      Count = Count + 1
      SELECT CASE (Code)
         CASE("C", "c")
            WRITE(*,CheckLine)  DashLine, Count, Payment, Deposit, Balance, DashLine
         CASE("P", "p")
            READ(Line,"(T11,I5)")  Input
            Payment = Payment + Input
            Balance = Balance - Input
            WRITE(*,PaymentLine)  Count, Input, Filler, Balance
         CASE("D", "d")
            READ(Line,"(T6,I5)")   Input
            Deposit = Deposit + Input
            Balance = Balance + Input
            WRITE(*,DepositLine)  Count, Filler, Input, Balance
      END SELECT
   END DO

   WRITE(*,LastLine)  DashLine, "Total", Payment, Deposit, Balance
END PROGRAM  CheckBook
Click here to download this program.

Program Input and Output

If the input data consist of the following:
         1    1
....5....0....5
D     +123$$$$$
d      +78$$$$$
c    $$$$$$$$$$
P    $$$$$  +50
D     +100$$$$$
D      +10$$$$$
p    $$$$$ +120
C    $$$$$$$$$$
P    $$$$$  +50
D     +150$$$$$
The output of the program is:
         1    1    2    2    3    3    4
....5....0....5....0....5....0....5....0
 Check Book Reference Listing
 ============================

 Count   Payment   Deposit   Balance
 -----   -------   -------   -------
     1   *******      +123      +123
     2   *******       +78      +201
 -----------------------------------
     3        +0      +201      +201
 -----------------------------------
     4       +50   *******      +151
     5   *******      +100      +251
     6   *******       +10      +261
     7      +120   *******      +141
 -----------------------------------
     8      +170      +311      +141
 -----------------------------------
     9       +50   *******       +91
    10   *******      +150      +241
 -----------------------------------
 Total      +220      +461      +241

Discussion

The computation of this problem is easy and will not be discussed. We shall only focus on the difficult spot of this problem.