Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

11.9 (Account Inheritance Hierarchy) Create an inheritance hierarchy that a bank

ID: 3727976 • Letter: 1

Question

11.9 (Account Inheritance Hierarchy) Create an inheritance hierarchy that a bank m represent customers' bank accounts. All customers at this bank can deposit Ge, redi their accounts and withdraw (i.e., debit) money from their accounts. More specifictves ot also exist. Savings accounts, for instance, earn interest on the money they hold. Checkine on the other hand, charge a fee per transaction. dtera might Create base class Account and derived classes SavingsAccount and CheckingAccout inherit from class Account. Base class Account should include one private instance variableof decimal to represent the account balance. The class should provide a constructor that recia initial balance and uses it to initialize the instance variable with a public property. The paope should validate the initial balance to ensure that it's greater than or equal to 0.0; if not, dhroe exception. Th should add an anaur to the current balance. Method Debit should withdraw money from the Account the debit amount does not exceed the Account's balance. If it does, the balance unchanged, and the method should display the message "Debit amount exce ance. " The class should also provide a get accessor in property Balance that re e class should provide two public methods. Method Credit and ensure the should be l eded account the c turns balance. Derived class SavingsAccount should inherit the functionality of an Acco a decimal instance variable indicating the interest rate (percentage) assignca dur ingsAccount's constructor should receive the initial balance, as weus est rate. SavingsAccount should provide public method CalculateInter indicating the amount of interest earned by an account. Method Calcu to as an initial hat alculateInterest shouldd

Explanation / Answer

// Account.java

public class Account {

private double balance;

public Account(double balance){

if(balance < 0){ // verifying the initial balance

System.out.println(" The initial balance is not valid");

}else {

this.balance = balance; // set the balance as initial balance

}

}

public void credit(double depositAmount){

balance = balance + depositAmount;

}

public boolean debit(double withdrawAmount){

if(withdrawAmount>balance){

System.out.println(" Debit amount exceeded the account balance");

return false;

}else {

balance = balance - withdrawAmount;

return true;

}

}

public double getBalance() {

return balance;

}

}

// SavingsAccount.java

public class SavingsAccount extends Account {

private double interestRate;

private double interestAmount;

public SavingsAccount(double balance , double interestRate) {

super(balance);

this.interestRate = interestRate;

}

public double CalculateInterest(){

double balance = super.getBalance() ;

interestAmount = (balance * interestRate)/100;

return interestAmount;

}

}

//CheckingAccount.java

public class CheckingAccount extends Account{

private double transactionCharge;

public CheckingAccount(double balance , double transactionCharge) {

super(balance);

this.transactionCharge = transactionCharge;

}

public void credit(double depositAmount){

depositAmount = depositAmount - transactionCharge;

super.credit(depositAmount);

}

public boolean debit(double withdrawAmount){

boolean isSuccess = super.debit(withdrawAmount);

if(isSuccess == true){

isSuccess = super.debit(withdrawAmount);

return true;

}

else return false;

}

}

//Bank.java

public class Bank {

public static void main(String[] args) {

double interestAmount ;

SavingsAccount savingsAccount = new SavingsAccount(1000.0, 1);

CheckingAccount checkingAccount = new CheckingAccount(2000.0, 0.5);

interestAmount = savingsAccount.CalculateInterest();

System.out.println("The amout of interest to be added "+interestAmount);

System.out.println("Balance before interest calculation "+savingsAccount.getBalance());

savingsAccount.credit(interestAmount);

System.out.println("Balance after interest calculation "+savingsAccount.getBalance());

}

}