Project Requirements
Create a new Eclipse workspace named "Project_1234567890" on the desktop of your computer (replace
1234567890 with your student ID number). For each question below, create a new project in that workspace. Call
each project by its question number: "Question1", "Question2", etc. If you do not remember how to create a
workspace or projects, read the "Introduction to Eclipse" document which is on iSpace. Answer all the questions below.
At the end of the project, create a ZIP archive of the whole workspace folder. The resulting ZIP file must be called
"Project_1234567890.zip" (replace 1234567890 with your student ID number). Upload the ZIP file on iSpace.
Make sure your code and your tests all have comments explaining what you are doing. Make sure all your code is
properly indented (nicely formatted). Make sure all your tests print true. Otherwise you will lose points.
Question 1
In this project you need to write software for a bank company. The bank has many bank accounts for different
customers. There are two types of bank accounts: credit accounts and student accounts.
Write an IAccount interface with the following UML specification:
+----------------------------------+
| > |
| IAccount |
+----------------------------------+
| + getName(): String |
| + getMoney(): int |
| + withdraw(int amount): void |
+----------------------------------+
and an Account class that implements IAccount and has the following UML specification:
+-----------------------------------+
| Account |
+-----------------------------------+
| - name: String |
| - money: int |
+-----------------------------------+
| + Account(String name, int money) |
| + getName(): String |
| + getMoney(): int |
| = setMoney(int money): void |
| + withdraw(int amount): void |
| + testAccount(): void |
+-----------------------------------+
The name instance variable indicates the name of the customer for the bank account. The money instance variable
indicates the amount of money which is currently available in the account (to simplify the assignment we will assume
that all amounts of money are always integers).
The setMoney method is protected, not public. This means that only subclasses of the Account class can use
the setMoney method. All the other classes in the software cannot use the setMoney method, so they cannot
change the amount of money in an account. This is good for the security of the banking software you are writing!
The purpose of the withdraw method is to withdraw (subtract) the amount of money given as argument to the
method from the amount of money currently stored in the account. The withdraw method of the Account class is
abstract.
Also add to your program a Test class to test your Account class.
Question 2
Add a class CreditAccount that extends Account. The constructor of the CreditAccount class takes a name
and an amount of money as arguments.
The withdraw method of the CreditAccount class simply subtracts the amount of money given as argument to the
method from the amount of money currently stored in the account. A credit account is allowed to have a negative
amount of money in the account, so money can be withdrawn from a credit account even if this makes the amount of
money in the credit account become negative.
Make sure you test your new CreditAccount class.
Question 3
Add a class StudentAccount that extends Account. The constructor of the StudentAccount class takes a name
and an amount of money as arguments. If the amount of money given as argument is strictly less than zero then the
constructor must throw a NotEnoughMoneyException with the message "Cannot create student
account with negative amount of money".
The withdraw method of the StudentAccount class subtracts the amount of money given as argument to the
method from the amount of money currently stored in the account. A student account is not allowed to have a negative
amount of money in the account, so money can be withdrawn from a student account only if the amount of money in
the student account will remain positive. If the amount to withdraw is too big then the amount of money in the student
account must not change and the withdraw method must throw a NotEnoughMoneyException with the
message "Cannot withdraw XXX yuan from account, only YYY yuan is available", where XXX
is replaced by the amount of money that was given as argument to the withdraw method and YYY is replaced by the
amount of money currently in the student account.
Change other classes and interfaces as necessary.
Make sure you test your new StudentAccount class.
Question 4
Add a Bank class with the following UML specification:
+-------------------------------------------+
| Bank |
+-------------------------------------------+
| - name: String |
| - accounts: ArrayList |
+-------------------------------------------+
| + Bank(String name) |
| + addAccount(IAccount account): void |
| + totalMoney(): int |
| + getMoney(String name): int |
| + withdraw(String name, int amount): void |
| + testBank(): void |
+-------------------------------------------+
When a bank is created, it has an arraylist of accounts but the arraylist is empty (the arraylist does not contain any bank
account).
The addAccount method takes an account as argument and adds the account to the arraylist of accounts for the bank.
The totalMoney method returns as result the total amount of money in all the bank accounts of the bank.
The getMoney method takes as argument the name of a customer and returns as result the amount of money
currently stored in the bank account that belongs to that customer. If the customer does not have a bank account in the
bank then the getMoney method must throw an UnknownCustomerException with the message "Customer
XXX unknown", where XXX is replaced with the name of the customer.
The withdraw method takes as argument the name of a customer and an amount of money and withdraws that
amount of money from the amount of money currently stored in the bank account that belongs to that customer. If the
customer does not have a bank account in the bank then the withdraw method must throw an
UnknownCustomerException with the message "Customer XXX unknown", where XXX is replaced with the
name of the customer.
Hint: use the equals method to compare strings, not the == operator which only works with constant strings.
Make sure you test your new Bank class.