Question:
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the annual interest rate to some initial values (with defaults of zero).
The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or a withdrawal (subtract from the balance). Finally, there should be a function that adds interest to the balance at the current interest rate. This function should have a parameter indicating how many years’ worth of interest are to be added (for example, 0.5 years indicates that the account should have six months’ interest added).
Use the class as part of an interactive program that allows the user to determine how long an initial balance will take to grow to a given value. The program should allow the user to specify the initial balance, the interest rate, and whether there are additional yearly deposits.
My answer:
account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class account{
public:
account(){
balance = interest_rate = 0;
}
account(double, double);
void set_interest_rate(double);
void deposit(double);
void withdrawal(double);
void add_interest2balance(double);
double retrieve_interest_rate() const;
double retrieve_balance() const;
private:
double interest_rate, balance;// annual interest rate
};
#endif
account.cpp
#include "account.h"
#include <assert.h>
account::account(double a, double b){
assert(0 < b && b < 1);
balance = a;
interest_rate = b;
}
double account::retrieve_balance() const{
return balance;
}
double account::retrieve_interest_rate() const{
return interest_rate;
}
void account::set_interest_rate(double a){
assert(0 < a && a < 1);
interest_rate = a;
}
void account::deposit(double a){
assert(a >= 0);
balance += a;
}
void account::withdrawal(double a){
assert(a >= 0);
balance -= a;
}
void account::add_interest2balance(double years){
// double months = years * 12;
balance += (balance * interest_rate * years);
}
main.cpp
#include "account.h"
#include <iostream>
using namespace std;
void account_info(account);
void account_list();
int main(){
double balance, interest_rate;
cout << "Please input initial balance: ";
cin >> balance;
cout << "Please input initial annual interest rate: ";
cin >> interest_rate;
account a(balance, interest_rate);
int choice;
bool continued = true;
while(continued){
account_list();
cin >> choice;
switch (choice)
{
case 1:
double deposit;
cout << "Please input how much to deposit: ";
cin >> deposit;
a.deposit(deposit);
break;
case 2:
double withdrawal;
cout << "Please input how much to withdraw: ";
cin >> withdrawal;
a.deposit(withdrawal);
break;
case 3:
double years;
cout << "n years passed, please input the n (double): ";
cin >> years;
a.add_interest2balance(years);
break;
case 4:
double new_rate;
cout << "Please input new interest rate: ";
cin >> new_rate;
a.set_interest_rate(new_rate);
break;
case 5:
account_info(a);
break;
case 6:
continued = false;
break;
default:
cout << "Please input numbers between 1 and 6" << endl;
break;
}
}
return 0;
}
void account_info(account a){
cout << "****** account info ******" << endl;
cout << "balance = " << a.retrieve_balance() << endl;
cout << "interest = " << a.retrieve_interest_rate() << endl;
cout << "**************************" << endl;
}
void account_list(){
cout << "****** please choose ******" << endl;
cout << "1: deposit" << endl;
cout << "2: withdrawal" << endl;
cout << "3: n year(s) passed" << endl;
cout << "4: set interest rate" << endl;
cout << "5: account info" << endl;
cout << "6: exit" << endl;
cout << "***************************" << endl;
}
结果:
Please input initial balance: 2000
Please input initial annual interest rate: 0.1
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
1
Please input how much to deposit: 500
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
1
Please input how much to deposit: 300
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
2
Please input how much to withdraw: 100
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
3
n years passed, please input the n (double): 0.4
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
4
Please input new interest rate: 0.2
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
3
n years passed, please input the n (double): 2
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
5
****** account info ******
balance = 4222.4
interest = 0.2
**************************
****** please choose ******
1: deposit
2: withdrawal
3: n year(s) passed
4: set interest rate
5: account info
6: exit
***************************
6
Reference:
整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p125