diff --git a/src/main/java/core/basesyntax/impl/StorageImpl.java b/src/main/java/core/basesyntax/impl/StorageImpl.java index 455a7b080..c35f9ea08 100644 --- a/src/main/java/core/basesyntax/impl/StorageImpl.java +++ b/src/main/java/core/basesyntax/impl/StorageImpl.java @@ -3,17 +3,55 @@ import core.basesyntax.Storage; public class StorageImpl implements Storage { + private static final int MAX_SIZE = 10; + private int size; + private K[] keys; + private V[] values; + + @SuppressWarnings("unchecked") + public StorageImpl() { + this.size = 0; + keys = (K[]) new Object[MAX_SIZE]; + values = (V[]) new Object[MAX_SIZE]; + } + @Override public void put(K key, V value) { + int index = findKeyIndex(key); + + if (index != -1) { + values[index] = value; + return; + } + if (size == MAX_SIZE) { + throw new IllegalStateException("Storage is full"); + } + keys[size] = key; + values[size] = value; + size++; } @Override public V get(K key) { - return null; + int index = findKeyIndex(key); + if (index == -1) { + return null; + } + return values[index]; } @Override public int size() { + return size; + } + + private int findKeyIndex(K key) { + for (int i = 0; i < size; i++) { + if ((keys[i] == null && key == null) + || (keys[i] != null && keys[i].equals(key))) { + return i; + } + } return -1; } }