0

This program creates 10 accounts and sets the balance to $100 each.

I wrote a for loop to initialize the balance but doesn't work. The setBalance() method is not recognize within the AccountArray Class How do I assign $100 balance to each account?

public class AccountArray{

public static AccountArray[] createAccountArray(){

AccountArray[] atm = new AccountArray [10];
for(int i = 0; i < atm.length; i++){
    atm[i] = new AccountArray(setBalance(100));
}
return atm;

  }
}


import java.util.Date;

public class Account{

    public int id = 0;
    public double balance = 0;
    public double annualInterestRate = 0;
    public double withdraw;
    public double deposit;
    java.util.Date date = new java.util.Date();


public Account(){
}

public Account(int id, double balance, double annualInterestRate){
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
}
public Account(int id, double balance, double annualInterestRate, Date date){
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    this.date = date;
}

public void setID(int id){
    this.id = id; 
}

public int getID(){
    return id;
}


public double getBalance(){
    return balance - withdraw + deposit;
}
public void setAnnualInterestRate(double annualInterestRate){
    this.annualInterestRate = annualInterestRate;
}
public double getAnnualInterestRate(){
    return (annualInterestRate/100);
}

public Date getDateCreated(){
    return date;
}


public double getMonthlyInterestRate(){
    return getBalance() * ((annualInterestRate/100) / 12); 
}
public void withdraw(double withdraw){
    this.withdraw = withdraw;
}

public double deposit(){
    return balance + deposit;
}
public void deposit (double deposit){
    this.deposit = deposit;
}


}

.

public class TestAccount extends AccountArray{

public static void main(String[] args) {

    Account account1122 = new Account(1122, 20000, 4.5);
    account1122.withdraw(2500);
    account1122.deposit(3000);
    System.out.println("Your account balance is: \t" + "$"+ account1122.getBalance() + 
            "\nYour monthly interest rate is: \t" + "$" + account1122.getMonthlyInterestRate() + 
            "\nYour account was created on: \t" + account1122.getDateCreated());

    //Declare account array */
    AccountArray[] atm;

    //Create Account array */
    atm = createAccountArray();

    System.out.println(atm[i].getBalance);
    }


}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
MontyMax
  • 105
  • 1
  • 1
  • 9
  • You need to go through array & Object in java – jmj Feb 23 '14 at 03:58
  • `setBalance` is a method, but you're calling it as if it was a normal standalone function. Just because you're calling it inside the constructor's arguments list doesn't absolve you from calling it how it SHOULD be called. Plus, you can't really call a method of an object that hasn't been initialized yet... At the time you try to call `setBalance`, your object doesn't exist yet, so there's no where to actualy SET a balance. – Marc B Feb 23 '14 at 03:59
  • 1
    When I see `double balance = 0;` it *really* scares me. Don't use floating point numbers for exact values (money is an exact value). Use a `BigDecimal` instead. See also [Why not use Double or Float to represent currency?](http://stackoverflow.com/q/3730019/289086) –  Feb 23 '14 at 04:02
  • 2
    @MichaelT: While your point is *very* valid, this seems like a school assignment more than dealing with actual real-world money. Don't sweat them about it **too** much. – Makoto Feb 23 '14 at 04:03

1 Answers1

2

Your program is attempting to create 10 AccountArray instances, not 10 Accounts. Even if that were to work, you're calling a method outside of the context of either a static reference or the object instance - that is never going to work.

What you probably want to do is this:

Account[] atm = new Account[10];
for(int i = 0; i < atm.length; i++){
    atm[i] = new Account();
    atm[i].setBalance(100);
}

...although ignoring the other constructors in this case seems a bit...wrong to me. I leave this part as an exercise for the reader.

Makoto
  • 104,088
  • 27
  • 192
  • 230