Design a class named BankAccount containing the following da
Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance. throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits money into the bank account. throw an IllegalArgumentException when deposit a negative amount into an account • A method named withdraw(double amount) that withdraws the money from the bank account. throw an IllegalArgumentException when withdrawing a negative amount from an account define the exception class InsufficientFundsException throw an InsufficientFundsException when the balance is less than the withdrawal amount. • A method transfer(double amount, BankAccount other) that transfer money from the bank account to another account. • A method named toString() that returns a string description for the account. Design two classes ChackingAccount and SavingAccount which are derived from the class BankAccount. • CheckingAccount will charge transaction fees. Each month there are 5 free transactions. $3 will be deducted from the checking account for every extra transaction. (Hint: You need to add a method to deduct the fees from the checking account if there are more than 5 tractions in a month. You need to modify the deposit and withdrawal method as well, and you need to add an instance field to remember the number of transactions. You may need to define two constants, one for number of allowed free transactions and the other for fee per extra transaction.) • SavingAccount will earn income at fixed interest rate. (Hint: You need to add a method to add interest each month. Also you need to add an instance field for interest rate.) 2/2 Write your test program to create objects and test all the methods. Your test program should cause all three exceptions to occur and catches them all. Use Comments to explain the lines of code and very important use the standard code for java for a programming 2 course for this assignment.
Solution
public class BankAccount {
private double balance;
public BankAccount() {
// TODO Auto-generated constructor stub
this.balance = 0;
}
/**
* @param balance
*/
public BankAccount(double balance) {
if (balance > 0)
this.balance = balance;
else
throw new IllegalArgumentException(
\"Cannot create account with a negative balance\");
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
/**
* @param amount
* add amount to the balance
*/
public void deposit(double amount) {
if (amount > 0)
this.balance += amount;
else
throw new IllegalArgumentException(
\"Cannot deposit with a negative amount\");
}
/**
* @param amount
* add amount to the balance
*/
public void withdraw(double amount) {
if (amount > 0){
if(balance>amount)
this.balance -= amount;
else
try {
throw new InsufficientFundsException(
\"Cannot withdraw with a negative amount\");
} catch (InsufficientFundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
throw new IllegalArgumentException(
\"Cannot withdraw with a negative amount\");
}
public void transfer(double amount, BankAccount other){
this.withdraw(amount);
other.deposit(amount);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \" balance=\" + balance ;
}
}
public class InsufficientFundsException extends Exception {
/**
*
*/
public InsufficientFundsException() {
// TODO Auto-generated constructor stub
super();
}
/**
* @param message
*/
public InsufficientFundsException(String message) {
// TODO Auto-generated constructor stub
super(message);
}
}
public class CheckingAccount extends BankAccount {
int numberOfTransactions;
final int freeTransactions = 5;
final int feePerExtraTransaction = 3;
public CheckingAccount(double balance) {
super(balance);
this.numberOfTransactions = 0;
}
/**
* @param amount
* add amount to the balance
*/
public void deposit(double amount) {
if (this.numberOfTransactions >= freeTransactions)
super.deposit(amount - feePerExtraTransaction);
else
super.deposit(amount);
this.numberOfTransactions++;
}
/**
* @param amount
* add amount to the balance
*/
public void withdraw(double amount) {
if (this.numberOfTransactions >= freeTransactions)
super.withdraw(amount - feePerExtraTransaction);
else
super.withdraw(amount);
this.numberOfTransactions++;
}
}
public class SavingsAccount extends BankAccount {
private double interestRate;
/**
* @param balance
* @param interestRate
*/
public SavingsAccount(double balance, double interestRate) {
super(balance);
// TODO Auto-generated constructor stub
this.interestRate = interestRate;
}
/**
*
*/
public void addInterest() {
super.deposit(interestRate * getBalance());
}
}
public class TestBankAccount {
/**
* @param args
*/
public static void main(String[] args) {
BankAccount account = new BankAccount(500);
System.out.println(\"Initial \" + account);
account.withdraw(600);
System.out.println(\"After withdraw 500 from account \" + account);
account.deposit(500);
System.out.println(\"After deposit 500 from account \" + account);
account.withdraw(600);
System.out.println(\"After withdraw 500 from account \" + account);
CheckingAccount account2 = new CheckingAccount(400);
for (int i = 1; i <= 5; i++) {
account2.deposit(100);
}
System.out.println(\"After 5 times diposit to checking account:\"
+ account2);
account2.deposit(100);
System.out.println(\"After 5 after transactions to checking account:\"
+ account2);
SavingsAccount account3 = new SavingsAccount(100, 0.2);
System.out.println(\"Savings Account :\" + account3);
account3.deposit(500);
System.out.println(\"Afer adding 500 to Savings Account :\" + account3);
account3.addInterest();
System.out.println(\"Afer adding interest of 20% to Savings Account :\"
+ account3);
}
}
OUTPUT:
Initial balance=500.0
After withdraw 500 from account balance=500.0
After deposit 500 from account balance=1000.0
After withdraw 500 from account balance=400.0
After 5 times diposit to checking account: balance=900.0
After 5 after transactions to checking account: balance=997.0
Savings Account : balance=100.0
Afer adding 500 to Savings Account : balance=600.0
Afer adding interest of 20% to Savings Account : balance=720.0
InsufficientFundsException: Cannot withdraw with a negative amount
at BankAccount.withdraw(BankAccount.java:50)
at TestBankAccount.main(TestBankAccount.java:6)



