Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,50 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int MAX_CAPACITY = 10;
private K [] key;
private V [] value;
Comment on lines +7 to +8
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable names 'key' and 'value' (lines 8-9) are not informative. Consider using plural forms like 'keys' and 'values' since they store multiple elements.

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;
}
}
Loading