package ru.job4j.monitore_synchronizy;

import net.jcip.annotations.ThreadSafe;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

/**
 * UserStorage.
 *
 * @author Alexander Ivanov
 * @version 1.0
 * @since 15.07.2017
 */
@ThreadSafe
public class UserStorage {
    /**
     * Storage for users.
     */
    private final Map<Integer, User> storage = new HashMap<>();

    /**
     * Add user to storage.
     *
     * @param user for adding.
     */
    public synchronized void add(User user) {
        storage.put(user.getId(), user);
    }

    /**
     * Setting new amount of money to user.
     *
     * @param userID    of user.
     * @param newAmount to update.
     * @return old amount of user's money.
     */
    public int update(int userID, int newAmount) {
        if (!storage.containsKey(userID)) {
            throw new NoSuchElementException("No such user");
        }
        User user = storage.get(userID);
        int oldValue = user.getAmount();
        synchronized (user) {
            user.setAmount(newAmount);
        }
        return oldValue;
    }

    /**
     * Delete user.
     *
     * @param userID of user.
     * @return deleting user.
     */
    public synchronized User delete(int userID) {
        return storage.remove(userID);
    }

    /**
     * Synchronized transfer money from one user to another.
     *
     * @param fromId user, which give money.
     * @param toId   user, which take money.
     * @param amount for transfer.
     * @return true if transfer is done.
     */
    public boolean transfer(int fromId, int toId, int amount) {
        if (!storage.containsKey(fromId) && !storage.containsKey(toId)) {
            throw new NoSuchElementException("No user");
        }
        User userFrom = storage.get(fromId);
        User userTo = storage.get(toId);
        synchronized (userFrom) {
            int amountUserFrom = userFrom.getAmount();
            if (amountUserFrom < amount) {
                return false;
            }
            synchronized (userTo) {
                update(fromId, amountUserFrom - amount);
                update(toId, userTo.getAmount() + amount);
            }
        }
        return true;
    }
}

@@ -767,16 +767,29 @@
  public 
+synchronized 
 int upda
@@ -812,32 +812,32 @@
 nt newAmount) %7B%0A
-
         if (!sto
@@ -1022,42 +1022,8 @@
 ();%0A
-        synchronized (user) %7B%0A    
     
@@ -1053,26 +1053,16 @@
 mount);%0A
-        %7D%0A
         
@@ -1545,16 +1545,29 @@
  public 
+synchronized 
 boolean 
@@ -1842,46 +1842,8 @@
 d);%0A
-        synchronized (userFrom) %7B%0A    
     
@@ -1893,28 +1893,24 @@
 ();%0A        
-  
-  
 if (amountUs
@@ -1936,28 +1936,24 @@
             
-    
 return false
@@ -1966,58 +1966,10 @@
     
-  
-  
 %7D%0A
-            synchronized (userTo) %7B%0A        
     
@@ -2021,24 +2021,16 @@
         
- 
-       
 update(t
@@ -2068,32 +2068,8 @@
 t);%0A
-            %7D%0A        %7D%0A
     

Add changing to UserStorage.