• Post Reply Bookmark Topic Watch Topic
  • New Topic

Person and Account classes  RSS feed

 
jim dimitriou
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I want to write an app that declares a class Person(String name, int age), and an Account class, Account(int code, double balance).
But, additionally, every Person has at most 3 accounts, and each account has a Peron associated with it.
my code so far...
My problem is:
When I make data input for a person, and additionally I want to read data for the account(s) that this rerson has,
what code should I write to create a new Account object as account[numOfAccounts].
And, what is the code to assign an owner to a new created Account object?

I'm a bit confused...
There exists a relationship between the two classes,
but I cannot find the way to implement this relation....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think about it in English. A person HAS-AN account (or two, or three...or none). But an account does not HAVE a person...an account could be owned by a corporation, or if the person dies and the bank can't find a next-of-kin, it belongs to nobody (for now...).

When you first make a person, they won't have an account.

Once you have a person, you would add an account to them. so I would probably make an addAccount() method on the Person class. This method would create the account, check to make sure the person doesn't already have three, and then would add the newly created account to the Person object.

I am assuming here that you are not talking about persistence - once your program closes, you loose all data.

If you are talking about writing to a DB or some kind of permanent storage, we have other issues to deal with.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing that you have the basic structure for both classes that matches your business logic, all you need now is to implement algorithms that support what you are trying to accomplish.

In the person class you can use a method such that every time you attach an account to a person, the numOfAccounts variable increments to keep track of the number of accounts already in the array and also the limit.

Example:



In the Account class, to register the person on the supplied account implement a register method that registers the person object

Example:



Therefore in the Account constructor there is no need for a Person object to be passed in.

Now we have the following

 
jim dimitriou
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks , I fixed it...
It's now working...
 
jim dimitriou
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Rico!!!
Your code is great!!!
Thanks a lot...
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!