package ru.job4j;

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.
 */
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) {
        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;}

}

@@ -2146,32 +2146,66 @@
 , int amount) %7B%0A
+        synchronized (this) %7B%0A    
         if (stor
@@ -2236,32 +2236,36 @@
 t() %3C amount) %7B%0A
+    
             Syst
@@ -2318,32 +2318,36 @@
 %22);%0A            
+    
 return;%0A        
@@ -2338,35 +2338,43 @@
 return;%0A        
+   
+ 
 %7D%0A%0A
+    
         if (!sto
@@ -2443,16 +2443,20 @@
         
+    
 new NoSu
@@ -2500,32 +2500,36 @@
 ered%22);%0A        
+    
 %7D%0A%0A        int a
@@ -2515,32 +2515,36 @@
      %7D%0A%0A        
+  
+  
 int amountForFro
@@ -2563,32 +2563,36 @@
 amount;%0A        
+    
 int amountForToU
@@ -2610,24 +2610,28 @@
 t;%0A%0A        
+  
+  
 update(fromI
@@ -2657,24 +2657,28 @@
 r);%0A        
+    
 update(toId,
@@ -2677,16 +2677,17 @@
 te(toId,
+ 
 amountFo
@@ -2697,32 +2697,36 @@
 User);%0A%0A        
+    
 if (storage.get(
@@ -2756,23 +2756,63 @@
 0) %7B
-delete(fromId);
+%0A                delete(fromId);%0A            %7D%0A        
 %7D%0A  
@@ -2932,29 +2932,16 @@
  public 
-synchronized 
 Map%3CInte
@@ -2963,16 +2963,46 @@
 torage()
+ %7B%0A        synchronized (this)
  %7Breturn
@@ -3016,12 +3016,13 @@
 torage;%7D
+%7D
 %0A%0A%7D%0A

add file for chapter_004_7.3.2.