Justin's Life... November 1997

---------------------------------------

/*                                                         Justin Clouse 
 *                                                         SS# XXX-XX-XXXX
 *
 * Assignment #8
 * This program helps manage a checking account by calculating the balance 
 *      and service charges when a transaction takes place.
 *
 * Algorithm
 * 1. Display program's purpose / Declare variables
 * 2. Open input/output files
 * 3. Read initial balance
 *    3.1 Printf("Initial Balance: %.2f", balance);
 * --- Function DataChecking ---
 * 4. If transaction code does not equal 0, 1, or 2, print error message
 * 5. If amount is negative, print error message
 * --- Function Transaction ---
 * 6. If transaction code equals 1
 *    6.1 Transaction is a check. Amount should be subtracted (sign = '-')
 *    6.2 Printf("Transaction: Check in amount of %.2f\n", trans_amt);
 *    6.3 Balance -= trans_amt;
 *    6.4 Printf("Current Balance: $%.2f\n", balance);
 * 7. If transaction code equals 2
 *    7.1 Transaction is a deposit. Amount should be added (sign = '+')
 *    7.2 Printf("Transaction: Deposit in amount of $%.2f\n", trans_amt);
 *    7.3 Balance += trans_amt;
 *    7.4 Printf("Current Balance: $%.2f\n", balance); 
 * 8. If transaction code equals 0
 *    8.1 End of month
 *    8.2 Printf("Transaction: End\n");
 *    8.3 Printf("Current Balance: $%.2f\n", balance);
 * --- Function BalanceManipulation ---
 * 9. Perform balance manipulation
 *    9.1 Balance = balance (+ or -) trans_amt
 *    9.2 Printf("Current Balance: $%.2f\n", balance);
 * --- Function ServiceCharge ---
 * 10. If trans_code = 1
 *    10.1 Add $0.15 to serv_charge
 *    10.2 Printf("Service Charge: Check --- Charge $0.15\n");
 *    10.3 If balance is less than $500
 *        10.3.1 Add 1 to lessthan500
 *        10.3.2 If lessthan500 equals 1
 *               10.3.2.1 Add $5 to serv_charge
 *               10.3.2.2 Printf("Service Charge: Below $500 --- Charge ");
 *               10.3.2.3 Printf("$5.00\n");
 * 11. If trans_code = 2
 *    11.1 Add $0.10 to serv_charge
 *    11.2 Printf("Service Charge: Deposit --- Charge $0.10\n");
 * 12. If balance is less than $50
 *    12.1 Printf("Warning: Your balance is below $50.00\n");
 *        12.3.3 If balance is less than $0 and sign = '-'
 *        12.3.4 Add $10 to serv_charge
 *        12.3.5 Printf("Service Charge: Negative Balance --- $10.00\n");
 * 13. Printf("Total Service Charge: %.2f\n\n", serv_charge);
 * ------
 * 14. Close data files
 * 15. End
 */
#include <stdio.h>

/* Displays program purpose */
void instruct(FILE *outp) 
{
     fprintf(outp, "\n\nThis program helps manage a checking account \n");
     fprintf(outp, "by calculating the balance and service charges \n");
     fprintf(outp, "when a transaction takes place. \n\n");
}

/* Function To Check Data Validity                              */
int datacheck(int trans_code, float trans_amt, FILE *outp){
     if (trans_code != 0 && trans_code != 1 && trans_code != 2){
         fprintf(outp, "Your file contains an invalid transaction code.\n");
         return(1);
     } else if (trans_amt < 0){
               fprintf(outp, "Your file contains negative numbers which\n");
               fprintf(outp, "are not allowed.\n");
               return(1);
     } else return(0);
}

/* Function To Determine Transaction                             */
void transaction(int trans_code, float trans_amt, float *balance, 
                 FILE *outp){
     if (trans_code = 1){
         fprintf(outp, "Transaction: Check in amount of %.2f\n", trans_amt);
         *balance -= trans_amt;
     }
     if (trans_code = 2){
         fprintf(outp, "Transaction: Deposit in amount of $%.2f\n", 
                 trans_amt);
         *balance += trans_amt;
     }
     if (trans_code = 0){
         fprintf(outp, "Transaction: End\n");
     }
}

/* Function To Calculate Service Charge                          */
void servicecharge(int trans_code, float *balance, float serv_charge, 
                   int *lessthan500, FILE *outp){
     if (trans_code = 1){
         serv_charge += .15;
         fprintf(outp, "Service Charge: Check --- Charge $0.15\n");
         if (*balance < 500){
             *lessthan500 += 1;
             if (*lessthan500 = 1){
                 serv_charge += 5;
                 fprintf(outp, "Service Charge: Below $500 --- Charge ");
                 fprintf(outp, "$5.00\n");
             }
         }
     }
     if (trans_code = 2){
         serv_charge += .10;
         fprintf(outp, "Service Charge: Deposit --- Charge $0.10\n");
     }
     if (*balance < 50){
         fprintf(outp, "Warning: Your balance is below $50.00\n");
         if (trans_code = 1){
             serv_charge += 10;
             fprintf(outp, "Service Charge: Negative Balance --- $10.00\n");
         }
     }
     fprintf(outp, "Total Service Charge: %.2f\n\n", serv_charge);
}

/* Main Function */
void main(void)
{
     /* Declare variables                                        */
     int trans_code, lessthan500 = 0, all_clear;
     float balance, trans_amt, serv_charge;

     /* Pointer to input file                                    */
     FILE *inp, *outp;

     /* Open input file                                          */
     inp = fopen("account.dat", "r");
     outp = fopen("transactions.dat", "w");

     /* Call function to display program's purpose               */
     instruct(outp);

     /* Initial balance                                          */
     fscanf(inp, "%lf", &balance);
     fprintf(outp, "Initial Balance: %.2f", balance);

     /* Get Input And Output Data                                */
     while (fscanf(inp,"%lf", &trans_code) && 
            fscanf(inp,"%lf", &trans_amt) != EOF){
       fscanf(inp, "%lf", &trans_code);
       fscanf(inp, "%lf", &trans_amt);
       all_clear = datacheck(trans_code, trans_amt, outp);
       if (all_clear != 1){
           transaction(trans_code, trans_amt, &balance, outp);
           servicecharge(trans_code, &balance, serv_charge, 
                         &lessthan500, outp);
       fprintf(outp, "Final Balance: %.2f", balance);
       }
     }

     fclose(inp);
     fclose(outp);
}