interestcalculator/Project2/Interest.cpp
2023-04-11 18:40:15 -07:00

130 lines
2.9 KiB
C++

/*
* Cody Cook
* Project 2
* SNHU
* 2023/04/02
*/
#include <string>
#include <iostream>
#include <iomanip>
#include "Interest.h"
#include "Menu.h"
// Overloaded constructor
Interest::Interest()
{
m_initial = 0;
m_rate = 0;
m_years = 0;
m_monthly = 0;
}
// Overloaded constructor
Interest::Interest(double initial, double monthly, double rate, unsigned int years)
{
m_initial = initial;
m_rate = rate;
m_years = years;
m_monthly = monthly;
}
// Setters
// Set the initial investment amount
void Interest::setInitial(double t_initial)
{
m_initial = t_initial;
}
// Set the interest rate
void Interest::setRate(double t_rate)
{
m_rate = t_rate;
}
// Set the number of years
void Interest::setYears(unsigned int t_years)
{
m_years = t_years;
}
// Set the monthly deposit amount
void Interest::setMonthly(double t_monthly)
{
m_monthly = t_monthly;
}
// Getters
// Get the initial investment amount
double Interest::getInitial() const
{
return m_initial;
}
// Get the interest rate
double Interest::getRate() const
{
return m_rate;
}
// Get the number of years
unsigned int Interest::getYears() const
{
return m_years;
}
// Get the monthly deposit amount
double Interest::getMonthly() const
{
return m_monthly;
}
// calculate the balance; if t_monthlyDeposit is true, add the monthly deposit amount to the balance each month
void Interest::calculateBalance(bool t_monthlyDeposit)
{
// set variables
double yearEndBalance = m_initial;
double interestEarned = 0;
double interestRatePerMonth = m_rate / 100 / 12;
const std::string HEADERS[] = {"Year", "Year End Balance", "Interest Earned"};
std::string deposit;
// set the string depending on mode
if (t_monthlyDeposit)
{
deposit = "with";
}
else
{
deposit = "without";
}
// display headers
surroundText("Results " + deposit + " a monthly deposit", 74, '#', true);
std::cout << HEADERS[0] << "\t\t\t" << HEADERS[1] << "\t\t" << HEADERS[2] << "\t" << '#' << std::endl;
// for every year
for (unsigned int year = 1; year <= m_years; year++)
{
double yearEndInterestEarned = 0;
// for every month
for (int month = 1; month <= 12; month++)
{
// find the amount of interest earned, add it to the year end value, then add the interest to the balance
interestEarned = yearEndBalance * interestRatePerMonth;
yearEndInterestEarned += interestEarned;
yearEndBalance += interestEarned;
// if the mothly deposit is set, add it to the balance
if (t_monthlyDeposit)
{
yearEndBalance += m_monthly;
}
}
// output the details
printDetails(year, yearEndBalance, yearEndInterestEarned);
}
}