Java: Trying to read in a date into a constructor method

Soldato
Joined
5 Dec 2003
Posts
2,716
Location
Glasgow
Em as title really... i'm trying to run this constrctor object so that a date is passed to it from another object eventually and then it can store the date information in the appropriate format but I can't seem to get it to work, here is the code:

public class Customer
{
// instance variables
private String name;
private String address;
private Date dateOfBirth;

/**
* Constructor for objects of class Customer
*/
public Customer(String customerName, String customerAddress, Date customerDateOfBirth)
{
// add new customer

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date dateOfBirth = new Date();

name = customerName;
address = customerAddress;
dateOfBirth = customerDateOfBirth();
}
 
davestar_delux said:
I have never coded java but I do code c# which is quite similar.

Firstly, ocrrect me if im wrong but here you are redeclaring the variable dateofbirth in the local scope:

Code:
Date dateOfBirth = new Date()

I would imagine it would be:

Code:
dateOfBirth = new Date()

Secondly you create a formatting object for the date but don't use it, are you not supposed to pass it into the date constructor?

Finally customerdateofbirth is a variable not a method, remove the ().
 
You dont need to format the date till you use it. If you want to, then you could store the date formatted as a string, but that would be poor design. You have it basically right, with the following corrections:

public Customer(String customerName, String customerAddress, Date customerDateOfBirth)
{
// add new customer

name = customerName;
address = customerAddress;
dateOfBirth = customerDateOfBirth;
}

No need to declare a local dateOfBirth variable, as it already exists within the class. Also note the removal of () in the assignment, which would attempt to locate a method called customerDateOfBirth().

Also, if you intend to modify the original date object and dont want to modify the date object in this class, then you should use:

dateOfBirth = customerDateOfBirth.clone();

which would create a new copy of this Date object.
 
Right I think I have the class working but I am struggling to test it as when I create an instance it asks to enter the parameters which i do but it throws up errors. I think they are due to the date, is there anyway I can test it quickly in this way? and if so, how do I enter the date?
 
Thanks guys. Another problem now, I have created this method:

public boolean updateOverdraftLimit(int newLimit)
{
//add code to check whether newLimit is between acceptable limits
overdraftLimit = newLimit;

System.out.println("New overdraft limit: £" + overdraftLimit);
return true;
}

And call it from another class like so:

SavingsAccount savAcc1 = new SavingsAccount();

savAcc1.updateOverdraftLimit(250);

How do I now use the return boolean value in the calling class to write out whether the update was successful or not?
 
Thanks for all the help so far everyone, i'm getting my head aorund a lot of this now. Sorry to be a pest but I have one more problem I could use help with.

I have to keep a log of transactions for my banking system, the only operations I need to perform on this are to add a transaction and to display all transactions so i've figured out I think my best option is a LinkedList which i'm fine with.

The trouble is there are 3 parts to each transaction which will need to be stored together, they are the transactionType (a string either withdraw or deposit), amount and the date. I don't know how to group these so that I can insert them into a single node on the list?
 
Well I've created my object and it went okay, however when I try to pass parameters to put values into that object it is throwing up an error "java.lang.NullPointerException" and highlighting a line in my addTransaction method.

Here is the method calling it from my currentAccount class:

public boolean deposit(int depositAmount)
{
StringBuffer depositSB = new StringBuffer( "Deposit" );
trans1.addTransaction(depositSB, depositAmount);
System.out.println("Amount Deposited: £" + depositAmount);
return true;
}


And here is the full Transaction class that seems to have the error:


import java.util.*;
import java.text.*;
import java.util.LinkedList;

public class Transaction
{
class transactionTypeWrapper{
Date now;
StringBuffer transactionType;
int amount;
}

transactionTypeWrapper aTransaction;

public Transaction()
{
LinkedList transactions = new LinkedList();
}

public void addTransaction(StringBuffer transactionType, int amount)
{
aTransaction.transactionType = transactionType; //error appears to be with this line?
aTransaction.amount = amount;
System.out.println("Transaction Successful");
}
}
 
Back
Top Bottom