MontyMax
MontyMax

Reputation: 105

Initializing array of objects to $100

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);
    }


}

Upvotes: 0

Views: 294

Answers (1)

Makoto
Makoto

Reputation: 106460

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.

Upvotes: 2

Related Questions