Skip to content
Open
Changes from 1 commit
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
32 changes: 31 additions & 1 deletion src/main/java/core/basesyntax/impl/StorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
package core.basesyntax.impl;

import core.basesyntax.Storage;
import java.util.Objects;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #6 states 'Don't use class Objects'. Replace Objects.equals() with direct null-safe comparison or use .equals() method on the key objects.


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

@Override
public void put(K key, V value) {
for (int i = 0; i < size; i++) {
if (Objects.equals(this.key[i], key)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #5 states 'Don't create repeating code'. The key comparison logic in put() and get() methods follows the same pattern. Extract this into a private helper method (e.g., findKeyIndex()) that returns the index of a key or -1 if not found.

this.value[i] = 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) {
for (int i = 0; i < size; i++) {
if (Objects.equals(this.key[i], key)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #5: Same repeating pattern issue as line 21. The key search loop exists in both put() and get() methods.

return this.value[i];
}
}
return null;
}

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