In C++ 1. Create a class named SavingsAccount 2. The class should be defined in
ID: 3837642 • Letter: I
Question
In C++
1. Create a class named SavingsAccount
2. The class should be defined in its own header file and have at least the following data members and functions:
Data Members
- accountOfficer, pointer to an object of class Name to represent the account officer who is responsible for the account.
- oDate, pointer to an object of class Date to represent the date that the account was opened.
- annualInterestRate, data member to represent the interest currently on the account. This value should be the same for all objects created.
- customer, an object of type Customer to represent the Customer of this account.
-accountNumber, data member to represent the account number of the account.
-balance, data member to represent the current balance of the account.
Data members shared by all objects of the class:
- activeOfficer, an object of class Name to represent the current account officer who is creating accounts
- cdate, an object of class Date to represent today's date
Note that this class is composed of several classes that have already been built namely Name, Date and Customer. You should not need to make any changes to these classes and should set-up a symbolic link to the necessary files. If you do not have working versions of these embedded classes use primitive types as data members (i.e. string to represent customer name)
Member functions:
- Default Constructor(), this constructor should initialize that data members appropriately
- Constructor(), this constructor should initialize that data members customer, savingsBalance, and accountNumber to the corresponding values passed to it.
Note that the data members, accountOfficer, and oDate should be initialized to the activeOfficer and tDate objects respectively.
- modifyInterestRate(), this function should modify the value of the data member annualInterestRate to the value of the parameter passed to it.
- updateSavingsBalance(), this function should calculate the monthly interest and update the savings balance data member with the interest results
- Any other methods as necessary
3. Write a class source code implementation file that contains any necessary declaration of the static variable in global space and code implemenation of all methods in the class.
4. Write a driver program to test this class. The program should:
- Prompt you to enter the current Account Officer on duty.
- Prompt you to enter today's date
- Prompt you to enter current interest rate.
- Create an array of pointers for up to some maximum number of accounts that can be created.
- Allow for the program to continuosly create new accounts (up to maximum) requesting the appropriate information each time.
5. Once all the accounts have been created, the program should:
- invoke a local function accumulateInterest() whose purpose is to update savings balnce for each account created for one year (e.g. 12 months) of savings. This function should take as an argument the array of accounts and accumulate teh interest for one year (i.e. 12 times) on each object of the array.
- invoke a local function to output a blance summary for each account.
Sample Run:
Current Account Officer: John Williams
Current Date: 11/14/2018
Current annual interest rate: 5%
Enter Customer name: Ted Johnson
Enter Starting balance: 10000.0
Enter Customer name: Raul Lopez
Enter Starting balance: 100.0
Enter Customer Name: -
Accumulating Interest for one year...
Account Officer Date Opened Customer Current Balance
John Williams 11/14/2018 Ted Jones $105000.00
John Williams 11/14/2018 Raul Lopez $105.0
Explanation / Answer
main.cpp
#include <iostream>
#include "savingsAccount.h"
#include <iomanip>
using namespace std;
int main( ) {
//declare local variables
int MAX_ACCOUNTS = 3;
int numAccounts = 0;
void display(SavingsAccount *[ ], const int);
//call SavingsAccount static functions to input static data members
SavingsAccount::inputActiveOfficer( );
SavingsAccount::inputCurrentDate( );
SavingsAccount::inputInterestRate( );
cout << endl;
//array of SavingsAccount pointers
SavingsAccount *ptrS[MAX_ACCOUNTS];
//enter account information
for (int i = 0; i < MAX_ACCOUNTS; i++) {
ptrS[i] = new SavingsAccount;
(*ptrS[i]).inputCustomerName( );
(*ptrS[i]).inputStartingBalance( );
cout << endl;
numAccounts++;
}
display(ptrS, MAX_ACCOUNTS);
return 0;
}
//function to calculate interest, update the savings balance, and display the information
//for all of the account entered
void display(SavingsAccount *ptrS[ ], const int MAX_ACCOUNTS) {
cout << "Account Officer Date Opened Customer Current Balance" << endl;
for (int i = 0; i < MAX_ACCOUNTS; i++) {
ptrS[i] -> calculateInterest( );
ptrS[i] -> updateSavingsBalance( );
ptrS[i] -> display( );
delete ptrS[i];
ptrS[i] = NULL;
cout << endl;
}
}
date.cpp
#include "date.h"
//default constructor
Date::Date( ) {
month = day = year = 0;
}
//constructor to set all data members
Date::Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}
//setDate, checks for valid date
bool Date::setDate(int m, int d, int y ) {
bool setValue = false;
int maxDays;
//determine the maximum amount of days in every month
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
maxDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
maxDays = 30;
break;
case 2:
maxDays = 28;
}
if (month >= 1 && month <= 12 && day > 0 && day <= maxDays) {
setValue = true;
month = m;
day = d;
year = y;
}
return(setValue);
}
//inputDate, gets user input and checks validity
void Date::inputDate( ) {
char dummyChar;
do {
cout << "Input date in the form mm/dd/yyyy: ";
cout >> month >> dummyChar >> day >> dummyChar >> year;
} while (!setDate(month, day, year));
}
//getMonth, accessor method for month data member
int Date::getMonth( ) const {
return(month);
}
//getDay, accessor method for day data member
int Date::getDay( ) const {
return(day);
}
//getYear, accessor method for year data member
int Date::getYear( ) const {
return(year);
}
//display function
void Date::display( ) const {
cout << month << '/' << day << '/' << year;
}
date.h
#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std;
class Date {
public:
//constructors
Date( );
Date(int, int, int);
//mutator methods
bool setDate(int, int, int);
void inputDate( );
//accessor methods
int getMonth( ) const;
int getDay( ) const;
int getYear( ) const;
//display
void display( ) const;
private:
int month, day, year;
};
#endif
name.cpp
#include "name.h"
//default constructor
Name::Name( ) {
fname = "First";
mi = 'I';
lname = "Last";
}
//constructor for name
Name::Name(string f, char m, string l) {
fname = f;
mi = m;
lname = l;
}
//mutator method
void Name::setName(string f, char m, string l) {
fname = f;
mi = m;
lname = l;
}
//populate data with user input
void Name::inputName( ) {
cout << "Input name in the format First MI Last: ";
cin >> fname >> mi >> lname;
}
//accessor method for first name
string Name::getFirstName( ) const {
return(fname);
}
//accessor method for middle initial
char Name::getMiddleInitial( ) const {
return(mi);
}
//accessor method for last name
string Name::getLastName( ) const {
return(lname);
}
//display method
void Name::display( ) {
cout << "Name: " << fname << ' ' << mi << ' ' << lname;
}
name.h
#ifndef NAME_H
#define NAME_H
#include <iostream>
using namespace std;
class Name {
public:
//constructors
Name( );
Name(string, char, string);
//mutator methods
void setName(string, char, string);
void inputName( );
//accessor methods
string getFirstName( ) const;
char getMiddleInitial( ) const;
string getLastName( ) const;
//display method
void display( );
private:
string fname, lname;
char mi;
};
#endif
savingsAccount.cpp
#include "date.h"
#include "name.h"
#include "savingsAccount.h"
#include <iomanip>
using namespace std;
Name SavingsAccount::activeOfficer;
double SavingsAccount::annualInterestRate;
Date SavingsAccount::cDate;
//default constructor
SavingsAccount::SavingsAccount( ) {
annualInterestRate = 0;
accountNumber = 0;
savingsBalance = 0;
accountOfficer = activeOfficer;
oDate = cDate;
}
//constructor to initialize customer, savingsBalance, and accountNumber
SavingsAccount::SavingsAccount(string f, char m, string l, double b, int a) {
customer.setName(f, m, l);
savingsBalance = b;
accountNumber = a;
accountOfficer = activeOfficer;
oDate = cDate;
}
//mutator method inputActiveOfficer, accepts user input for activeOfficer
void SavingsAccount::inputActiveOfficer( ) {
cout << "Current Active Officer, ";
activeOfficer.inputName( );
}
//mutator method inputCurrentDate, accepts user input for current date
void SavingsAccount::inputCurrentDate( ) {
cout << "Current Date, ";
cDate.inputDate( );
}
//mutator method inputInterestRate, accepts user input for static data member
//annualInterestRate
void SavingsAccount::inputInterestRate( ) {
double i;
cout << "Current Annual Interest Rate: ";
cin >> annualInterestRate;
}
//mutator method, input customer name
void SavingsAccount::inputCustomerName( ) {
cout << "Customer Name, ";
customer.inputName( );
}
//mutator method, input starting balance
void SavingsAccount::inputStartingBalance( ) {
cout << "Enter Starting Balance: ";
cin >> savingsBalance;
}
//calculateInterest, calculates and returns monthly interest
double SavingsAccount::calculateInterest( ) {
double interest;
interest = savingsBalance * (annualInterestRate/12.0);
return(interest);
}
//modifyInterestRate, modifies the static data member interest rate
void SavingsAccount::modifyInterestRate(double i) {
annualInterestRate = i;
annualInterestRate /= 100;
}
//updateSavingsBalance, calculates the monthly interest rate by invoking
//calculateInterest and updates the savings balance data member with
//the interest results
void SavingsAccount::updateSavingsBalance( ) {
double interest;
interest = calculateInterest( );
savingsBalance += interest;
}
//accessor methods
double SavingsAccount::getInterestRate( ) {
return(annualInterestRate);
}
//display method
void SavingsAccount::display( ) {
accountOfficer.display( );
cout << ' ';
oDate.display( );
cout << ' ';
customer.display( );
cout << ' ';
cout << std::fixed << setprecision(2) << '$' << savingsBalance;
}
savingsAccount.h
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
//check
#include "date.h"
#include "name.h"
class SavingsAccount {
public:
//constructors
SavingsAccount( );
SavingsAccount(string, char, string, double, int);
//mutator methods for static data members
static void inputActiveOfficer( );
static void inputCurrentDate( );
static void inputInterestRate( );
//mutator methods continued
void inputCustomerName( );
void inputStartingBalance( );
//calculate interest
double calculateInterest( );
//mutator method for annualInterestRate
static void modifyInterestRate(double);
void updateSavingsBalance( );
//accessor methods
static double getInterestRate( );
//display method
void display( );
private:
Name accountOfficer;
Date oDate;
static double annualInterestRate;
Name customer;
int accountNumber;
double savingsBalance;
static Name activeOfficer;
static Date cDate;
};
#endif