We are going to build a simulation of bank accounts. There are two types of bank accounts: cheque accounts and savings accounts. With a cheque account, you can deposit money, withdraw money and transfer money to another account. Your cheque account allows you to be overdrawn up to a maximum amount. This is set when you create the account and cannot be changed.
A savings account is similar to a cheque account but has a number of restrictions. You can deposit money into the savings account, but you cannot withdraw money from your savings account. You can transfer money, but only from your savings account to one of the approved cheque accounts. In addition, you will receive interest on your savings account every year. When you create your savings account you specify how much interest you will receive annually and you are not allowed to be in the red on your savings account!
We now want to build classes for the cheque and savings accounts. Since there are a number of attributes and methods that are used in both classes it might be useful to create a common superclass in which these correspondences are stored.
So far we have always used a variable of type double
to store money amounts, but actually this is not the right way to do this. This is because doubles might run into the problem of so-called rouding errors. For example, just run the code System.out.println(0.1 + 0.2);
and you will quickly see what we mean. Because when working with money you may sometimes want to do some extra operations (e.g. with conversions between money from different countries) there are also separate libraries available nowadays that make dealing with money amounts easier. In this course, we will keep it simple and store money amounts (in cents) in
int variables from now on to avoid the rounding problems as shown previously. So in this assignment, assume that the number
500 is actually 5 euros (because 500 cents) and 100 euros will be represented with the int value
10000`.
Pay attention to the following points when implementing your code:
deposit(int amount)
, withdraw(int amount)
and transfer(int amount, BankAccount otherAccount)
.addYearlyInterest()
method at the savings account that adds the defined amount of interest (in %) to the account.toString()
methods to clearly print the status of the accounts.deposit(..)
, withdraw(..)
and transfer(..)
methods.Feel free to use our main method!
public static void main(String[] args) {
// Accounts for person 1
ChequeAccount chequePerson1 = new ChequeAccount("NL02ABNA0123456789", 50000);
SavingsAccount savingPerson1 = new SavingsAccount("NL03ABNA9876543210", 3.5);
// Register that there is a relationship between the saving account and the checking account
savingPerson1.addChequeAccount(chequePerson1);
// Accounts for person 2
ChequeAccount cheque1Person2 = new ChequeAccount("NL02RABO0111111111", 100000);
ChequeAccount cheque2Person2 = new ChequeAccount("NL02RABO0222222222", 0);
SavingsAccount savingPerson2 = new SavingsAccount("NL02RABO0333333333", 2.0);
// Register that there is a relationship between the saving account and the checking accounts
savingPerson2.addChequeAccount(cheque1Person2);
savingPerson2.addChequeAccount(cheque2Person2);
// Deposit some money to each account
chequePerson1.deposit(10000);
cheque1Person2.deposit(30000);
cheque2Person2.deposit(25000);
// Print state
System.out.println(chequePerson1);
System.out.println(savingPerson1);
System.out.println(cheque1Person2);
System.out.println(cheque2Person2);
System.out.println(savingPerson2);
System.out.println();
// Withdraw some money from the accounts and transfer also some money
chequePerson1.withdraw(5000);
cheque1Person2.withdraw(20000);
cheque2Person2.transfer(25000, savingPerson2);
savingPerson2.transfer(2000, cheque1Person2);
savingPerson2.addYearlyInterest();
// Print state
System.out.println(chequePerson1);
System.out.println(savingPerson1);
System.out.println(cheque1Person2);
System.out.println(cheque2Person2);
System.out.println(savingPerson2);
}
The code above should produce the following output:
[Cheque] IBAN: NL02ABNA0123456789, balance: €100,00
[Saving] IBAN: NL03ABNA9876543210, balance: €0,00
[Cheque] IBAN: NL02RABO0111111111, balance: €300,00
[Cheque] IBAN: NL02RABO0222222222, balance: €250,00
[Saving] IBAN: NL02RABO0333333333, balance: €0,00
[Cheque] IBAN: NL02ABNA0123456789, balance: €50,00
[Saving] IBAN: NL03ABNA9876543210, balance: €0,00
[Cheque] IBAN: NL02RABO0111111111, balance: €120,00
[Cheque] IBAN: NL02RABO0222222222, balance: €0,00
[Saving] IBAN: NL02RABO0333333333, balance: €234,60
If you think you are done, please execute the test methods given in the TestApplication (click to download). Repair your code if some of the tests are failing.