In: Computer Science
Description
Typically, most everyone saves money periodically for retirement. In this exercise, for simplicity, we assume that the money is put into an account that pays a fixed interest rate, and money is deposited into the account at the end of the specified period. Suppose the person saving for retirement deposits D dollars m times per year. The account pays r% compound interest rate. And the person will be saving for a period of t time given in years. For example, the person might save D = $500 every m = 12 months. The retirement account pays 4.8% compound interest per year. And the person as t = 25 years to save for retirement. The total amount of savings S accumulated at the end of this time is given by the formula
S = D h (1 + r/m) mt − 1 r/m i
For example, suppose that you deposit $500 at the end of each month (m = 12). The account pays 4.8% interest per year compounded monthly for t = 25 years. Then the total money accumulated into this account is
500[(1 + 0.048/12)300 − 1]/(0.048/12) = 289, 022.42
On the other hand, suppose that you want to accumulate S dollars in savings, and would like to know how much money you should deposit D each deposit period. The periodic payment you need to reach this goal S is given by the formula
D = S(r/m) (1 + r/m)mt − 1
Design a class that uses the above formulas to calculate retirement savings and to help plan retirement goals. Your class should have private instance variables (all of type double) to hold D the deposit amount, m the number of deposit periods per year, r the interest rate and t the time in years the plan will save for retirement. In the class style guidelines, I discourage using single letter variable names usually when writing code. However when we are directly implementing a formula in an engineering or scientific calculation, it is often clearer to use the variable names directly as given in the formula.
For this assignment you need to perform the following tasks.
1. Create a class named RetirementAccount. The class should have a class constructor that takes the 4 variables, D, m, r, t as parameters.
2. Create a default class constructor as well. This constructor can set all of the private member variable values to 1.0.
3. You must have getter and setter methods for all 4 of the member variables of the class. The methods will be named set_D(), set_m(), set_r(), set_t() and get_D(), get_m(), get_r(), get_t(), respectively.
4. Create a member function named tostring(). This function will not take any parameters as input. It returns a string that represents the values/settings of the RetirementAccount class. See the example output below for the format of the string to be returned.
5. Create a member function named calculateRetirementSavings(). This function should implement the first formula to calculate the total amount of savings S the account will generate. This function has no parameters as inputs, and it returns a double amount (the amount of savings) as its result.
6. Create a member function named planRetirementDeposits(). This function takes a double parameters S as input, which is the savings goal you wish to achieve. This function should implement the second equation given above. The function returns a double result, D which is the amount of money that should be deposited each period to meet the savings goal S.
Complete Code for RetirementAccount class and for the DriverProgram added below.
The formula given in description contains invalid characters such as i, h. So I advice you to once check the formula in the question and make necessary changes to the formula in the code in RetirementAccount.cpp file.
Hit like if you liked the solution.
// main.cpp
#include"RetirementAccount.h"
using namespace std;
int main() {
RetirementAccount ra(500.0, 12, 4.80, 25);
cout<<ra.tostring()<<endl;
double S_final = 289022.42f;
double D =
ra.planRetirementDeposits(S_final);
cout<<"calculated monthly deposited needed
to save $"<<S_final<<" is "<<D<<endl;
double S =
ra.calculateRetirementSavings();
cout<<"Total Retirement savings calculated
for monthly deposite of "<<ra.get_D()<<" is
"<<S<<endl;
return 0;
}
// RetirementAccount.h
#ifndef __RETIREMENT_ACCOUNT__
#define __RETIREMENT_ACCOUNT__
#include<iostream>
#include<string>
using namespace std;
class RetirementAccount {
public:
RetirementAccount(double
D, double m, double r, double t);
RetirementAccount();
//getters
double get_D();
double get_m();
double get_r();
double get_t();
//setters
void set_D(double
D);
void set_m(double
m);
void set_r(double
r);
void set_t(double
t);
string
tostring();
double
calculateRetirementSavings();
double
planRetirementDeposits(double S);
private:
double D;
double m;
double r;
double t;
};
#endif /* __RETIREMENT_ACCOUNT__ */
// RetirementAccount.cpp
#include"RetirementAccount.h"
RetirementAccount::RetirementAccount(double D, double m, double
r, double t) {
this->D = D;
this->m = m;
this->r = r;
this->t = t;
}
RetirementAccount::RetirementAccount() {
RetirementAccount(1.0, 1.0, 1.0, 1.0);
}
//getters
double RetirementAccount::get_D() { return this->D; }
double RetirementAccount::get_m() { return this->m; }
double RetirementAccount::get_r() { return this->r; }
double RetirementAccount::get_t() { return this->t; }
//setters
void RetirementAccount::set_D(double D) { this->D = D; }
void RetirementAccount::set_m(double m) { this->m = m; }
void RetirementAccount::set_r(double r) { this->r = r; }
void RetirementAccount::set_t(double t) { this->t = t; }
string RetirementAccount::tostring() {
string st;
st = string("===== RetirementAccount =====\n")
+
" Deposit in $ :
"+to_string(get_D())+"\n" +
" No.
Months : "+to_string(get_m())+"\n" +
" Interest Rate:
"+to_string(get_r())+"%\n"+
" No. of years :
"+to_string(get_t())+"\n";
return st;
}
double RetirementAccount::calculateRetirementSavings() {
double S = D * (1 + (r/m)) * ((m*t) - 1) *
(r/m);
return S;
}
double RetirementAccount::planRetirementDeposits(double S)
{
double D = S * (r/m) * (1 + (r/m)) * ((m*t) -
1);
return D;
}
// OUTPUT