Java Program Create an inheritance hierarchy that a bank mig

Java Program:

Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e. Credit) money into their accounts and withdraw (i.e. debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction.
Create base class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account.
Base class Account should include one private instance variable of type double to represent the account balance. The class should provide a constructor that received an initial balance and uses it to initialize the instance variable with that initial balance. The class should provide two public methods named method and credit. Method credit should add an amount to the current balance. Method debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged, and the method write an error message to the console, “Debit amount exceeded account balance.” Inputs should be sanitized for these methods in such a way that they only accept positive numbers. If presented with a negative, they should write an error message to the console. The Account class also will need to implement an interface called IBalance. That interface should have one method defined, getBalance(), that will return the balance of the account.
Derived class SavingsAccount should inherit from base class Account, but also include a double instance variable indicating the interest rate (percentage) assigned to the Account. SavingsAaccount’s constructor should receive the initial balance, as well as an initial value for the interest rate. SavingsAccount should provide public method calculateInterest that returns a decimal indicating the amount of interest earned by an account. Method calculateInterest should determine this amount by multiplying the interest rate by the account balance. credit and debit’s implementation should remain unchanged.
Derived class CheckingAccount should inherit from base class Account and include a double instance variable that represents the fee charged per transaction. CheckingAccount’s constructor should receive the initial balance, as well as parameter indicating a fee amount. Class CheckingAccount should redefine methods credit and debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount’s version of these methods should invoke the base-class Account version to perform the updates to an account balance. CheckingAccount’s debit method should charge a fee only if money is actually withdrawn.
After defining the classes in this hierarchy, write a polymorphic Banking app, AccountTester, which creates objects of each class and tests their methods using the Account hierarchy created above. In this app (public static void main class), create several test objects each of the SavingsAccount and CheckingAccount types. You will need to perform multiple credit and debit transactions for each account, testing all of your boundary conditions and for negative input values. As you process each account, determine if it is a SavingsAccount. If so, then calculate the amount of interest owed and add it to the account’s balance. After processing any Account, display its balance.

Solution

public class Account implements IBalance{

private double balance;


public Account(double balance){

   this.balance=balance;

}

public void credit(double amount) throws NegativeException{
   if (amount < 0){
       throw new NegativeException(\"amount can\'t be less than zero\");
   }else
       balance+=amount;

}
public void debit(double amount) throws NegativeException,BalanceLess{
   if (amount < 0){
       throw new NegativeException(\"amount can\'t be less than zero\");
   }
   else{
      
       if (balance<amount){
           throw new BalanceLess(\"Debit amount exceeded account balance.\");}
      
       else{
           balance-=amount;
          
       }  
       }
       }

public double getBalance(){
   return balance;
}
}

class NegativeException extends Exception {
public NegativeException(String msg){
super(msg);
}
}
class BalanceLess extends Exception {
public BalanceLess(String msg){
super(msg);
}
}

/* Now, we will create SavingAccounts Class */

class SavingsAccount extends Account{
  
   private double interestRate;
  
  

class SavingsAccount extends Account{
  
   private double interestRate;
  
  
public SavingsAccount(double balance, double interestRate) {
       super(balance);
       this.interestRate = interestRate;
      
   }

public double calculateInterest (){
   return interestRate*super.getBalance();
}
public double totalAmount(){
   return super.getBalance()+this.calculateInterest();
}
}

/* Now, we will create CheckingAccounts Class */

class CheckingAccount extends Account{
   private double fee;
   double balance;
   public CheckingAccount(double balance, double fee) {
       super(balance);
       this.fee = fee;
      
   }

   public void debit(double amount) throws NegativeException, BalanceLess{
       super.debit(amount);
       balance=super.getBalance();
       balance-=fee;
   }

public double getBalance(){
       return balance;
   }
}

/*Interface*/

public interface IBalance{
double getBalance();
}

/*Account Tester Class */

public class AccountTester {
   public static void main(String[] args){
       SavingsAccount sa=new SavingsAccount(1000, 0.1);
       CheckingAccount ca=new CheckingAccount(1000, 100);
       System.out.println(ca.getBalance());
      
       System.out.println(sa.calculateInterest());
       System.out.println(sa.totalAmount());
       try{
       ca.debit(100);
       System.out.println(ca.getBalance());
       ca.credit(-100);
       sa.credit(-1);
       ca.debit(2000);
      
}
       catch(Exception e){
           e.printStackTrace();
       }

}}

Java Program: Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e. Credit)
Java Program: Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e. Credit)
Java Program: Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e. Credit)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site