IN JAVA: LANGUAGE: Design an abstract class named BankAccount to hold the follow
ID: 3866852 • Letter: I
Question
IN JAVA: LANGUAGE: Design an abstract class named BankAccount to hold the following data for a bank account:
Balance
Number of deposits this month
Number of withdrawals
Annual interest rate
Monthly service charges
The class should have the following methods: Constructor: The constructor should accept arguments for the balance and annual interest rate.
deposit: A method that accepts an argument for the amount of the deposit. The method should add the argument to the account balance. It should also increment the variable holding the number of deposits.
withdraw: A method that accepts an argument for the amount of the withdrawal. The method should subtract the argument from the balance. It should also increment the variable holding the number of withdrawals.
calcInterest: A method that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance.
This is performed by the following formulas: Monthly Interest Rate = (Annual Interest Rate /12) Monthly Interest = Balance * Monthly interest Rate Balance = Balance + Monthly Interest monthlyProcess: A method that subtracts the monthly service charges from the balance, calls the calcInterest method, and then sets the variables that hold the number of withdrawals, number of deposits, and monthly service charges to zero.
Next, design a Savings Account class that extends the BankAccount class. The Savings Account class should have a status field to represent an active or inactive account. If the balance of a savings accounts falls below $25, it becomes inactive. (The status field should be a Boolean variable.) No more withdrawals may be made until the balance is raised above $25, at which time the account becomes active again.
The savings account class should have the following methods: withdraw: A method that determines whether th account is inactive before a withdrawal is made. (No withdrawal will be allowed if the account is not active.) A withdrawal is then made by calling the superclass version of the method.
deposit: A method that determines whether the account is inactive before the deposit is made. If the account is inactive and deposit brings the balance above$25, the account becomes active again. A deposit is then made by calling the superclass version of the method. montlyProcess: Before the superclass method is called, this method checks the number of withdrawals.
If the number of withdrawals for the month is more than 4, a service charge of $1 for each withdrawal above 4 is added to the superclass field that holds the monthly service charges. (Don’t forget to check the account balance after the servide charge is taken. If the balance falls below $25, the account becomes inactive.)
Explanation / Answer
public abstract class BankAccount {
// Private data members
private double Balance;
private int num_Deposits;
private int num_withdrwals;
private int annualInterestRate;
private double serviceCharge;
// constructor
public BankAccount(double Balance, int annualInterestRate)
{
this.Balance = Balance;
this.annualInterestRate = annualInterestRate;
num_Deposits = 1;
num_withdrwals = 0 ;
}
/**
* Method to get Balance amount
* @return
*/
public double getbalance()
{
return Balance;
}
/**
* Method to withdraw an amount
* @param amount
*/
public void Withdraw(double amount)
{
if(getbalance() >= amount)
{
Balance -= amount;
System.out.println("Amount withdrawn successfully.");
System.out.println("Final Account Balance after withdraw is: $"+getbalance());
num_withdrwals++;
}
else
{
System.out.println(">>>Insufficient funds in the account to process the withdraw<<<");
}
}
/**
* Method to deposit an amount
* @param amount
*/
public void deposit(double amount)
{
Balance += amount;
System.out.println("Amount deposited successfully.");
num_Deposits++;
System.out.println("Final Account Balance after deposit is: $"+getbalance());
}
/**
* Method to calculate interest
*/
public void calcInterest()
{
double monthlyInterestRate = annualInterestRate / 12;
double monthlyInterestAmount = Balance * monthlyInterestRate;
Balance += monthlyInterestAmount;
}
/**
* Method to set monthly service charge
* @param charge
*/
public void setMonthlyServiceCharge(double charge)
{
this.serviceCharge = charge;
}
/**
* Method to process the service charge and interest
*/
public void monthlyProcess()
{
if(Balance >= serviceCharge)
{
Balance -= serviceCharge;
calcInterest();
}
serviceCharge = 0;
num_Deposits = 0;
num_withdrwals = 0;
}
/**
* Method to get number of total withdrawals
* @return
*/
public int getNumWithdrawals()
{
return this.num_withdrwals;
}
/**
* Method to get total number of deposits
* @return
*/
public int getNumDeposits()
{
return this.num_Deposits;
}
}
public class SavingsAccount extends BankAccount{
// private data member of this class
private boolean status;
public SavingsAccount(double balance, int interestRate)
{
super(balance, interestRate);
status = true;
}
//Overriding super class method Withdraw
public void Withdraw(double amount)
{
if(status == false)
{
System.out.println("Unable to process. Your account is inactive");
}
else
super.Withdraw(amount);
if(super.getbalance() < 25)
status = false;
}
//Overriding super class method Deposit
public void deposit(double amount)
{
super.deposit(amount);
if(super.getbalance() > 25)
status = true;
}
//Overriding super class method monthlyProcess()
public void monthlyProcess()
{
if(super.getNumWithdrawals() > 4)
{
double charge = (super.getNumWithdrawals() - 4); // getting number of withdrawals more then 4
super.setMonthlyServiceCharge(charge);
super.monthlyProcess();
if(super.getbalance() < 25)
this.status = false;
}
}
}
File BankAccount.javapublic abstract class BankAccount {
// Private data members
private double Balance;
private int num_Deposits;
private int num_withdrwals;
private int annualInterestRate;
private double serviceCharge;
// constructor
public BankAccount(double Balance, int annualInterestRate)
{
this.Balance = Balance;
this.annualInterestRate = annualInterestRate;
num_Deposits = 1;
num_withdrwals = 0 ;
}
/**
* Method to get Balance amount
* @return
*/
public double getbalance()
{
return Balance;
}
/**
* Method to withdraw an amount
* @param amount
*/
public void Withdraw(double amount)
{
if(getbalance() >= amount)
{
Balance -= amount;
System.out.println("Amount withdrawn successfully.");
System.out.println("Final Account Balance after withdraw is: $"+getbalance());
num_withdrwals++;
}
else
{
System.out.println(">>>Insufficient funds in the account to process the withdraw<<<");
}
}
/**
* Method to deposit an amount
* @param amount
*/
public void deposit(double amount)
{
Balance += amount;
System.out.println("Amount deposited successfully.");
num_Deposits++;
System.out.println("Final Account Balance after deposit is: $"+getbalance());
}
/**
* Method to calculate interest
*/
public void calcInterest()
{
double monthlyInterestRate = annualInterestRate / 12;
double monthlyInterestAmount = Balance * monthlyInterestRate;
Balance += monthlyInterestAmount;
}
/**
* Method to set monthly service charge
* @param charge
*/
public void setMonthlyServiceCharge(double charge)
{
this.serviceCharge = charge;
}
/**
* Method to process the service charge and interest
*/
public void monthlyProcess()
{
if(Balance >= serviceCharge)
{
Balance -= serviceCharge;
calcInterest();
}
serviceCharge = 0;
num_Deposits = 0;
num_withdrwals = 0;
}
/**
* Method to get number of total withdrawals
* @return
*/
public int getNumWithdrawals()
{
return this.num_withdrwals;
}
/**
* Method to get total number of deposits
* @return
*/
public int getNumDeposits()
{
return this.num_Deposits;
}
}