diff --git a/src/main/java/core/basesyntax/impl/StorageImpl.java b/src/main/java/core/basesyntax/impl/StorageImpl.java index 455a7b080..c29138722 100644 --- a/src/main/java/core/basesyntax/impl/StorageImpl.java +++ b/src/main/java/core/basesyntax/impl/StorageImpl.java @@ -3,17 +3,50 @@ import core.basesyntax.Storage; public class StorageImpl implements Storage { + private static final int MAX_CAPACITY = 10; + private K [] key; + private V [] value; + private int size; + + public StorageImpl() { + this.key = (K[]) new Object[MAX_CAPACITY]; + this.value = (V[]) new Object[MAX_CAPACITY]; + this.size = 0; + } + + private int findIndex(K key) { + for (int i = 0; i < size; i++) { + if (key == null ? this.key[i] == null : key.equals(this.key[i])) { + return i; + } + } + return -1; + } + @Override public void put(K key, V value) { + int index = findIndex(key); + if (index != -1) { + this.value[index] = value; + return; + } + if (size < MAX_CAPACITY) { + this.key[size] = key; + this.value[size] = value; + size++; + } else { + System.out.println("Array is full"); + } } @Override public V get(K key) { - return null; + int index = findIndex(key); + return index != -1 ? this.value[index] : null; } @Override public int size() { - return -1; + return size; } }