// Saliha Babar                   CS1A                      Practice Practicum
//
/*****************************************************************************
 * CALCULATE SAVINGS ACCOUNT BALANCE 
 * ___________________________________________________________________________
 * This program accepts annual rate, starting balance, and number of months 
 * that has passed since the account was established as well as the amount of 
 * deposited and withdrawed each month.
 * This program then calculates the ending balance of an account.
 *
 * Formula related to this program
 * Amount of Interest = balance * mothly interest rate
 * Balance = deposited - withdrawn
 * Ending Balance = Initial balance + balance
 * ____________________________________________________________________________
 * INPUT 
 *    numOfMonths;       : number of months 
 *    annualRate         : annual rate
 *    monthlyRate;       : monthly rate
 *    monthlyRateDecimal : monthly rate in decimal
 *    initialBalance     : starting balance of an account
 *    amtDeposited       : deposited amount of money each month
 *    amtWithdrawn      : withdrawed amount of money each month
 * 
 * OUTPUT
 *    balance            : balance of the account
 *    totalDeposited;    : total deposited amount
 *    totalWithdrawn;   : total withdrawed amount
 *    interestPerMth;    : interest earned per month
 *    totalInterest      : total intrest 
 *    endingAmt;         : ending balance of the account
 * 

 * 
 * ***************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int numOfMonths;          // INPUT - number of months 
	float annualRate;         // INPUT - annual rate
	float monthlyRate;        // INPUT - monthly rate
	float monthlyRateDecimal; // INPUT - monthly rate in decimal
	float initialBalance;     // INPUT - starting balance of an account
	float amtDeposited;       // INPUT - deposited amount of money each month
	float amtWithdrawn;       // INPUT - withdrawed amount of money each month
	float balance;            // OUTPUT - balance of the account
	float totalDeposited;     // OUTPUT - total deposited amount
	float totalWithdrawn;     // OUTPUT - total withdrawed amount
	float interestPerMth;     // OUTPUT - interest earned per month
	float totalInterest;      // OUTPUT - total Intrest
	float endingAmt;          // OUTPUT - ending balance of the account
	
	cout << "Enter the annual rate. Eg: enter 10 if the rate is 10%.\n";
	cin >> annualRate;
	monthlyRate = annualRate / 12;
	monthlyRateDecimal = monthlyRate/ 100;
	
	cout << "Enter the starting balance of the account\n";
	cin >> initialBalance;
	
	cout << "Enter number of months passed since the account was established.\n";
	cin >> numOfMonths;
	
	
	// Set accumulators to 0
	balance = initialBalance;
	totalDeposited = 0;
	totalWithdrawn = 0;
	totalInterest = 0;
	
	
	for (int i = 1 ; i <= numOfMonths ; i ++)
	{
		cout << "Enter the amount deposited in the account for month : " << i << endl;
		cin >> amtDeposited;
		
		while ( amtDeposited < 0 )
		{
			cout << "Only enter positive number\n";
			cin >> amtDeposited;
		}
		totalDeposited += amtDeposited;
		
		cout << "Enter the amount withdrawed in the account for month : " << i << endl;
		cin >> amtWithdrawn;
		
		while ( amtWithdrawn < 0 )
		{
			cout << "Only enter positive number\n";
			cin >> amtWithdrawn;
		}
		totalWithdrawn += amtWithdrawn;

		balance += amtDeposited - amtWithdrawn;
		interestPerMth = monthlyRateDecimal * balance;
		totalInterest += interestPerMth;
		
		balance += interestPerMth;
		
		if (balance <= 0)
		{
			cout << "Balance is negative. The account has been closed.\n";
			i = numOfMonths + 1;
		}
	}
	
	// Calculate monthly interest
	
	cout << fixed << setprecision(2) << endl;
	cout << "Total amounts deposited is $" << totalDeposited << endl;
	cout << "Total amounts withdrawls is $" << totalWithdrawn << endl;
	cout << "Total interest earned is $" << totalInterest << endl;
	cout << "The ending balance is $" << balance << endl;
	
	return 0;
}