(E) Bank account

Goal: I will learn how to create a small class in C++ and how to use an instance of a class, i.e. an object as a parameter of a method.

Instructions: Retrieve the code template: templates/03/bank_account/ -> student/03/bank_account/.

(Function print_three_accounts in file main.cpp is not rational, but at this phase we do not know how to implement a function printing any number of bank accounts. The purpose of this function is to diminish the amount of repeated code and to make main function more clear.)

In this exercise, you will implement a program to handle different bank accounts. A bank account has a number (IBAN), owner, and balance. If a bank account has a credit card, it also has a credit limit. (In a credit card account, the balance can be negative as much as the credit limit is.)

You can save money in a bank account, you take money from it, and you can transfer money from it to another account. You can print information about a bank account in the form: owner : IBAN : balance. For a credit card account, you can set a credit limit.

In the code template, the class Account has a static attribute running_number_. A static attribute is not specific for an object (as usual attributes), for a class. In other words, its value is the same for all objects of the class, and if an object of the class changes the value, the new value is updated for all the other objects of the class. Such an attribute can be used to told the number of the objects of the class, and this is actually the case also in this program. The attribute in question is used in generating account numbers. For the first account created, the number is FI00 1234 01, for the second one it is FI00 1234 02, and so on. The Account class already has the method generate_iban, and thus, you need not care about these things any more. The call of the method generate_iban is already written in the code, as well as the initialization of the attribute running_number_.

The code template has a main program (in the file main.cpp). There you can see how the methods of Account have been called. In the class Account, implement all methods that have been called from the main program. Note that the Account class include the information about a single bank account.

In the main program, all methods have been called like void methods, i.e. methods without a return value. If you feel it useful to pass success/fail information for the caller, you can give a truth-value (bool) as the return value of such method. Now, the function can be called in both ways: as a method returning bool, or as a void method (if the caller is not interested in the return value).

After you have completed the class Account, the program will work with the given main program in the following way:

1: Creating three accounts
Cannot set credit limit: the account has no credit card

Printing all three accounts:
Tupu : FI00 1234 01 : 100 euros
Hupu : FI00 1234 02 : 200 euros
Lupu : FI00 1234 03 : 300 euros

2: Taking money succesfully:
50 euros taken: new balance of FI00 1234 01 is 50 euros

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 200 euros
Lupu : FI00 1234 03 : 300 euros

3: Taking money unsuccesfully:
Cannot take money: balance underflow

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 200 euros
Lupu : FI00 1234 03 : 300 euros

4: Taking money succesfully from a credit card account:
310 euros taken: new balance of FI00 1234 03 is -10 euros

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 200 euros
Lupu : FI00 1234 03 : -10 euros

5: Taking money unsuccesfully from a credit card account:
Cannot take money: credit limit overflow

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 200 euros
Lupu : FI00 1234 03 : -10 euros

6: Transferring money succesfully:
50 euros taken: new balance of FI00 1234 02 is 150 euros

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 150 euros
Lupu : FI00 1234 03 : 40 euros

7: Transferring money unsuccesfully:
Cannot take money: balance underflow
Transfer from FI00 1234 02 failed

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 150 euros
Lupu : FI00 1234 03 : 40 euros

8: Transferring money succesfully from a credit card account:
60 euros taken: new balance of FI00 1234 03 is -20 euros

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 210 euros
Lupu : FI00 1234 03 : -20 euros

9: Transferring money unsuccesfully from a credit card account:
Cannot take money: credit limit overflow
Transfer from FI00 1234 03 failed

Printing all three accounts:
Tupu : FI00 1234 01 : 50 euros
Hupu : FI00 1234 02 : 210 euros
Lupu : FI00 1234 03 : -20 euros

Tips for completing the assignment:

  • After you have inserted the declaration of the method in the file account.hh, go and test the property ”Refactor” -> ”Add Definition in account.cpp”. You can find it by right-clicking the method declaration.
  • The program will not compile in case the function main includes calls for methods you have not yet defined. If you want to be smart and implement your program in parts, you should first create a so-called stub of the class interface that only contains empty definitions of the methods with the return statements you might need. After doing this, you will naturally do a commit to the version control (the stub of the class interface is ready).
  • This exercise uses unit tests, which means that each method must separately work in a correct way. It is not enough that the program as a whole works correctly, i.e. prints the correct output. More precisely, print method takes care of actual printing. Other methods take care of an action that can be concluded from their name. If this action is not possible, they print an error message.
  • Think over what happens in transferring money. How to implement the method for this action in a very simple way (by calling the other methods of the class)?

A+ presents the exercise submission form here.