Relationship between classes in java

I need to implement the following classes in Java SE, but I cannot figure out how I can achieve the relationship between ATMCard and Account. I researched (maybe with the wrong keywords), but could not find anything. Thanks in advance.

Class diagram, mentioned in the question

+5
source share
3 answers

Firstly, I find your model a bit strange for ATMCard and Account :

  • not a PIN associated with an ATMCard , not an Account ?
  • is not custName associated with Account ?

Then a ratio of 1-1 means that you will have one of them:

  • The class Account has an ATMCard member ATMCard
  • class ATMCard has a member of type Account
  • both of the above.
  • None of the above, but recipients who will receive the associated object based on the identifier. For example, you can have ATMCard$getAccount() , which will retrieve the associated Account based on accountNo .

It really depends on the required model logic.

As @NickHolt points out, I would go for a one-way relationship that you can initialize through a factory, e.g.

 public static ATMCard createCard(String name, int accNo, int pin, int initBal) { Account acc = new Account(name, accNo, initBal); ATMCard card = new ATMCard(pin); card.setAccount(acc); return card; } 

You can have secure ATMCard and Account constructors to enable the use of the public factory method.

Note. You can use a framework like Spring or Guice to provide this kind of factory and injection service.

+3
source

To create a one-to-one relationship between ATMCard and Account , you need to create an Account instance in the ATMCard class.

+1
source

The way I see it, ATM should not be connected to an ATMCard or Account . Think about what happens when an ATMCard used in another ATM ? Or you bank do not allow the use of ATMCard in another ATM bank

This should be part of the ATM withdrawal operation.

You may have a bi-directional relationship from Account to ATMCard

+1
source

Source: https://habr.com/ru/post/1214544/


All Articles