Question
Look at Account2.java down below and create a Java project called Lab4 with the file. Then, create a new Java file called Bank2.java that can
Look at Account2.java down below and create a Java project called Lab4 with the file. Then, create a new Java file called Bank2.java that can hold up to 3 accounts with an array of the Account2 class type. (Of course, 3 is a very small number. But its only for our lab purpose.) The following presents a UML diagram of the Bank2 class.
Bank2 |
- accounts: Account2 [ ] - bankName: String - numOfAccounts: int |
+ Bank2( ) + setbankName (String) + openAccount ( ): boolean + closeAccount (int ): boolean + printAllAccounts( ) |
In the class, the openAccount() method should prompt the user to enter the account holders name, account number, account type, and initial balance. If the account number given by the user is already taken by another account holder, your method should return false. Additionally, if there are already 3 accounts in the bank, the method should return false.
The closeAccount() method should get an account number and if the account number doesnt exist, your method should return false. Otherwise, the method should remove the account ( assigning null to the array element) and return true.
Develop a a test program to test:
open and close an account
open an account and then attempt to open another account with the same number
close an account that does not exist
attempt to open more than 3 accounts
open 3 accounts, close one account, then successfully open a 4th , new, account
You can write your tests as 5 separate classes or as one class that does all 5 tests. Here is an example
public class Bank2Test_0 {
public static void main(String[] args) {
/**
* This test verifies that an account can be opened and closed.
*/
System.out.println(Test 1: verify openAccount and closeAccount);
Bank2 testBank = new Bank2();
testBank.setBankName("CSUMB");
System.out.println(When prompted enter Alice, 1234, 1, 1000.50);
testBank.openAccount();
System.out.println(One account should be displayed below.)
testBank.printAllAccounts();
System.out.println(Value true should be displayed below.);
System.out.println (testBank.closeAccount(1234));
System.out.println(No accounts should be listed below.);
testBank.printAllAccounts();
}
}
Accounts2.java file:
public class Account2 {
private String name;
private int number;
private int type;
private double balance;
public Account2(String name, int number, int type, double balance)
{
this.name = name;
this.number = number;
this.type = type;
this.balance = balance;
}
public boolean deposit(double fund)
{
if (fund < 0.0)
{
System.out.println("Error: no negative amount to deposit.");
return false;
}
else
{
balance += fund;
return true;
}
}
public boolean withdrawal(double fund)
{
if (fund > balance)
{
System.out.println("Error: insufficient balance to withdraw.");
return false;
}
else
{
balance -= fund;
return true;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started