• Post Reply Bookmark Topic Watch Topic
  • New Topic

Structural problems  RSS feed

 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having difficulty constructing a good structure in my program, all of the ways I have come up with so far have been very difficult to expand upon. The program I am supposed to write should save 3 fields of information about a person, first and last name and birth date.
How should I go about this? My current idea, which I can't get to work properly, looks basicly like this:

Is this anywhere near a good solution? Any tips on what to think about when drawing up the structure of a program?
[ October 17, 2003: Message edited by: Carl Pettersson ]
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest to put the create code (getting information from user) in the main method.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In later assignments I will extend the Person class, so I thought that it would be best to have that method in the class itself. What would I gain by putting it in the main class?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would separate the getting input part from the Person object. Let us say that you are reading the data from the console today.
In future you would like to read it from some GUI (Swing) and this required change in the Person Class. If you separate the code which involves getting data from the Class you dont need to touch the Person class.
One more benefit is you can read data from different sources console,gui etc, if the code for accepting input is isolated from Person class.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl
I agree with Pradeep - you definitely should move any interaction with the user out of the Person class. Person should simply hold a person's details, and that is all.
Personally (no pun intended), I would also lose the String array and use individual members for forename, surname and date of birth (perhaps consider using a Date object or GregorianCalendar for the date of birth, rather than a String - this makes comparisons a bit easier). By using an array you only make things more complex than they need to be (e.g. the accessor method for forename needs to 'know' which element of the array represents the forename, etc.)
Think about the responsibility of your classes - this one is an abstraction of a real world entity (a person). As such, its responsibilites within your application should only reflect this abstraction. Other areas of your program can use it to handle there own areas of responsibility.
Hope this helps
Michael
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I would pass the forename, surname and d/o/b into the constructor as arguments, rather than having a no argument constructor and a separate create method.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Person class should not be concerned with how the data is retrieved.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and one more thing - you have a method here called 'Person', with a void return type. Definitely remove it. Were you intending to create a defaullt constructor? Remember that constructors have no return type at all (void or otherwise). The reason you are managing to create an instance of this class is because the compiler creates a default constructor for you if you do not define any constructors your self.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!