Question: JAVA Write another class called Bank containing a main method in order to fully test your account classes. Create two objects for each account class

JAVA

Write another class called Bank containing a main method in order to fully test your account classes. Create two objects for each account class (Total 6). Using the objects call various methods defined within these classes:

public abstract class Account { protected int accountNumber; protected double balance;

// Constructor public Account(int a) { balance = 0.0; accountNumber = a; }

public void deposit(double amount) { if (amount > 0) balance += amount; else System.err.println("Account.deposit(...): " + "cannot deposit negative amount."); }

public void withdraw(double amount) { if (amount > 0) if (balance - amount >= 0) balance -= amount; else System.err.println("Account.withdraw(...): " + "cannot withdraw more than your balance."); else System.err.println("Account.withdraw(...): " + "cannot withdraw negative amount."); }

public double getbalance() { return balance; }

public double getAccountNumber() { return accountNumber; }

public void display() { System.out.println("Account " + accountNumber + " : " + "Balance = " + balance); }

public abstract void printAccountInfo(); }

=======================================================

public class SavingsAccount extends Account { private double interestRate; private static int countObjects = 0; /* static variable it will be initialized once and retain it's value */ static int numberOfAccouunts = 0;

public SavingsAccount(int a, double interestRate) { super(a); this.interestRate = interestRate; countObjects++; /* increment in static variable, when a object is created */ numberOfAccouunts++; } public void addInterest()

{

balance += balance * (interestRate / 100);

}

public double GetInterest() { return interestRate; }

public void withdraw(double amount) {

if (amount >= 0 && balance >= amount)

balance -= amount;

else if (amount < 0)

System.err.println("Account.withdraw(...): "

+ "cannot withdraw negative amount.");

else

System.err.println("Account.withdraw(...): "

+ "cannot withdraw more than balance.");

}

public void display() { super.display(); System.out.println("Interest rate: " + interestRate); }

// displayNumberOfAccounts() method which shows total number of accounts // created in SavingAccount class public static void displayNumberOfAccounts() { System.out.println("Total number of accounts created in SavingAccount class is : " + numberOfAccouunts); }

@Override public void printAccountInfo() { // TODO Auto-generated method stub

} }

==========================================

public class CurrentAccount extends Account { private double overdraftLimit; private static int countObjects = 0; // static variable it will be initialized once and retain it's value static int numberOfAccounts = 0;

public CurrentAccount(int a, double overdraftLimit) { super(a); this.overdraftLimit = overdraftLimit; countObjects++; // increment in static variable, when a object is created numberOfAccounts++; }

public void withdraw(double amount) { if (amount > 0) if ((balance + overdraftLimit) - amount >= 0) balance -= amount; else System.err.println("Account.withdraw(...): " + "cannot withdraw more than your balance."); else System.err.println("Account.withdraw(...): " + "cannot withdraw negative amount."); } public double getOverdraftLimit() { return overdraftLimit; }

// displayNumberOfAccounts() method which shows total number of accounts // created in CurrentAccount class public static void displayNumberOfAccounts() { System.out.println("Total number of accounts created in CurrentAccount class is : " + numberOfAccounts); }

@Override public void printAccountInfo() { // TODO Auto-generated method stub

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!