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.
Question 5
In this question and the next one we want to create a textual user interface (TUI) for our banking software.
Add a TUI class with a main method. Your code then has two classes with a main method: the Test class that you
can use to run all your tests for all your classes, and the TUI class that you will now use to run the interactive text-based
user interface of your program.
The TUI class does not have any testTUI method because this class is only used to allow users to use the software
interactively.
In addition to the main method, the TUI class has two methods called readLine and readPosInt.
The readLine method is static and private, it takes a string as argument, and returns as result a string. The
readPosInt method is static and private, it takes a string as argument, and returns as result a positive integer.
The readLine method uses System.out.print (not println) to print its string argument on the screen (later
when we use the readLine method, the string argument of the method will be a message telling the user to type
some text). Then the readLine method uses a Scanner to read a whole line of text from the standard input stream
System.in of the program and returns the text as result. (Do not close the scanner, because this would also close the
standard input stream, and then the next time you tried to read something from the standard input stream you would
get a NoSuchElementException!)
The readPosInt method uses System.out.print (not println) to print its string argument on the screen (later
when we use the readPosInt method, the string argument of the method will be a message telling the user to type some
integer). Then the readPosInt method uses a Scanner to read an integer from the standard input stream
System.in of the program.
After reading the integer, the readPosInt method must also use the Scanner's nextLine method to read the
single newline character that comes from the user pressing the Enter key on the keyboard after typing the integer (if
you do not read this newline character using the nextLine method inside the readPosInt method, then the
newline character will remain in the input stream, and, the next time you use the readLine method described above,
the readLine method will just immediately read only the newline character from the input stream and return an
empty string as result, without waiting for the user to type anything!)
If the user types something which is not an integer, then the nextInt method of the Scanner will throw an
InputMismatchException. In that case the code of your readPosInt method must catch the exception, use
System.out.println to print the error message "You must type an integer!" to the user (use
System.out.println for this, not System.err.println, otherwise you might hit a bug in Eclipse...), and then
do the whole thing again (including printing again the string argument of the readPosInt method) to try to read an
integer again (hint: put the whole code of the method inside a while loop).
After reading the integer and the newline character (which is just ignored), the readPosInt method tests the integer.
If the integer is bigger than or equal to zero, then the readPosInt method returns the integer as result. (Do not close
the scanner, because this would also close the standard input stream, and then the next time you tried to read
something from the standard input stream, you would get a NoSuchElementException!) If the integer is strictly
less than zero, then the readPosInt method uses System.out.println to print the error message "Positive
integers only!" to the user (use System.out.println for this, not System.err.println, otherwise you
might hit a bug in Eclipse...), and then does the whole thing again (including printing again the string argument of the
readPosInt method) to try to read an integer again (hint: just print the error message, and then the while loop you
already have around the whole code will automatically do the whole thing again...)
For example, if you want to check that your two methods readLine and readPosInt work correctly, put the
following code in the main method of your TUI class:
public static void main(String[] args) {
String str1 = readLine("Type some text: ");
System.out.println("Text read is: " + str1);
int i = readPosInt("Type an integer: ");
System.out.println("Integer read is: " + i);
String str2 = readLine("Type some text again: ");
System.out.println("Text read is: " + str2);
}
then running the main method of the TUI class should look like this (where aaaa bbbb, cccc, dddd eeee, -100,
-200, 1234, and ffff gggg are inputs typed by the user on the keyboard):
Type some text: aaaa bbbb
Text read is: aaaa bbbb
Type an integer: cccc
You must type an integer!
Type an integer: dddd eeee
You must type an integer!
Type an integer: -100
Positive integers only!
Type an integer: -200
Positive integers only!
Type an integer: 1234
Integer read is: 1234
Type some text again: ffff gggg
Text read is: ffff gggg
Question 6
Once you have checked that your methods readLine and readPosInt work correctly, remove all the code inside
the main method of the TUI class so that the main method is empty again.
In the rest of this question, use the readLine and readPosInt methods every time your program needs to read a
string or an integer from the user.
In the empty main method of the TUI class, create a single Bank object with the name "UIC Bank". The main
method of the TUI class must then print a menu that allows the user of your software to do six different actions that
involve the bank object, and your program must then read an integer from the user that indicates which action must be
performed by the program (see below for the details about each action). Use the readPosInt method to print the
menu (give the string for the menu as the argument of readPosInt) and to read the integer typed by the user.
For example, the menu should look like this:
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
The user then types an integer between 1 and 6 to select the action.
For example (where 3 is an input from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
and your program then performs the selected action.
After an action has been performed by your program, your program must again print the menu and ask again the user of
the program for the next action to perform. (hint: put the whole code of the main method inside a while loop, except
for the one line of code that creates the single bank object).
If the user types an integer which is not between 1 and 6, then your program must print an error message "Unknown
action!" to the user (hint: when testing the integer for the action, use the default case of a switch statement)
and then print the menu again (by just going back to the beginning of the while loop).
For example (where 7 is an input from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 7
Unknown action!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
If the user types something which is not an integer, the readPosInt method that you implemented in the previous
question will automatically repeat the menu and ask the user to type an integer again until the user actually types an
integer, so you do not have to worry about this in the code of the main method of your TUI class.
For example (where aaaa and bbbb are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): aaaa
You must type an integer!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): bbbb
You must type an integer!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
Here are the detailed explanations for each action.
Action 1: printing the total amount of money stored in the bank.
When the user of the software specifies action 1, your program must simply print on the screen the total amount of
money currently stored in the bank object for "UIC Bank". Then your program goes back to printing the menu of actions
(by just going back to the beginning of the while loop).
For example (where 1 is an input from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 2000
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
Action 2: adding a new customer account to the bank.
When the user of the software specifies action 2, your program must add a new account to the bank. To add a new
account, your program needs to ask the user three things: the type of account (an integer read using readPosInt: the
integer 1 represents a credit account, the integer 2 represents a student account, any other integer must result in an
error message "Unknown type of account!" being printed and the software going immediately back to the
main menu), the name of the customer (a string read using readLine), and the initial amount of money that the
customer puts into the account when the account is created. You program must then create the correct account, add it
to the bank, and print an information message for the user. The program then goes back to the menu.
For example (where 2, 3, 2, 1, Philippe, 500, 2, 2, Meunier, and 1500 are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 3
Unknown type of account!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 1
Enter the name of the customer: Philippe
Enter the initial amount of money: 500
Credit account for Philippe with 500 yuan has been added
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 2
Enter the name of the customer: Meunier
Enter the initial amount of money: 1500
Student account for Meunier with 1500 yuan has been added
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
Note that the readPosInt method prevents the initial amount of money from being negative, so the constructor for
the StudentAccount class will never throw a NotEnoughMoneyException when you create a student account
object. Nevertheless the code of the main method of your TUI class must handle this exception by printing the error
message "BUG! This must never happen!" and immediately terminating the program using
System.exit(1);
Action 3: listing the amount of money in the account of a given customer.
When the user of the software specifies action 3, your program must ask the user to type the name of a customer, and
the program then prints the amount of money which is currently in the account of this user.
For example (where 3, Philippe, 3, and Meunier are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
If the name of the customer is wrong (the bank does not have an account for this customer) then an
UnknownCustomerException exception will be thrown by the Bank object. The code of the main method of your
TUI class must catch this exception, print the error message from the exception object, and then it just goes back to
printing the menu of actions (by just going back to the beginning of the while loop).
For example (where 3 and aaaa are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: aaaa
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
Action 4: withdrawing money from the account of a given customer.
When the user of the software specifies action 4, your program must ask the user to type the name of a customer, and
an amount of money to withdraw, and the program then withdraws that amount of money from that customer's bank
account. Then the program goes back to the main menu.
For example (where 3, Philippe, 4, Philippe, 200, 3, and Philippe are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: Philippe
Enter the amount of money to withdraw: 200
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 300 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
If the name of the customer is wrong (the bank does not have an account for this customer) then an
UnknownCustomerException exception will be thrown by the Bank object. The code of the main method of your
TUI class must catch this exception, print the error message from the exception object, and then it just goes back to
printing the menu of actions (by just going back to the beginning of the while loop).
For example (where 4, aaaa, and 200 are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: aaaa
Enter the amount of money to withdraw: 200
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
If the account of the customer is a student account and the amount of money to withdraw is too big then a
NotEnoughMoneyException exception will be thrown by the StudentAccount object. The code of the main
method of your TUI class must catch this exception, print the error message from the exception object, and then it just
goes back to printing the menu of actions (by just going back to the beginning of the while loop).
For example (where 3, Meunier, 4, Meunier, 2000, 3, and Meunier are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: Meunier
Enter the amount of money to withdraw: 2000
Cannot withdraw 2000 yuan from account, only 1500 yuan is available
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
Action 5: depositing money into the account of a given customer.
When the user of the software specifies action 5, your program must ask the user to type the name of a customer, and
an amount of money to deposit, and the program then deposits that amount of money into that customer's bank
account. Then the program goes back to the main menu.
Note: the bank object that you are using does not have a deposit method. So, in the code of the main method of the
TUI class, simulate a deposit by simply doing a withdrawal of the negative amount! For example, depositing 500 yuan
on an account is the same as withdrawing -500 yuan from the same account.
For example (where 3, Philippe, 5, Philippe, 200, 3, and Philippe are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 300 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 5
Enter the name of the customer: Philippe
Enter the amount of money to deposit: 200
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
If the name of the customer is wrong (the bank does not have an account for this customer) then an
UnknownCustomerException exception will be thrown by the Bank object. The code of the main method of your
TUI class must catch this exception, print the error message from the exception object, and then it just goes back to
printing the menu of actions (by just going back to the beginning of the while loop).
For example (where 5, aaaa, and 200 are inputs from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 5
Enter the name of the customer: aaaa
Enter the amount of money to deposit: 200
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6):
Note that, even if a customer has a student account, the readPosInt methods prevents the amount to deposit from
being negative. So doing a deposit always increases the amount of money stored in an account (or the amount of money
remains the same if the customer deposits zero yuan). This means the student account will never throw a
NotEnoughMoneyException. Nevertheless the code of the main method of your TUI class must handle this
exception by printing the error message "BUG! This must never happen!" and immediately terminating the
program using System.exit(1);
Action 6: quitting the program.
When the user of the software specifies action 5, your program must print a "Goodbye!" message, and terminate the
program using: System.exit(0);
For example (where 6 is an input from the user):
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 6
Goodbye!
Here is a more complete example of running the software:
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): aaaa
You must type an integer!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): bbbb cccc
You must type an integer!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): -100
Positive integers only!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 7
Unknown action!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 0
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): -100
Positive integers only!
Type the account type (credit:1 student:2): 3
Unknown type of account!
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 1
Enter the name of the customer: Philippe
Enter the initial amount of money: -100
Positive integers only!
Enter the initial amount of money: 500
Credit account for Philippe with 500 yuan has been added
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 2
Type the account type (credit:1 student:2): 2
Enter the name of the customer: Meunier
Enter the initial amount of money: 1500
Student account for Meunier with 1500 yuan has been added
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 2000
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: aaaa
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: Philippe
Enter the amount of money to withdraw: -100
Positive integers only!
Enter the amount of money to withdraw: 200
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 300 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: aaaa
Enter the amount of money to withdraw: 200
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 4
Enter the name of the customer: Meunier
Enter the amount of money to withdraw: 2000
Cannot withdraw 2000 yuan from account, only 1500 yuan is available
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Meunier
Meunier has 1500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 5
Enter the name of the customer: Philippe
Enter the amount of money to deposit: -100
Positive integers only!
Enter the amount of money to deposit: 200
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 3
Enter the name of the customer: Philippe
Philippe has 500 yuan in the bank
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 5
Enter the name of the customer: aaaa
Enter the amount of money to deposit: 200
Customer aaaa unknown
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 1
Total amount of money in the bank: 2000
Type an action (total:1 add:2 list:3 withdraw:4 deposit:5 quit:6): 6