//--------------------------------------------------------- // Implement and test a class that models an investment // earning compound interest. // // D. Searls // Asbury College // Oct 2006 //--------------------------------------------------------- #include #include #include using namespace std; class investment { public: //----------------------------------------------------- // Construct a new investment with default values: // Present Value: 1.0 // Nominal Annual Rate: 0.0 // Periods per Year: 1 // Term: 1 year //----------------------------------------------------- investment() { } //----------------------------------------------------- // Construct a new investment with the specified // state. // // Preconditions: // // The present value must be positive. If not, the // present value will be set to one. // // The nominal annual rate must be non-negative. If not, // the rate will be set to zero. // // The periods per year must be 1, 2, 4, or 12. If not, // the periods per year will be set to one. // // The number of years must positive. If not, the number // of years will be set to one. // // In parameters: pValue, r (in %), freq, term //----------------------------------------------------- investment(double pValue, double r, int freq, int term) { } //----------------------------------------------------- // Construct a new investment with the same state as // the specified investment. // // In parameter: invest //----------------------------------------------------- investment(const investment& invest) { } // Member functions //----------------------------------------------------- // Set the present value to the specified value. // // Precondition: newPV > 0. If newPV <= 0 this // function does nothing. // // In Parameter: newPV //----------------------------------------------------- void setPV(double newPV) { } //----------------------------------------------------- // Set the nominal annual rate to the specified value. // // Precondition: newRate >= 0. If newRate < 0 this // function does nothing. // // In Parameter: newRate //----------------------------------------------------- void setRate(double newRate) { } //----------------------------------------------------- // Set the compounding frequency to the specified value. // // Precondition: newFreq is 1, 2, 4, or 12. Otherwise, // this function does nothing. // // In Parameter: newFreq //----------------------------------------------------- void setFreq(int newFreq) { } //----------------------------------------------------- // Set the number of years to the specified value. // // Precondition: newYears > 0. If newYears <= 0 this // function does nothing. // // In Parameter: newYears //----------------------------------------------------- void setYears(int newYears) { } //----------------------------------------------------- // Return the present value. //----------------------------------------------------- double getPV() const { } //----------------------------------------------------- // Return the nominal annual rate as a percent. //----------------------------------------------------- double getRate() const { } //----------------------------------------------------- // Return the compounding frequency. //----------------------------------------------------- int getFreq() const { } //----------------------------------------------------- // Return the number of years. //----------------------------------------------------- int getYears() const { } //----------------------------------------------------- // Return the future value. //----------------------------------------------------- double getFV() const { } //----------------------------------------------------- // Return the total interest earned. //----------------------------------------------------- double getInterest() const { } //----------------------------------------------------- // Return the annual percentage yield as a percent. //----------------------------------------------------- double getAPY() const { } private: double pv; // Present value double rate; // Nominal annual rate int ppy; // Periods per year int years; // Term in years }; //--------------------------------------------------------- // displayRecord // // Display an investment record. // // In Parameter: data //--------------------------------------------------------- void displayRecord(investment data) { cout << fixed; cout << setw(10) << setprecision(2) << data.getPV(); cout << setw(8) << data.getRate() << '%'; if (data.getFreq() == 1) { cout << " Annually "; } else if (data.getFreq() == 2) { cout << " Semiannually"; } else if (data.getFreq() == 4) { cout << " Quarterly "; } else { cout << " Monthly "; } cout << setw(7) << data.getYears(); cout << setw(12) << data.getFV(); cout << setw(12) << data.getInterest(); cout << setw(8) << setprecision(3) << data.getAPY() << '%'; cout << endl; } //********************************************************* // M A I N D R I V E R //********************************************************* int main() { investment invest1; investment invest2(1000.0, 4.0, 4, 10); investment invest3(0.0, -1.0, 1, 0); investment invest4(invest2); cout << " Present Nominal Compounding Future Total\n"; cout << " Value Rate Frequency Years Value Interest APY\n"; cout << "---------- ------- ------------ ----- ---------- ---------- -------\n"; displayRecord(invest1); displayRecord(invest2); displayRecord(invest3); displayRecord(invest4); invest1.setPV(2000.0); invest1.setRate(6.25); invest1.setFreq(12); invest1.setYears(20); displayRecord(invest1); invest1.setPV(0); invest1.setRate(-0.1); invest1.setFreq(0); invest1.setYears(0); displayRecord (invest1); return 0; }