1

enter image description here

Hello, so i have to implement a Bank management software based on this UML. Alright, since between Person and Bank there's composition, and the same goes with Account and Bank, i've decided to store all the data like this:

Hashmap <Person, ArrayList<Account>>

To each Person i can have a list of open Accounts(Spending or Saving) and i will access these fields from the Bank class(where I have add/remove Person, add/remove Account, deposit(Person,Account), withdraw(Person,Account)).

My question is the following: how do I add an account to the bank's HashMap, in the ArrayList pointed at by Person p?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Rares Macavei
  • 55
  • 1
  • 7
  • 2
    That UML diagram is faulty. It does not show any relationship between a `Person` and an `Account`. It is much too simplistic to serve as a guide for any implementation. – Jim Garrison May 11 '18 at 21:36
  • Possible duplicate of [HashMap in class diagram (UML)](https://stackoverflow.com/questions/8868459/hashmap-in-class-diagram-uml) – qwerty_so May 11 '18 at 22:51

1 Answers1

0
 Hashmap<Person, ArrayList<Account>> bankAccounts = new Hashmap<Person, ArrayList<Account>>();

 Person person = new Person("Rares"); // considering you can create a person with just a name field for example

 bankAccounts.get(person).add(new Account());
Rishikesh Dhokare
  • 3,559
  • 23
  • 34
  • That would throw a NPE. – Vasan May 11 '18 at 21:46
  • and why would that be? OP is already storing the person to account mappings in bankAccounts. Of course the list should be empty and can not be null. – Rishikesh Dhokare May 11 '18 at 21:48
  • Ok, then perhaps you could add a comment explaining that. As it is, your code looks like it can be used as-is but doesn't initialize (or even hint at initializing) the list in the map. – Vasan May 11 '18 at 21:52