I have class Person and I want to input data form the user like first name, age, sex, weight, etc. (I wantto use the Scanner class.)
How should I deal with the input data to create an object of class Person? I consider the following options and would like to know which one to use.
1) Create a Person object with a no-args constructor at the START and then - input ONE piece of data (eg first name) - use the Person’s SETTER to set this data in the aforementioned object - repeat the steps for the other pieces of data At the end send the object to a PersonDAO
Disadvantage: If I include a no-args constructor for the Person class, I kind of allow the creation of an ‘empty’ invalid Person object somewhere else in the program. (I’m not really sure if I should really worry about that. )
2) Create a Person object with a multiparameter constructor at the END. - first input the pieces of data one by one and keep them in an array/a list/variables. - when done with input, extract the pieces of data from the array/list/variables and create a Person object by putting them in the Person’s constructor (one with many parameters) At the end send the object to a PersonDAO.
Disadvantage: If one of the arguments is invalid, I will learn about that rather late. The whole procedure looks more complicated.
Which option is better then? Maybe there is some other way of dealing with input data before sending it to DAO...
And I guess, the DAO will use the object's GETTERS to create a record in a database?
I suggest using Builder design pattern. It looks like:
So your class :
buildmethod