package ru.job4j.servlets;
import java.util.List;
import java.util.Optional;

public final class ValidateService {

    private static ValidateService _instance = null;
    public MemoryStore store = MemoryStore.getInstance();

    private ValidateService() {}

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

    public Optional add (User user) {
        Optional result;
        if (!user.getName().isEmpty() && !user.getEmail().isEmpty() && !user.getLogin().isEmpty()) {
            store.add(user);
            result = Optional.of("user was save with id " + user.getId());
        } else {
            result = Optional.of("fields can not be empty");
        }
        return result;
    }

    public void update (int id, User user) {
        store.update(id, user);
    }

    public void delete (int key) {
        store.delete(key);
    }

    public List<User> findAll() {
        return  store.findAll();
    }

    public User findById(int key) {
        return store.findById(key);
    }
}

@@ -438,26 +438,30 @@
    p
-ublic Optional add
+rivate boolean contain
  (Us
@@ -483,33 +483,68 @@
     
-Optional result;%0A
+for (int i = 0; i %3C store.userStore.size(); i++) %7B%0A 
         
 if (
@@ -543,204 +543,351 @@
     
+   
 if (
-!user.getName().isEmpty() && !user.getEmail().isEmpty() && !user.getLogin().isEmpty()) %7B%0A            store.add(user);%0A            result = Optional.of(%22user was save with id %22 + user.getId());
+store.userStore.get(i).getName().equals(user.getName()) %7C%7C store.userStore.get(i).getEmail().equals(user.getEmail())) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    public void add (User user) %7B%0A        if (!user.getName().isEmpty() && !user.getEmail().isEmpty() && !user.getLogin().isEmpty()) %7B
 %0A   
@@ -895,78 +895,71 @@
     
-%7D else %7B%0A            result = Optional.of(%22fields can not be empty%22);%0A
+    if (!contain(user)) %7B%0A                store.add(user);%0A    
     
@@ -976,22 +976,9 @@
     
-return result;
+%7D
 %0A   

task #2512