In Java language. With comments. Design a class named BankAccount containing the
ID: 673312 • Letter: I
Question
In Java language. With comments.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. Vthrow 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.)
Explanation / Answer
*/
class is BankAccount
*/
public class BankAccount
{
private static int nextAccountNumber = 10000;
private static double currentInterestRate = 0.05;
public static void setCurrentRate(double newRate)
{
currentInterestRate = newRate;
}
private int accountNumber;
private double balance;
private double rate;
public Account(double initialBalance)
{
accountNumber = nextAccountNumber++;
balance = initialBalance;
rate = currentInterestRate;
}
public int getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
public double getRate()
{
return rate;
}
public double getCurrentRate()
{
return currentInterestRate;
}
public void deposit(double amount)
{
balance += amount;
}
public boolean withdraw(double amount)
{
if (amount > balance) // refuse to let account be overdrawn
return false;
else
{
balance -= amount;
return true;
}
}
public void updateRate()
{
rate = currentInterestRate;
}
public void elapse(int numberOfDays)
{
for (int day = 1; day <= numberOfDays; ++day)
{
balance += balance*rate/12;
}
}
}
BankAccount
public class BankAccount
{
private static int nextAccountNumber = 10000;
private static double currentInterestRate = 0.05;
public static void setCurrentRate(double newRate)
{
currentInterestRate = newRate;
}
private int accountNumber;
private double balance;
private double rate;
public Account(double initialBalance)
{
accountNumber = nextAccountNumber++;
balance = initialBalance;
rate = currentInterestRate;
}
public int getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
public double getRate()
{
return rate;
}
public double getCurrentRate()
{
return currentInterestRate;
}
public void deposit(double amount)
{
balance += amount;
}
public boolean withdraw(double amount)
{
if (amount > balance) // refuse to let account be overdrawn
return false;
else
{
balance -= amount;
return true;
}
}
public void updateRate()
{
rate = currentInterestRate;
}
public void elapse(int numberOfDays)
{
for (int day = 1; day <= numberOfDays; ++day)
{
balance += balance*rate/12;
}
}
}
SavingAccount
public class SavingAccount extends BankAccount
{
public boolean withdraw(double amount)
{
if (getbalance()>= amount+minAmount)
{
super.withdraw(amount);
return true;
}
else return false;
}
private double minAmount;
}
private intnumberOfWithdrawals;
public SavingsAccount ()
{
balance = 0;
numberOfWithdrawals = 0;
}
public voidwithdraw (intamount)
{
if (numberOfWithdrawals > 4)
{
throw newRuntimeException (If the withdrawls >4 a service charge of 1$ is charged);
}
else
{
balance = balance amount;
numberOfWithdrawals++;
}
}
public voidresetNumOfWithdrawals () {}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.