user3083893
user3083893

Reputation: 29

Java.util.Date null output

I have no idea why my code is giving me a null for java.util.date.

Question: Write a test program that creates an Account object with an account ID of 1122, a balance of 20000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2500, use the deposit method to deposit $3000, and print the balance, the monthly interest, and the date when this account was created
Here is my code:

import java.util.*;  
public class Account {  
private int ID;  
private double Balance;  
private double annualInterestRate;  
private java.util.Date dateCreated;  
public Account(){}  
public Account(int ID, double Balance, double annualInterestRate){  
    this.ID=ID;  
    this.Balance=Balance;  
    this.annualInterestRate= annualInterestRate;  
}  
public void setID(int ID){  
    this.ID=ID;  
}
public void setBalance(double Balance){  
    this.Balance=Balance;  
}  
public void setAnnualInterestRate(double annualInterestRate){  
    this.annualInterestRate= annualInterestRate;  
}  
public int getID(){  
    return ID;  
}  
public double getBalance(){  
    return Balance;  
}  
public double getInterestRate(){  
    return annualInterestRate;  
}  
public java.util.Date getDateCreated(){  
    return dateCreated;  
}  
public double getMonthlyInterestRate(){  
    return annualInterestRate/12;  
}  
public void withDraw(double val){  
    if ((Balance - val) <0)  
    {  
        System.out.println("Offensive content removed from this line");  
    }  
    else  
    {  
        Balance -= val;  
    }  
}  
public void dePosits(double value){  
    Balance += value;  
}  

public static void main(String [] arges){  
Account account = new Account(1122, 20000,.045);  
account.withDraw(2500);  
account.dePosits(3000);  
System.out.println(account.getBalance());  
System.out.println(account.getDateCreated());  
}   

}

Upvotes: 0

Views: 2174

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280054

You haven't initialized dateCreated. For example, in the constructor (or somewhere else depending on your use case)

dateCreated = new java.util.Date();  

or any other way to initialize with the date it needs.

Upvotes: 3

Related Questions