Alright, I admit that I'm struggling a bit with Java but for the project that I'm trying to do I need to create a bunch of bank accounts and then calculate the interest and return the names of the account holders and their balance and interest, as well as the total balance and interest across all accounts. The printStatement() method needs to be in the abstract class while the accounts must be created in the test class. The only way that I could think of doing this was to add all of the accounts to a list of some sort and pass that to the printStatement method. The problem is that when I add all of the bank account objects to the list and pass them over and try to go through them with a for loop, the printstatement ends up printing the same account 5 times instead of going through the other accounts.
This is the BankAccount abstract class:
import java.util.*;
public abstract class BankAccount {
/**
* @param args
*/
private double newMoney;
private String name;
private double interestEarned;
private double totalMoney = 0;
private double totalInterest = 0;
public BankAccount(double newMoney, String name){
this.newMoney = newMoney;
this.name = name;
}
abstract void calcInterest();
protected void addInterest(double newInterestEarned){
newMoney = newMoney + newInterestEarned;
}
protected void setInterestEarned(double interest){
interestEarned = interest;
}
public String toString(){
return name + " " + newMoney;
}
public double getCurrentBalance(){
return newMoney;
}
public double getInterest(){
return interestEarned;
}
public void printStatement(ArrayList arl){
for (int i = 0; i < arl.size(); i++){
BankAccount temp = (BankAccount) arl.get(i);
temp.calcInterest();
temp.addInterest(temp.getInterest());
double theInterest = getInterest();
double theMoney = getCurrentBalance();
System.out.println(name + " Interest Earned: " + theInterest + " Current Balance: " + theMoney);
totalMoney = totalMoney + theMoney;
totalInterest = totalInterest + theInterest;
}
System.out.println("\nTotal Money: " + totalMoney);
System.out.println("Total Interest: " + totalInterest);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
This is the TestBankAccount class:
import java.util.*;
public class TestBankAccount {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<BankAccount> arl = new ArrayList<BankAccount>(5);
BankAccount cd = new CDAccount(11150, "John Anderson");
BankAccount cd1 = new CDAccount(10000, "Phil Phillips");
BankAccount save = new SavingsAccount(5000, "Bill Jones");
BankAccount check = new CheckingAccount(64665.75, "Sharon Smith", true);
BankAccount check1 = new CheckingAccount(9000, "Bob Evan", false);
arl.add(cd);
arl.add(cd1);
arl.add(save);
arl.add(check);
arl.add(check1);
cd1.printStatement(arl);
}
}
If I try to run the testBankAccount it just returns the statement for "Phil Phillips" over and over again. I realize that this is very likely because I am calling printStatement through cd1, but I can't figure out how to call the method in an abstract class otherwise.