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

import core.basesyntax.Storage;

public class Main extends StorageImpl {
public static void main(String[] args) {
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 #3 violation: Constant name should follow UPPER_SNAKE_CASE naming convention (e.g., MAX_ELEMENTS_CAPACITY) and could be more informative about its purpose.


Storage<Integer, String> storage = new StorageImpl<>();
storage.put(1991, "Independence of Ukraine");

String value = storage.get(1991);
int size = storage.size();

System.out.println("Value: " + value);
System.out.println("Size: " + size);
}
}
32 changes: 31 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,47 @@
import core.basesyntax.Storage;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line before class declaration. Remove according to checklist requirements.

public class StorageImpl<K, V> implements Storage<K, V> {
public static final int MAX_Keys_And_Values = 10;
protected final K[] keys;
protected final V[] values;
private int size = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All non-constant fields should be initialized in the constructor. Move size = 0 initialization from field declaration (line 9) to the constructor.


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line before method implementation. Remove according to checklist.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line before method implementation. Remove according to checklist.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line before method implementation. Remove according to checklist.

@Override
public V get(K key) {
for (int i = 0; i < keys.length; 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.

Logical inconsistency: put() iterates using size while get() uses keys.length. When checking for keys, get() should use size to avoid accessing uninitialized array positions.

if (keys[i] == null && key == null) {
return values[i];
} else if (keys[i] != null && keys[i].equals(key)) {
return values[i];
}
}
return null;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty line before method implementation. Remove according to checklist.

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