package ru.job4j.servlets;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class MemoryStore implements Store {

    private static MemoryStore _instance = null;

    public MemoryStore() {}

    public static synchronized MemoryStore getInstance() {
        if (_instance == null)
            _instance = new MemoryStore();
        return _instance;
    }

    private AtomicInteger id = new AtomicInteger(0);
    public  List<User> userStore = new CopyOnWriteArrayList<User>();


    @Override
    public void add(User user) {
        user.setId(id.getAndIncrement());
        userStore.add(user);
    }

    @Override
    public void update(int id, User user) {
        userStore.set(id, user);
    }

    @Override
    public void delete(int key) {
        userStore.remove(key);
    }

    @Override
    public List<User> findAll() {
        return userStore;
    }

    @Override
    public User findById(int key) {
        int index = 0;
        User user = new User();
        for (User u : this.userStore) {
            if (this.userStore.get(index).id == key) {
                user = this.userStore.get(index);
                break;
            }
        }
        return user;
    }


}

@@ -754,118 +754,641 @@
     
-userStore.set(id, user);%0A    %7D%0A%0A    @Override%0A    public void delete(int key) %7B%0A        userStore.remove(key);
+for (int i = 0; i %3C userStore.size(); i++) %7B%0A            if (id == userStore.get(i).getId()) %7B%0A                userStore.get(i).setName(user.getName());%0A                userStore.get(i).setEmail(user.getEmail());%0A                userStore.get(i).setCreateDate(user.createDate);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void delete(int id) %7B%0A        for (int i = 0; i %3C userStore.size(); i++) %7B%0A            if (id == userStore.get(i).getId()) %7B%0A                userStore.get(i).setName(null);%0A                userStore.get(i).setEmail(null);%0A                userStore.get(i).setCreateDate(null);%0A            %7D%0A        %7D
 %0A   
@@ -1519,37 +1519,13 @@
 int 
-key) %7B%0A        int index = 0;
+id) %7B
 %0A   
@@ -1613,43 +1613,23 @@
 if (
-this.userStore.get(index).id
+u.getId()
  == 
-key
+id
 ) %7B%0A
@@ -1652,59 +1652,168 @@
 user
- = this.userStore.get(index);%0A                break
+.setId(u.getId());%0A                user.setName(u.getName());%0A                user.setEmail(u.getEmail());%0A                user.setCreateDate(u.getCreateDate())
 ;%0A  
@@ -1861,12 +1861,10 @@
 ;%0A    %7D%0A
-%0A%0A
 %7D%0A

task #2512