Thread: Seeking Critique about my prog!

  1. #1
    Yevmaster
    Guest

    Question Seeking Critique about my prog!

    Hello and thanks for opening this post.

    I was doing an exercise out of a C++ book asking to create sa SavingsAccount class. Now, the point of program was to practice on static data members, and I got that to work flawlessly.

    However, I can see my program is inflexible in the way it calculates monthly interest. Long story short, to calculate new monthly interest rate, I need a new balance every time.

    The way I choose to do this is to find the new savings balance in main() and then pass it as an argument into calculateMonthlyInterest() member function in SavingsAccount class. That way, new savings balance is calculated off the new balance I pass from main().

    I can see this method of finding next month's savings balance is inefficient because balance argument to calculateMonthlyInterest must change every time in main() into a new savings balance. I can also see that to find savings balance after suppose the 6th month, I have to find savings balance for every other month before it so I can pass the correct argument for balance.

    I am interested what you guys have to say on how I should solve this dilemma. My code is below:

    SavingsAccount.h // class definition
    Code:
     
    // dynamic_mem.h
    // SavingsAccount class definition
    
    #ifndef SAVINGSACCOUNT_H
    #define SAVINGSACCOUNT_H
    
    class SavingsAccount {
    
    public:
       SavingsAccount(double); // constructor
       ~SavingsAccount(); // destructor
        
       // set function
       void setBalance(double); 
       
       // get functions
       double getBalance() const;
       double calculateMonthlyInterest(double); 
       // passes old savings value as argument
       // returns new Savings Balance
       
       // static member function
       static void modifyInterestRate(double); 
       // send user-entered annual interest rate as argument
       
    private:
       // static data member
       static double annualInterestRate;
       
       double savingsBalance;
       double balance;
     
    };
    
    #endif
    SavingsAccount.cpp // Member function definitions

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    #include "SavingsAccount.h" // SavingsAccount class definition
    
    // define and initialize static data member
    double SavingsAccount::annualInterestRate = 0.00;
    
    // define static member function that sets annualInterestRate to a user-passed value
    void SavingsAccount::modifyInterestRate(double rate) {
       annualInterestRate = rate;
    }
    
    SavingsAccount::SavingsAccount(double bal) { // user passes initial balance as argument of object
       setBalance(bal); 
       calculateMonthlyInterest(bal); // finds monthly interest of balance and adds it to "total" savings balance
    }
    
    void SavingsAccount::setBalance(double bal) { 
       balance = bal;
    }
    
    double SavingsAccount::getBalance() const {
       return balance;
    }
    
    double SavingsAccount::calculateMonthlyInterest(double balance) {
    
       double monthlyInterest = (balance * annualInterestRate) / 12; // calculate monthly interest
     
       savingsBalance = balance + monthlyInterest;
       
       return savingsBalance;
    }
    
    SavingsAccount::~SavingsAccount() {
       annualInterestRate = 0.00; // set static data member to consistent state it was in
    }
    main.cpp // driver to test Class SavingsAccount

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::fixed;
    
    #include <iomanip>
    
    using std::setw;
    using std::setprecision;
    
    #include "SavingsAccount.h" // SavingsAccount class definition
    
    int main()
    {
       SavingsAccount::modifyInterestRate(.03); //change annual interest rate to 3%
       
       SavingsAccount saver1(2000.00); // set initial balances of saver1 and saver2
       SavingsAccount saver2(3000.00);
       double savings_1 = saver1.calculateMonthlyInterest(2000.00);
       double savings_2 = saver2.calculateMonthlyInterest(3000.00);
       
       cout << "After 1st month, saver1 with an initial balance of " << setprecision(2) 
            << fixed << saver1.getBalance() << " has a new \nsavings balance of " 
            << savings_1 << endl;
       
       cout << "After 1st month, saver2 with an initial balance of " << saver2.getBalance() 
            << " has a new \nsavings balance of " << savings_2 << endl;
            
       SavingsAccount::modifyInterestRate(.04); //change interest rate to 4%
       
       cout << "After 2nd month, saver1 with a previous balance of " << savings_1
            << " has a new \nsavings balance of " << saver1.calculateMonthlyInterest(savings_1) << endl;
       
       cout << "After 2nd month, saver2 with a previous balance of " << savings_2
            << " has a new \nsavings balance of " << saver2.calculateMonthlyInterest(savings_2) << endl;
                    
       return 0;
    }
    Thank you for your help in advance.

    Justin

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Look for a formula on calculating interest compunded monthly. I can't remember it off the top of my head.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Compound Interest Formula

    A(t) = p * ( 1 + r/n )^ (nt)
    where p=the principle/initial deposit
    r=the yearly rate
    n=the number of times compounded per year
    t=the number of years passed



    Continuous Compound Formula

    A(t) = p*e^(rt)
    p=principle/initial deposit
    r=yearly rate
    t=years passed

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Question Corrected program, would this be wrong?

    Hello,
    This is original thread starter, Yevmaster, just using my registered name. I've corrected my program without using
    compound interest formula you guys have suggested.

    I have altered the "calculateMonthlyInterest()" member function to work as a recursive function instead of sending a new Savings balance from main() into the function as an argument that takes the value of a new balance.

    Below is simply the member function I changed. Notice the user must pass the initial balance and the month for which he wants to know his interest from main().

    Code:
    double SavingsAccount::calculateMonthlyInterest(double balance, int month) {
    
       double monthlyInterest = (balance * annualInterestRate) / 12; // calculate monthly interest
    
       if (month == 0)
          return savingsBalance;
       else {
          savingsBalance = balance + monthlyInterest;
          return calculateMonthlyInterest(savingsBalance,month-1);
       }
    }

    1) I realize the annual interest rate does not change in this function. Should it change during course of year? If so, how many times per year do banks typically change their annual interest rate?

    2) Is my recursive function partially, completely, or not at all doing the same thing as the compound interest formula? If partially, could you please add in the part it is lacking into my code? If none at all, is it even possible to make a function simulating the compound interest formula? If so, how would it work?

    I appreciate your responses to my questions. Thank you very much.

    Justin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Aeres seeking interested hoobyists!
    By Akkernight in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 03-19-2009, 11:50 AM
  2. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 02:58 AM
  3. YangHui triangle print test prog?
    By toysoldier in forum C++ Programming
    Replies: 8
    Last Post: 08-20-2004, 08:46 AM
  4. Idea: Elevator Prog
    By gamer4life687 in forum Contests Board
    Replies: 0
    Last Post: 11-15-2002, 10:01 PM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM