0

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.

3
Contributors
6
Replies
18
Views
5 Years
Discussion Span
Last Post by JJHT7439
0

The printStatement method signature looks wrong to me. I would expect it to have no parameters, and just print a statement for the current account ("this"), OR, to take a list of accounts as parameter, in which case it should be a static method (ie belong to the class, not any one specific instance of an account).
If it's the second option, you need to call getInterest etc on each member of the list:

for (BankAccount temp : ar1) {  // modern version of the for loop
   temp.calcInterest();
   double theInterest = [B]temp.[/B]getInterest();
   // etc
0

Alright, I moved the loop over to the test class to try and get it to work there, and it almost works now but there still seems to be a problem. For some reason when I run it it doesn't calculate the interest on the accounts that aren't CD accounts.

The output is this:

John Anderson   Interest Earned:    50.75       Current Balance:  11200.75
Phil Phillips   Interest Earned:    45.0        Current Balance:  10045.0
Bill Jones  Interest Earned:    0.0     Current Balance:  5000.0
Sharon Smith    Interest Earned:    0.0     Current Balance:  64665.75
Bob Evan    Interest Earned:    0.0     Current Balance:  9000.0

Total Money: 99911.5
Total Interest: 95.75

The first 2 are CD accounts and it calculates interest fine, but it doesn't calculate interest on the other types of accounts for some reason.

BankAccount 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(){
        //BankAccount temp = (BankAccount) arl.get(i);
        //temp.calcInterest();
        //temp.addInterest(temp.getInterest());
        //double theInterest = temp.getInterest();
        //double theMoney = temp.getCurrentBalance();
        System.out.println(name + " Interest Earned:    " + getInterest() + "       Current Balance:  " + newMoney);
        //System.out.println("\nTotal Money: " + totalMoney);
        //System.out.println("Total Interest: " + totalInterest);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

TestBankAccount class:

import java.util.*;
public class TestBankAccount {
    /**
     * @param args
     */
    private static double totalMoney = 0;
    private static double totalInterest = 0;
    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", false);
        BankAccount check1 = new CheckingAccount(9000, "Bob Evan", true);
        arl.add(cd);
        arl.add(cd1);
        arl.add(save);
        arl.add(check);
        arl.add(check1);
        for (int i = 0; i < arl.size(); i++){
            BankAccount temp = (BankAccount) arl.get(i);
            temp.calcInterest();
            temp.addInterest(temp.getInterest());
            temp.printStatement();
            totalMoney = totalMoney + temp.getCurrentBalance();
            totalInterest = totalInterest + temp.getInterest();
        }
        System.out.println("\nTotal Money: " + totalMoney);
        System.out.println("Total Interest: " + totalInterest);
    }
}

CDAccount class:

public class CDAccount extends BankAccount{
    static double MONTHLY_INTEREST_RATE = .06;
    static double MINIMUM_BALANCE = 1000;
    private double newMoney;
    private String owner;
    private double interest;

    public CDAccount(double newMoney, String owner){
        super(newMoney, owner);
        this.newMoney = newMoney;
        this.owner = owner;
    }
    public void calcInterest(){
        interest = ((newMoney-MINIMUM_BALANCE) * (MONTHLY_INTEREST_RATE/12));
        newMoney = newMoney + interest;
    }
    public double getInterest(){
        return interest;
    }
    public double getMoney(){
        return newMoney;
    }
    public String toString(){
        return "A SavingsAccount with initial balance " + newMoney + " and owner " + owner;
    }
    public static void main(String[] args){
        CDAccount check = new CDAccount(10000, "Axel");
        System.out.println(check);
        check.calcInterest();
        System.out.println(check);
    }

}

SavingsAccount class:

public class SavingsAccount extends BankAccount{
    static double SAVINGS_MONTHLY_INTEREST_RATE = .03;
    private double newMoney;
    private String owner;
    private double interest;

    public SavingsAccount(double newMoney, String owner){
        super(newMoney, owner);
        this.newMoney = newMoney;
        this.owner = owner;
    }
    public void calcInterest(){
        interest = (newMoney * (SAVINGS_MONTHLY_INTEREST_RATE/12));
        newMoney = newMoney + interest;
    }
    public String toString(){
        return "A SavingsAccount with initial balance " + newMoney + " and owner " + owner;
    }
    public static void main(String[] args){
        SavingsAccount check = new SavingsAccount(10000, "Axel");
        check.calcInterest();
        System.out.println(check);
    }

}

CheckingAccount class:

public class CheckingAccount extends BankAccount{

    /**
     * @param args
     */
    static double BONUS_MONTHLY_RATE = .015;
    private boolean bonus;
    private String owner;
    private double newMoney;
    private double interest = 0;

    public CheckingAccount(double newMoney, String owner, boolean bonus){
        super(newMoney,owner);
        this.newMoney = newMoney;
        this.owner = owner;
        this.bonus = bonus;
    }
    public void calcInterest(){
        if(bonus == true){
            interest = (newMoney * (BONUS_MONTHLY_RATE/12));
        }
        newMoney = newMoney + interest;
    }
    public String toString(){
        return "A CheckingAccount with initial balance " + newMoney + ", owner " + owner + ", and bonus flag " + bonus;
    }
    public static void main(String[] args) {
        CheckingAccount check = new CheckingAccount(10000, "Axel Evergreen", false);
        check.calcInterest();
        System.out.println(check);
        CheckingAccount check1 = new CheckingAccount(10000, "Axel", true);
        check1.calcInterest();
        System.out.println(check1);
    }

}

I feel like all of the account classes are working fine but something is obviously wrong. All of the accounts have different ways of determining interests too but I think thats all coded correctly. I'd appreciate any help.

Edited by ~s.o.s~: removed real name + formatting

0

You declare and use newMoney (etc) in the abstract superclass. This variable is inherited by all the subclasses EXECPT that you declare it private, then declare new variables with the same names in your subclasses. So the "newMoney" that you calc in the subclass is a different variable from the "newMoney" that the inherited superclass methods access. Your code will only work if the subclasses are using the inherited variables from the superclass.
As a general rule you should never declare a variable in a subclass that has the same name as a variable in the superclass - it's guaranteed to cause confusion, at best. And never declare variables that should be inherited as "private" because that prevents them from being accessed in any subclass.

0

Ah alright that makes so much more sense. I also realized that I was calling a getInterest() method in my Test class and only my CD account had one for some reason. Everything is working now, thanks for the help guys.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.