Skip to content

Commit

Permalink
add StorageImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
FroGitHub committed Jan 5, 2025
1 parent 9346a74 commit 8169e7a
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,48 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {

private static final int MAX_SIZE = 10;
private K[] keys;
private V[] values;
private int size;

public StorageImpl() {
keys = (K[]) new Object[MAX_SIZE];
values = (V[]) new Object[MAX_SIZE];
size = 0;
}

@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
if ((keys[i] == null && key == null) || (keys[i] != null && keys[i].equals(key))) {
values[i] = value;
return;
}
}

if (size < MAX_SIZE) {
keys[size] = key;
values[size] = value;
size++;
} else {
throw new IllegalStateException("Storage is full");
}
}

@Override
public V get(K key) {
for (int i = 0; i < size; i++) {
if ((keys[i] == null && key == null) || (keys[i] != null && keys[i].equals(key))) {
return values[i];
}
}
return null;
}

@Override
public int size() {
return -1;
return size;
}
}

0 comments on commit 8169e7a

Please sign in to comment.