Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

BankBalance Example Program

This program reads transaction data from a file, calculates a bank account balance by processing deposits, withdrawals and interest payments, and writes the output to a separate file. It initializes variables, opens input and output files, reads the initial account balance and transactions, uses a switch statement to process each transaction by adding or subtracting from the running balance, tracks deposit, withdrawal and interest totals, charges a fee if the balance falls below a minimum, and writes the ending balance and transaction summaries to the output file.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

BankBalance Example Program

This program reads transaction data from a file, calculates a bank account balance by processing deposits, withdrawals and interest payments, and writes the output to a separate file. It initializes variables, opens input and output files, reads the initial account balance and transactions, uses a switch statement to process each transaction by adding or subtracting from the running balance, tracks deposit, withdrawal and interest totals, charges a fee if the balance falls below a minimum, and writes the ending balance and transaction summaries to the output file.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//*******************************************************

// Author: D.S. Malik


//
// Program -- Checking Account Balance.
// This program calculates a customer's checking account
// balance at the end of the month.
//*******************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const double MINIMUM_BALANCE = 1000.00;
const double SERVICE_CHARGE = 25.00;
int main()
{
//Declare and initialize variables
int acctNumber;
double beginningBalance;
double accountBalance;

//Step 1

double amountDeposited = 0.0;


int numberOfDeposits = 0;
double amountWithdrawn = 0.0;
int numberOfWithdrawals = 0;
double interestPaid = 0.0;
char transactionCode;
double transactionAmount;
bool isServiceCharged = false;
ifstream infile;
ofstream outfile;
infile.open("Ch5_money.txt");

//Step 2

if (!infile)
//Step 3
{
cout << "Cannot open the input file." << endl;
cout << "Program terminates!!!" << endl;
return 1;
}
outfile.open("Ch5_money.out");

//Step 4

outfile << fixed << showpoint;


outfile << setprecision(2);

//Step 5
//Step 5

cout << "Processing data" << endl;


infile >> acctNumber >> beginningBalance;
accountBalance = beginningBalance;

//Step 6
//Step 7

infile >> transactionCode >> transactionAmount; //Step 8


while (infile)
{

//Step 9

switch (transactionCode)
{
case 'D':
//Step 9.a
case 'd':
accountBalance = accountBalance
+ transactionAmount;
amountDeposited = amountDeposited
+ transactionAmount;
numberOfDeposits++;
break;
case 'I':
//Step 9.b
case 'i':
accountBalance = accountBalance
+ transactionAmount;
interestPaid = interestPaid
+ transactionAmount;
break;
case 'W':
//Step 9.c
case 'w':
accountBalance = accountBalance
- transactionAmount;
amountWithdrawn = amountWithdrawn
+ transactionAmount;
numberOfWithdrawals++;
if ((accountBalance < MINIMUM_BALANCE)
&& (!isServiceCharged))
{
accountBalance = accountBalance
- SERVICE_CHARGE;
isServiceCharged = true;
}
break;
default:
cout << "Invalid transaction code" << endl;
} //end switch
infile >> transactionCode >> transactionAmount;
}//end while
//Output Results
//Step 10
outfile << "Account Number: " << acctNumber << endl;
outfile << "Beginning Balance: $" << beginningBalance
<< endl;
outfile << "Ending Balance: $" << accountBalance
<< endl << endl;
outfile << "Interest Paid: $" << interestPaid << endl
<< endl;
outfile << "Amount Deposited: $" << amountDeposited
<< endl;
outfile << "Number of Deposits: " << numberOfDeposits
<< endl << endl;
outfile << "Amount Withdrawn: $" << amountWithdrawn
<< endl;
outfile << "Number of Withdrawals: "
<< numberOfWithdrawals << endl << endl;
if (isServiceCharged)
outfile << "Service Charge: $" << SERVICE_CHARGE
<< endl;
}

return 0;

Input File called money.txt used by the above program:


467343
23750.40
W 250.00
D 1200.00
W 75.00
W 375.00
D 580.00
I 245.50
W 400.00
W 600.00
D 450.50
W 35.65

Output money.out has following contents


Account Number: 467343
Beginning Balance:
$23750.40
Ending Balance: $24490.75
Interest Paid: $245.50
Amount Deposited: $2230.50
Number of Deposits: 3
Amount Withdrawn: $1735.65
Number of Withdrawals: 6

You might also like