user1505612
user1505612

Reputation: 45

Iterator loop with array

I just have what I'm sure is a very easy and quick question here... So let's say I have an Account class as follows:

import java.text.NumberFormat;

public class Account
{
    private final double RATE = 0.03; // interest rate of 3.5%
    private long acctNumber;
    private double balance;
    private String name;

    //-----------------------------------------------------------------
    // Sets up the account by defining its owner, account number,
    // and initial balance.
    //-----------------------------------------------------------------
    public Account (String owner, long account, double initial)
    {
        name = owner;
        acctNumber = account;
        balance = initial;
    }
    //----------------------------------------------------------------- 
    // Deposits the specified amount into the account. Returns the
    // new balance.
    //-----------------------------------------------------------------
    public double deposit (double amount)
    {
        balance = balance + amount;
        return balance;
    }
    //-----------------------------------------------------------------
    // Withdraws the specified amount from the account and applies
    // the fee. Returns the new balance.
    //-----------------------------------------------------------------
    public double withdraw (double amount, double fee)
    {
        balance = balance - amount - fee;
        return balance;
    }
    //-----------------------------------------------------------------
    // Adds interest to the account and returns the new balance.
    //-----------------------------------------------------------------
    public double addInterest ()
    {
        balance += (balance * RATE);
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns the current balance of the account.
    //-----------------------------------------------------------------
    public double getBalance ()
    {
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns a one-line description of the account as a string.
    //-----------------------------------------------------------------
    public String toString ()
    {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        return acctNumber + "\t" + name + "\t" + fmt.format(balance);
    }
}

And I create the Bank class shown here...

public class Bank 
{   
    Account[] accounts;// = new Account[30];
    int count=0;
    String name;

    public Bank(String name)
    {
        this.name = name; 
        accounts = new Account[30];
    }
    public void addAccount(Account acct)
    {
        accounts[count] = acct;
        count++;
    }
    public void addInterest()
    {
        //for (Account acct : accounts)
            //acct.addInterest();
        for(int i = 0; i < count; i++)
            accounts[i].addInterest();
    }
}

I receive an error if I try to use the addInterest() method with the for (Account acct: accounts) loop you see commented out. Can someone please provide me with insight on why this is? I thought these loops were equivalent. Thanks in advance.

Upvotes: 0

Views: 156

Answers (2)

Arne
Arne

Reputation: 2146

The for loop over an iterable array iterates all 30 elements, not only the elements you really added.

You may use an ArrayList<Account> and add elements as needed. This allows you to omit the count field:

public class Bank 
{   
    ArrayList<Account> accounts = new ArrayList<Account>();
    String name;

    public Bank(String name)
    {
        this.name = name; 
    }
    public void addAccount(Account acct)
    {
        accounts.add(acct);
    }
    public void addInterest()
    {
        for (Account acct : accounts)
            acct.addInterest();
    }
}

Upvotes: 1

sathish_at_madison
sathish_at_madison

Reputation: 833

You have to initialize the Account array

so you might want to change this to:

public void addInterest()
    {
        //for (Account acct : accounts)
            //acct.addInterest();
        for(int i = 0; i < count; i++)
            accounts[i].addInterest();
    }

to something like this:

     public void addInterest()
        {
            for (Account acct : accounts)    {
            acct= new Account("John",1234596069,200.00);
            acct.addInterest();
            }
//            for(int i = 0; i < count; i++)
//                accounts[i].addInterest();
        }

Essentially you have to initialize the array variable before invoking a method.

Upvotes: 0

Related Questions