Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax;

import core.basesyntax.impl.StorageImpl;

public class Main {
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 import is unused and can be safely removed.

public static void main(String[] args) {
StorageImpl<Integer, String> storage = new StorageImpl<>();
String box = "box3";
storage.put(44, box);
storage.put(22, box);
storage.put(33, box);
storage.size();

System.out.println(storage.get(33));
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 method call has no effect because its return value is not assigned to any variable or used in any way. You can remove this line.

System.out.println(storage.size());
}
}
34 changes: 33 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,49 @@
import core.basesyntax.Storage;

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

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

@Override
public void put(K key, V value) {
if (key == null || value == null) {
throw new NullPointerException("Key or value cannot be null.");
}

for (int i = 0; i < size; i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You have duplicated key‑searching logic in both put() and get().
This violates the “Don’t repeat yourself” rule from the checklist.

Please extract this repeated logic into a private helper method, for example:

private int indexOfKey(K key) { ... }

Then call this method from both put() and get() to avoid code duplication.

if (keys[i] == key) {
values[i] = value;
return;
}
}
Comment on lines +20 to +25
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 an existing key is very similar to the one in the get method (lines 35-39). This violates checklist item #5: 'Don't create repeating code'. Consider extracting this logic into a private helper method that finds the index of a key. You can then call that method from both put() and get() to avoid duplication.


if (size < MAX_STORAGE_SIZE) {
keys[size] = key;
values[size] = value;
size++;
}
}

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

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