Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Array objects...

In this code, I am trying to create 10 accounts with initial $100 in each account.

After creating and array of 10 accounts, I have tried to deposit 100 in each account and then print their balance. I am getting an error.  After correction on this, how do I deposit the initial $100 using its constructor>

Thank you
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class ATM {
    public static void main(String[] args){
        Account[] myAccount = new Account[10];
        for(int i=0;i<myAccount.length;i++)
            myAccount[i].deposit(100.0);
        
        for(int i=0;i<myAccount.length;i++)
            System.out.println(myAccount[i].getBalance());        
    }
}

class Account
{
    private int id;
    private double balance;
    private double annualInterestRate;
    private String dateCreated;
    
    public Account(){
        CurrentDate();
    }
    
    public Account(int id, double balance){
        this.id=id;
        this.balance=balance;
        CurrentDate();
    }
    
    private void CurrentDate() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        dateCreated=dateNow;
    }
            
    public void setId(int id)
    {
        if(id>100)this.id=id;
    }
    
    public int getId()
    {
        return id;
    }
    
    public void setBalance(double balance)
    {
        this.balance=balance;
    }
    
    public double getBalance()
    {
        return balance;
    }
 
    public void setAnualInterestRate(double annualInterestRate)
    {
        this.annualInterestRate=annualInterestRate;
    }
    
    public double getAnualInterestRate()
    {
        return annualInterestRate;
    }
    
    public String getDateCreated()
    {
        return dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return annualInterestRate/12;
    }
    
    public void withdraw(double amount)
    {
        if(amount<=balance) balance=balance-amount;
    }
    
    public void deposit(double amount)
    {
        if(amount>0) balance=balance+amount;
//        System.out.println(balance);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could do
for (int i = 0; i < myAccount.length; i++) {
            myAccount[i] = new Account();
            myAccount[i].deposit(100.0);
            System.out.println(myAccount[i].getBalance());
        }

Open in new window

WHne you exceute this
Account[] myAccount = new Account[10];
you just create the array as a whole
then you need to cretae each individual element as an instance of Account class before you can invoke any method
on that object.
Before you construct ech of your elements this element contains null and when you invoke method
on the object which is equal to null - that's when you get null pointer exception, no atter if this object is element of the array
or single vraibale
Avatar of Mike Eghtebas

ASKER

thanks
Don't forget to create elements of arrays - it is easy to confuse it with creation of arrays
and forget about it.
No problem
:)