package moneyTransfer.storage;

import moneyTransfer.user.Account;
import moneyTransfer.user.User;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class UserStorage implements Storage {
    private final transient Map<Integer, Account> accounts;
    private final Object monitor;

    public UserStorage() {
        this.accounts = new HashMap<>();
        this.monitor = new Object();
    }

    @Override
    public synchronized boolean addAccount(final Account account) {
        //synchronized (this.monitor) {
            final Account a = this.accounts.get(account.getId());
            if (a != null) {
                return false;
            }

            this.accounts.put(account.getId(), account);
            return true;
        //}
    }

    @Override
    public Account viewAccount(final Integer id) {
        synchronized (this.monitor) {
            final Account account = this.accounts.get(id);
            if (account != null) {
                return account;
            }

            return new User(new BigDecimal("-1"), "not found", -1);
        }
    }

    @Override
    public boolean updateAccount(final Integer id, final String name) {
        synchronized (this.monitor) {
            final Account oldVersion = this.accounts.get(id);
            if (oldVersion != null) {
                final Account updatedAccount = new User(
                        oldVersion.getAmount(), name, id
                );

                this.accounts.put(id, updatedAccount);
                return true;
            }

            return false;
        }
    }

    @Override
    public boolean delAccount(final Integer id) {
        synchronized (this.monitor) {
            final Account account = this.accounts.get(id);
            if (account != null) {
                this.accounts.remove(id);
                return true;
            }

            return false;
        }
    }
}

@@ -233,23 +233,16 @@
 ate 
-final transient
+volatile
  Map
@@ -442,29 +442,16 @@
  public 
-synchronized 
 boolean 
@@ -494,18 +494,16 @@
         
-//
 synchron
@@ -508,36 +508,31 @@
 onized (this
-.monitor
 ) %7B%0A
+%0A  
             
@@ -523,32 +523,34 @@
 %0A%0A              
+  
 final Account a 
@@ -595,24 +595,28 @@
             
+    
 if (a != nul
@@ -628,32 +628,36 @@
                 
+    
 return false;%0A  
@@ -658,35 +658,43 @@
 se;%0A            
+   
+ 
 %7D%0A%0A
+    
             this
@@ -738,32 +738,36 @@
 t);%0A            
+    
 return true;%0A   
@@ -763,24 +763,25 @@
 n true;%0A
+%0A
         
 //%7D%0A    
@@ -776,10 +776,8 @@
     
-//
 %7D%0A  
@@ -1218,32 +1218,24 @@
 onized (this
-.monitor
 ) %7B%0A        

add 4 ways synchronize threads