From UML to Java, Simple association -



From UML to Java, Simple association -

this image i'm going transfer uml java, don't know how lock them , don't know how create 1 bankaccount locked 1 person.

how connect 2 classes??

here code far

my main method

public class upp5 { public static void main(string[] args) { person2 david = new person2(); bankaccount acc1 = new bankaccount(); bankaccount acc2 = new bankaccount(); david.setname("david"); david.setpnr("551012-8978"); acc1.setbnr("37"); acc2.setbnr("38"); system.out.println("namn: " + david.getname() + " \npersonnummer:" + david.getpnr()); system.out.println(acc1.getbnr() + "\n" + acc2.getbnr()); } }

bankaccount.java:

public class bankaccount { private string bnr; private double balance; public void credit() { } public void withdraw(){ } public string getbnr(){ homecoming bnr; } public void setbnr(string newaccount){ bnr = newaccount; } public void createaccount(string newnbr){ bnr = newnbr; } }

and person2.java

public class person2 { private string pnr; private string name; //koppla konto till pnr public void addaccount(bankaccount a){ } //skapa pnr och namn public void setpnr(string newpnr) { pnr = newpnr; } public void setname(string newname){ name = newname; } // hämta pnr och namn public string getpnr(){ homecoming pnr; } public string getname(){ homecoming name; } }

you need define list<bankaccount> person2 entity:

public class person2 { private string pnr; private string name; // list of bank accounts (from 0 n) person can have. private list<bankaccount> accounts; //koppla konto till pnr public void addaccount(bankaccount a){ if (accounts = null) accounts = new arraylist<bankaccount>(); accounts.add(a); } //skapa pnr och namn public void setpnr(string newpnr) { pnr = newpnr; } public void setname(string newname){ name = newname; } // hämta pnr och namn public string getpnr(){ homecoming pnr; } public string getname(){ homecoming name; } // include getters setters }

edit1: suggested @nathancastlehow if want double relationship, bankaccount.java must have person2 attibute

public class bankaccount { private string bnr; private double balance; // 1 bank business relationship can owned single person private person2 person; public void credit() { } public void withdraw(){ } public string getbnr(){ homecoming bnr; } public void setbnr(string newaccount){ bnr = newaccount; } // generate getters setters public person2 getperson(){ ...... } }

java associations uml

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -