Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions