Reputation: 101
This week I was tasked with writing an abstract BankAccount class and a SavingsAccount class which extends BankAccount. I have written out the code as the assignment asks and it seems to compile perfectly. I now must write a driver to test the two classes and here is where I am stuck.. Just to be clear I'm not asking anyone to write it for me, I want to eventually be able to do this all on my own. I'm just asking for a little guidance. (I've scheduled one on one time with my instructor and he has cancelled twice)
I basically am wondering how to write the driver class for these two classes. Are my classes missing anything in terms of fields or methods? How do you seasoned programmers plan out this kind of stuff?
Any suggestions you may have would be appreciated!
public abstract class BankAccount
{
double balance;
int numOfDeposits;
int numOfWithdraws;
double interestRate;
double annualInterest;
double monSCharges;
double amount;
double monInterest;
//constructor accepts arguments for balance and annual interest rate
public BankAccount(double bal, double intrRate)
{
balance = bal;
annualInterest = intrRate;
}
//sets amount
public void setAmount(double myAmount)
{
amount = myAmount;
}
//method to add to balance and increment number of deposits
public void deposit(double amountIn)
{
balance = balance + amountIn;
numOfDeposits++;
}
//method to negate from balance and increment number of withdrawals
public void withdraw(double amount)
{
balance = balance - amount;
numOfWithdraws++;
}
//updates balance by calculating monthly interest earned
public double calcInterest()
{
double monRate;
monRate= interestRate / 12;
monInterest = balance * monRate;
balance = balance + monInterest;
return balance;
}
//subtracts services charges calls calcInterest method sets number of withdrawals and deposits
//and service charges to 0
public void monthlyProcess()
{
calcInterest();
numOfWithdraws = 0;
numOfDeposits = 0;
monSCharges = 0;
}
//returns balance
public double getBalance()
{
return balance;
}
//returns deposits
public double getDeposits()
{
return numOfDeposits;
}
//returns withdrawals
public double getWithdraws()
{
return numOfWithdraws;
}
}
and the subclass
public class SavingsAccount extends BankAccount
{
//sends balance and interest rate to BankAccount constructor
public SavingsAccount(double b, double i)
{
super(b, i);
}
//determines if account is active or inactive based on a min acount balance of $25
public boolean isActive()
{
if (balance >= 25)
return true;
return false;
}
//checks if account is active, if it is it uses the superclass version of the method
public void withdraw()
{
if(isActive() == true)
{
super.withdraw(amount);
}
}
//checks if account is active, if it is it uses the superclass version of deposit method
public void deposit()
{
if(isActive() == true)
{
super.deposit(amount);
}
}
//checks number of withdrawals adds service charge
public void monthlyProcess()
{
if(numOfWithdraws > 4)
monSCharges++;
}
}
Upvotes: 0
Views: 31433
Reputation: 997
A driver or runner class is usually a class with a main method in which you can run code. Basically...
public class TestDriver {
public static void main(String[] args) {
// Run your code here...
}
}
What you probably need to do is create a few SavingsAccount objects inside of it, and show that the methods it implements work.
If this is a school assignment, you may need to get more specific details from your instructor if you are not understanding the requirements.
Upvotes: 2