package ru.job4j.collections.bank;

import javax.jws.soap.SOAPBinding;
import java.util.*;


/**
 * Created by admin on 14.05.2017.
 */
public class Bank {
    protected Map<User, List<Account>> map = new LinkedHashMap<>();

    public void addUser(User user) {
        map.put(user, user.list);
    }

    public void deleteUser(User user) {
        if (map.containsKey(user)) {
            System.out.println("log");
            map.remove(user);
        }
    }

    public void addAccountToUser(User user, Account account) {
        if (user != null && map.containsKey(user)) {
            for (User user1 : map.keySet()) {
                if (user.equals(user1)) {
                    user1.list.add(account);

                }
            }
        }
    }

    public void deleteAccountFromUser(User user, Account account) {
        if (user != null && map.containsKey(user)) {
            for (User user1 : map.keySet()) {
                if (user.equals(user1)) {
                    user1.list.remove(account);
                }
            }
        }
    }

    public List<Account> getUserAccounts(User user) {
        List<Account> accounts = new ArrayList<>();
        if (user != null && map.containsKey(user)) {
            for (User user1 : map.keySet()) {
                if (user.equals(user1)) {
                    accounts = user1.list;
                }
            }
        }
        return accounts;
    }

    public boolean transferMoney(User srcUser, Account srcAccount, User dstUser, Account dstAccount, double amount) {
        boolean answer = false;
        User src = null;
        User dst = null;
        Set<User> set = map.keySet();
        for (User user : set) {
            if (user.equals(srcUser)) {
                src = user;
            }
            if (user != src && user.equals(dstUser)) {
                dst = user;
            }
        }
        if (map.containsKey(src) && map.containsKey(dst)) {
            if (map.get(srcUser).contains(srcAccount) && map.get(dst).contains(dstAccount)) {
                int indexSrc = map.get(src).indexOf(srcAccount);
                int indexDst = map.get(dst).indexOf(dstAccount);
                if (src != null && dst != null && src.list.get(indexSrc).value - amount >= 0) {
                    Account accountSrc = new Account(src.list.get(indexSrc).value - amount, src.list.get(indexSrc).requisites);
                    src.list.set(indexSrc, accountSrc);
                    Account accountDst = new Account(dst.list.get(indexDst).value + amount, dst.list.get(indexDst).requisites);
                    answer = true;
                    map.get(src).set(indexSrc, accountSrc);
                    map.get(dst).set(indexDst, accountDst);
                }
            }
        }
        return answer;
    }
}

@@ -591,467 +591,292 @@
     
-for (User user1 : map.keySet()) %7B%0A                if (user.equals(user1)) %7B%0A                    user1.list.add(account);%0A%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public void deleteAccountFromUser(User user, Account account) %7B%0A        if (user != null && map.containsKey(user)) %7B%0A            for (User user1 : map.keySet()) %7B%0A                if (user.equals(user1)) %7B%0A                    user1.list.remove(account);%0A                %7D%0A            %7D
+List accounts = map.get(user);%0A            accounts.add(account);%0A        %7D%0A    %7D%0A%0A    public void deleteAccountFromUser(User user, Account account) %7B%0A        if (user != null && map.containsKey(user)) %7B%0A            List accounts = map.get(user);%0A            accounts.remove(account);
 %0A   
@@ -955,25 +955,16 @@
     List
-%3CAccount%3E
  account
@@ -972,24 +972,11 @@
  = n
-ew ArrayList%3C%3E()
+ull
 ;%0A  
@@ -1042,158 +1042,33 @@
     
-for (User user1 : map.keySet()) %7B%0A                if (user.equals(user1)) %7B%0A                    accounts = user1.list;%0A                %7D%0A            %7D
+accounts = map.get(user);
 %0A   
@@ -1268,321 +1268,50 @@
     
-User src = null;%0A        
+if (src
 User 
-dst 
+!
 = null
-;%0A        Set%3CUser%3E set = map.keySet();%0A        for (User user : set) %7B%0A            if (user.equals(srcUser)) %7B%0A                src = user;%0A            %7D%0A            if (user != src && user.equals(dstUser)) %7B%0A                dst = user;%0A            %7D%0A        %7D%0A        if (
+ && dstUser != null && 
 map.
@@ -1325,16 +1325,20 @@
 sKey(src
+User
 ) && map
@@ -1353,16 +1353,20 @@
 sKey(dst
+User
 )) %7B%0A   
@@ -1430,16 +1430,20 @@
 .get(dst
+User
 ).contai
@@ -1480,120 +1480,101 @@
     
-int indexSrc = map.get(src).indexOf(srcAccount);%0A                int indexDst = map.get(dst).indexOf(dst
+for (Account account : map.get(srcUser)) %7B%0A                    if (account.equals(src
 Account)
 ;%0A  
@@ -1565,25 +1565,27 @@
 (srcAccount)
-;
+) %7B
 %0A           
@@ -1593,64 +1593,27 @@
     
-if (src != null && dst != null && src.list.get(indexSrc)
+        if (account
 .val
@@ -1656,71 +1656,31 @@
     
+    
-Account accountSrc = new Account(src.list.get(indexSrc)
+    account
 .value -
  amo
@@ -1679,109 +1679,93 @@
 ue -
+=
  amount
-, src.list.get(indexSrc).requisites);%0A                    src.list.set(indexSrc, accountSrc);%0A
+;%0A                            answer = true;%0A                        %7D%0A      
     
@@ -1762,32 +1762,38 @@
                 
+    %7D%0A
           Accoun
@@ -1790,270 +1790,209 @@
     
-Account accountDst = new Account(dst.list.get(indexDst).value + amount, dst.list.get(indexDst).requisites);%0A                    answer = true;%0A                    map.get(src).set(indexSrc, accountSrc);%0A                    map.get(dst).set(indexDst, accountDst);
+      %7D%0A                for (Account account : map.get(dstUser)) %7B%0A                    if (account.equals(dstAccount) && answer) %7B%0A                        account.value += amount;%0A                    %7D
 %0A   

task 10038