C++: One interest rate for every account

Some settings belong to a whole class rather than to one object. Model a bank whose interest rate applies to everyone.

What to build

An Account class with a static interest rate, a static count, and commands that open accounts and apply a year of interest.

Requirements

  • Keep the rate and the number of accounts as static members, with static member functions to set and read them.
  • open NAME AMOUNT opens an account. rate R sets the rate in percent. year adds one year of interest to every account.
  • show prints every account in the order it was opened, with two decimals.
  • After the last command, print accounts N.

Input

The first line has q. Each of the next q lines is a command.

Output

For each show, one line per account: NAME BALANCE. Then the final accounts N line.

Constraints

1 ≤ q ≤ 100, amounts are from 0 to 1000000, and rates are from 0 to 100. The rate starts at 0.

Example

Input:

5
open Asha 1000
open Bilal 500
rate 10
year
show

Output:

Asha 1100.00
Bilal 550.00
accounts 2

Why it works

Every account reads the same static rate, so changing it once changes it for all of them. A static member function can be called through the class with no account at all.

Examples

Example 1 · Input
5
open Asha 1000
open Bilal 500
rate 10
year
show
Expected output
Asha 1100.00
Bilal 550.00
accounts 2
Example 2 · Input
3
open X 100
year
show
Expected output
X 100.00
accounts 1

Hints

Hint 1

`static double rate;` is shared by all accounts, so one `rate` command changes it for all of them.

Hint 2

A static `int count` that the constructor increases gives the number of accounts opened.

Hint 3

`year` loops over every account and adds `balance * rate / 100` to it.

Solution

Show one correct solution (try it yourself first)
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

class Account {
    static double rate;
    static int count;
    string owner;
    double balance;
public:
    Account(string o, double b) : owner(o), balance(b) {
        count++;
    }
    static void setRate(double r) { rate = r; }
    static int howMany() { return count; }
    void addYear() {
        balance += balance * rate / 100;
    }
    void show() const {
        cout << owner << " " << balance << endl;
    }
};

double Account::rate = 0;
int Account::count = 0;

int main() {
    int q;
    cin >> q;
    vector<Account> accounts;
    cout << fixed << setprecision(2);
    while (q-- > 0) {
        string cmd;
        cin >> cmd;
        if (cmd == "open") {
            string name;
            double amount;
            cin >> name >> amount;
            accounts.push_back(Account(name, amount));
        } else if (cmd == "rate") {
            double r;
            cin >> r;
            Account::setRate(r);
        } else if (cmd == "year") {
            for (Account& a : accounts) a.addYear();
        } else {
            for (const Account& a : accounts) a.show();
        }
    }
    cout << "accounts " << Account::howMany() << endl;
    return 0;
}

Lesson: Static and const members →