Keep a bank account balanced

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值