package ru.job4j;

import net.jcip.annotations.ThreadSafe;

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

/**
 * Task 7.3.2.
 * Create User Storage
 *
 * @author Anton Vasilyuk on 27.07.2017.
 * @version 1.0.
 */
@ThreadSafe
public class UserStorage {

    /**.
     * @storage is structure for data about the users
     */
    private Map<Integer, User> storage;

    /**.
     * Constructor for this class
     */
    public UserStorage() {
        storage = new HashMap<>();
    }

    /**.
     * Method for add this user to the structure
     * @param user is user for adding
     */
    public void add(User user) {
        synchronized (this) {
            if (user != null && !storage.containsKey(user.getId())) {
                storage.put(user.getId(), user);
            } else {
                return;
            }
        }
    }

    /**.
     * Method for update data about the user
     * @param id user to update
     * @param amount is change amount the user
     */
    public void update(Integer id, int amount) {
        synchronized (this) {
            if (storage.get(id) != null && amount != 0 && storage.containsKey(id)) {
                if (!storage.containsKey(id)) {new NoSuchElementException("This id no registered");}
                int newAmount = storage.get(id).getAmount() + amount;
                User user = storage.get(id);
                user.setAmount(newAmount);
                storage.put(id, user);
            } else {
                return;
            }
        }
    }

    /**.
     * Method for delete user from this structure
     * @param id is id from search this user
     */
    public void delete(Integer id) {
        synchronized (this) {
            if (id != null && storage.containsKey(id)) {
                storage.remove(id);
            } else {
                return;
            }
        }
    }

    /**.
     * Method for transfer money from first user account to second user account
     * @param fromId the user who transfer
     * @param toId the user which trahsfer
     * @param amount is the amount of money
     */
    public void transfer(Integer fromId, Integer toId, int amount) {
        synchronized (this) {
            if (storage.get(fromId).getAmount() < amount) {
                System.out.printf("The user has insufficient funds");
                return;
            }

            if (!storage.containsKey(fromId) || !storage.containsKey(toId)) {
                new NoSuchElementException("This id no registered");
            }

            int amountForFromUser = 0 - amount;
            int amountForToUser = amount;

            update(fromId, amountForFromUser);
            update(toId,amountForToUser);

            if (storage.get(fromId).getAmount() == 0) {delete(fromId);}
        }
    }

    /**.
     * The method for checking change in this the data structure
     * @return storage
     */
    public synchronized Map<Integer, User> getStorage() {return this.storage;}

    public void printMap() {
        System.out.println(storage);
    }
}

@@ -2211,42 +2211,8 @@
 ) %7B%0A
-        synchronized (this) %7B%0A    
     
@@ -2259,28 +2259,24 @@
 %3C amount) %7B%0A
-    
             
@@ -2333,36 +2333,32 @@
 %22);%0A            
-    
 return;%0A        
@@ -2353,35 +2353,27 @@
 rn;%0A        
-  
-  
 %7D%0A%0A
-    
         if (
@@ -2446,20 +2446,16 @@
         
-    
 new NoSu
@@ -2503,35 +2503,27 @@
 %22);%0A        
- 
-   %7D%0A%0A    
+%7D%0A%0A
         int 
@@ -2546,36 +2546,32 @@
 r = 0 - amount;%0A
-    
         int amou
@@ -2597,28 +2597,24 @@
 t;%0A%0A        
-    
 update(fromI
@@ -2640,28 +2640,24 @@
 r);%0A        
-    
 update(toId,
@@ -2675,36 +2675,32 @@
 User);%0A%0A        
-   
- 
 if (storage.get(
@@ -2743,26 +2743,16 @@
 omId);%7D%0A
-        %7D%0A
     %7D%0A%0A 
@@ -2939,78 +2939,6 @@
 ;%7D%0A%0A
-    public void printMap() %7B%0A        System.out.println(storage);%0A    %7D%0A
 %7D%0A

add file for chapter_004_7.3.2