// // Example 5.19. This program computes the amount of money in // the bank after a certain number of years. It also computes // how many years it would take to arrive at a given amount. // The bank compounds interest annually. // #include #include using namespace std; #include "Money.h" Money readPrincipal(); float readRate(); int main() { int menu(); // prototypes void findAmount(); void findYears(); void error(int choice); int choice; // menu choice choice = menu(); switch (choice) { case 1: findAmount(); break; case 2: findYears(); break; default: error(choice); } return 0; } // // This function prints a menu of choices, reads and // returns the user's integer choice, 1 or 2; all other // choices are in error. // Pre: none // Post: Function has returned user's choice from the menu. // int menu() { int choice; // menu choice cout << "This program makes savings computations for an" << endl; cout << "account with interest compounded annually. It will" << endl; cout << "ask you the one-time deposit amount and the annual" << endl; cout << "interest rate. You have two choices: Given a number" << endl; cout << "of years, the program will return the final amount;" << endl; cout << "given a final amount, the program will tell the" << endl; cout << "number of years you must leave the money in the bank." << endl; cout << "Choose from the following: " << endl << endl; cout << "1. Given a number of years, calculate amount" << endl; cout << "2. Given a desired amount, calculate years" << endl << endl; cout << "Your choice: "; cin >> choice; return choice; } // // This function obtains principal, interest rate, and years of // deposit; determines if an input error occurs; and if // not, calculates and prints final amount. // amount = principal * (1 + rate)^years // Pre: none // Post: If no errors, final amount has been displayed. // void findAmount() { int readYears(); // prototype float rate; // interest rate int years; // number of years in bank Money principal; // object for original deposit Money zeroDollars; // object representing $0.00 principal = readPrincipal(); rate = readRate(); years = readYears(); if (principal.lessThan(zeroDollars) || principal.equalTo(zeroDollars) || (rate <= 0) || (years <= 0) ) //error cout << endl << "Invalid input. Run program again." << endl; else { principal.multiplyBy(pow(1 + rate, years)); cout << endl << "You will have $"; principal.display(); cout << " in the bank." << endl; } } // // This function obtains principal, interest rate, and amount // desired; determines if an input error occurs; and if // not, calculates and prints number of years needed. // years = smallest integer greater than or equal to // log(amount/principal)/log(1 + rate) // Pre: none // Post: The number of years needed or an error message // has been displayed. // void findYears() { Money readAmount(void); // prototype float rate; // interest rate int years; // number of years in bank Money amount; // final amount in bank Money principal; // original deposit Money zeroDollars; // object representing $0.00 principal = readPrincipal(); rate = readRate(); amount = readAmount(); if ((!principal.lessThan(amount)) || principal.equalTo(zeroDollars) || principal.lessThan(zeroDollars) || (rate <= 0)) //error cout << endl << "Invalid input. Run program again" << endl; else { years = ceil(log(amount.quotient(principal)) / log(1 + rate)); cout << endl << "You will need the account for " << years << " years." << endl; } } // // This function reads and returns principal // Pre: none // Post: The Money object for the principal was returned. Money readPrincipal() { double deposit; // principal as a double cout << endl << "Give the original deposit: "; cin >> deposit; Money principal(deposit); // original deposit object return principal; } // // This function reads and returns interest rate, -1 in // case of error // Pre: none // Post: Floating point interest rate has been returned, -1 // in case of error. // float readRate() { float rate; // interest rate cout << "Give the interest rate as a decimal number" << endl; cout << " For example, for 6.25% type 0.0625: "; cin >> rate; if ((rate <= 0.0) || (rate >= 0.2)) // error rate = -1; return rate; } // // This function reads and returns years of deposit // Pre: none // Post: Years of deposit have been returned. // int readYears() { int years; // number of years in bank cout << "Number of years to leave in bank: "; cin >> years; return years; } // // This function reads and returns the amount desired // Pre: none // Post: The Money object for the amount desired was returned. // Money readAmount() { double desired; // amount desired cout << "Give the amount desired: "; cin >> desired; Money amount(desired); // object for amount desired return amount; } // // This function prints an error message for an invalid // menu choice // Pre: choice has an invalid value. // Post: Error message is displayed. // void error(int choice) { cout << choice << " is an invalid choice. Please run again." << endl; }