One double data field named balance with default values 0.0 to denote the balanc
ID: 675400 • Letter: O
Question
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 Vdefine 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. S3 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.) .Explanation / Answer
//CheckingAccount.java
package m6;
import java.util.Date;
public class CheckingAccount extends BankAccount{
private int currentMonth;
private int numOfTransactions;
final int fee = 3;
final int maxTransaction = 5;
public CheckingAccount() {
super();
Date temp = new Date();
currentMonth = temp.getMonth()+temp.getYear();
numOfTransactions=0;
}
public CheckingAccount(double b) {
super(b);
Date temp = new Date();
currentMonth = temp.getMonth()+temp.getYear();
numOfTransactions=0;
}
@Override
public void withdraw(double b) throws InsufficientFundException {
Date temp = new Date();
int now = temp.getMonth()+temp.getYear();
if(now == currentMonth)
numOfTransactions++;
else {
numOfTransactions=0;
currentMonth = temp.getMonth()+temp.getYear();
}
if(numOfTransactions > maxTransaction)
super.withdraw(b+fee);
else
super.withdraw(b);
}
@Override
public void deposit(double b) {
Date temp = new Date();
int now = temp.getMonth()+temp.getYear();
if(now == currentMonth)
numOfTransactions++;
else {
numOfTransactions=0;
currentMonth = temp.getMonth()+temp.getYear();
}
if(numOfTransactions > maxTransaction)
super.deposit(b-fee);
else
super.deposit(b);
}
}
------------------------------------------------------------------------------------------------------------------------
//BankAccount.java
package m6;
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0.0;
}
public BankAccount(double b) {
if(b <= 0)
throw new IllegalArgumentException("Negative money cannot be added");
balance = b;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void deposit(double b) {
if(b <= 0)
throw new IllegalArgumentException("Negative money cannot be added");
balance += b;
}
public void withdraw(double b) throws InsufficientFundException {
if(b <= 0)
throw new IllegalArgumentException("Negative money cannot be withdrawn");
if(balance-b < 0)
throw new InsufficientFundException("Insufficient balance in bank");
balance += b;
}
public void transfer(double amount, BankAccount b) throws InsufficientFundException {
withdraw(amount);
b.deposit(amount);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Balance is: $"+balance;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
//InsufficientFundException.java
package m6;
public class InsufficientFundException extends Exception {
String message;
public InsufficientFundException(String m) {
message = m;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.