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
42 changes: 40 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,55 @@
import core.basesyntax.Storage;

public class StorageImpl<K, V> implements Storage<K, V> {
private static final int SIZE_ARRAY = 10;
private K[] keys;
private V[] values;
private int size;

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

private int getIndex(K key) {
for (int i = 0; i < size; i++) {
if ((key == null && this.keys[i] == null)
|| (this.keys[i] != null && this.keys[i].equals(key))) {
return i;
}
}
Comment on lines +18 to +23
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This loop for finding a key is nearly identical to the one in the get method. This violates checklist item #5: 'Don't create repeating code'. Consider extracting this search logic into a separate private helper method that returns the index of a given key. Both put and get methods could then use this helper method to avoid code duplication.

return -1;
}

@Override
public void put(K key, V value) {
int index = getIndex(key);

if (index != -1) {
// ключ уже есть → обновляем
values[index] = value;
return;
}

// ключа нет → добавляем
if (size < keys.length) {
keys[size] = key;
values[size] = value;
size++;
} else {
System.out.println("The storage is full");
}
}

@Override
public V get(K key) {
return null;
int index = getIndex(key);
return index == -1 ? null : values[index];
}

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