package ru.job4j.bank;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * all users & their accounts with all accessible operations.
 */
public class Bank {
    /**
     * all users & their accounts.
     */
    private Map<User, List<Account>> bankClients = new HashMap<>();

    /**
     * getter.
     * @return - bankClients.
     */
    public Map<User, List<Account>> getBankClients() {
        return bankClients;
    }

    /**
     * Constructor.
     * @param user - added user.
     */
    public void addUser(User user) {
        bankClients.put(user, new ArrayList<Account>());
    }

    /**
     * @param user - deletted user.
     */
    public void deleteUser(User user) {
        bankClients.remove(user);
    }

    /**
     * @param user - user, to whom we add account.
     * @param account - added account.
     */
    public void addAccountToUser(User user, Account account) {
        bankClients.get(user).add(account);
    }

    /**
     * @param user - user, from whom we delete account.
     * @param account - deletted account.
     */
    public void deleteAccountFromUser(User user, Account account) {
        bankClients.get(user).remove(account);
    }

    /**
     * transfer money(amount) from srcUser & srcAccount to dstUser & dstAccount.
     * @param srcUser - from that user we get money.
     * @param srcAccount - from that user's account we get money.
     * @param dstUser - user, to whom we put money.
     * @param dstAccount - user's account, where we put money.
     * @param amount - amount of money to transfer.
     * @return - true, if success.
     */
    public boolean transferMoney(User srcUser, Account srcAccount, User dstUser, Account dstAccount, double amount) {
        boolean flag = false;

        for (Account tempAccount : bankClients.get(srcUser)) {
            if (tempAccount.equals(srcAccount)) {
                if (tempAccount.getValue() >= amount) {
                    tempAccount.setValue(tempAccount.getValue() - amount);
                    flag = true;
                    break;
                } else {
                    System.out.println("Haven't enough money for finish operation.");
                    flag = false;
                }
            }
        }

        if (flag) {
            for (Account tempAccount : bankClients.get(dstUser)) {
                if (tempAccount.equals(dstAccount)) {
                    tempAccount.setValue(tempAccount.getValue() + amount);
                    break;
                }
            }
        }
        return flag;
    }
}
@@ -1785,140 +1785,246 @@
     
-boolean flag = false;%0A%0A        for (Account tempAccount : bankClients.get(srcUser)) %7B%0A            if (tempAccount.equals(src
+Account source = findRequiredAcc(srcUser, srcAccount);%0A        if (source == null) %7B%0A            System.out.println(%22Source account doesn't found. Please, try again%22);%0A        %7D%0A%0A        Account dest = findRequiredAcc(dstUser, dst
 Account)
 ) %7B%0A
@@ -2011,35 +2011,33 @@
 ser, dstAccount)
-) %7B
+;
 %0A               
@@ -2029,39 +2029,165 @@
         
-        if (tempAccount
+if (dest == null) %7B%0A            System.out.println(%22Destination account doesn't found. Please, try again%22);%0A        %7D%0A%0A        boolean allow = source
 .getValu
@@ -2195,19 +2195,17 @@
 () %3E
-=
  amount
-) %7B
+;%0A
 %0A   
@@ -2213,150 +2213,139 @@
     
-            tempAccount.setValue(tempAccount.getValue() - amount);%0A                    flag = true;%0A                    break;%0A       
+if (allow) %7B%0A            source.setValue(source.getValue() - amount);%0A            dest.setValue(dest.getValue() + amount);%0A
         
  %7D e
@@ -2340,17 +2340,16 @@
         
- 
 %7D else %7B
@@ -2349,24 +2349,16 @@
  else %7B%0A
-        
         
@@ -2431,32 +2431,35 @@
 %22);%0A        
+%7D%0A%0A
         
     flag = f
@@ -2450,93 +2450,294 @@
     
-    flag = false;%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (flag) %7B%0A    
+return allow;%0A    %7D%0A%0A    /**%0A     * find required account.%0A     * @param user - User.%0A     * @param account - required User's account.%0A     * @return - required account or null.%0A     */%0A    public Account findRequiredAcc(User user, Account account) %7B%0A        Account reqAccount = null;%0A
     
@@ -2783,20 +2783,17 @@
 nts.get(
-dstU
+u
 ser)) %7B%0A
@@ -2800,28 +2800,24 @@
             
-    
 if (tempAcco
@@ -2827,20 +2827,17 @@
 .equals(
-dstA
+a
 ccount))
@@ -2859,71 +2859,34 @@
     
-    tempAccount.setValue(tempAccount.getValue() + amount);%0A    
+reqAccount = tempAccount;%0A
     
@@ -2908,26 +2908,8 @@
 ak;%0A
-                %7D%0A
     
@@ -2947,12 +2947,18 @@
 urn 
-flag
+reqAccount
 ;%0A  

Test task.003 - Collection. Simplifying of method transfernoney().